Nginx
Nginx 설치
Nginx를 관리자 권한으로 설치한다.
- AWS 터미널
$ sudo apt install nginx
Nginx 설정
Nginx를 사용하려면 설정이 필요하다. 설정을 위해 /etc/nginx/sites-avaliable/
디렉토리로 이동한다. /etc/nginx/sites-avaliable
은 Nginx 설정 파일이 위치한 디렉토리이다.
- AWS 터미널
$ cd /etc/nginx/sites-avaliable/
이어서 다음과 같은 Nginx 설정 파일을 작성한다. 이 역시 시스템 디렉토리이므로 관리자 권한으로 작성해야 한다.
$ sudo nano myapi
myapi 파일을 다음과 같이 작성한다.
server {
listen 80;
server_name {server_ip_address};
location = /favicon.ico { access_log off; log_not_found off; }
localtion / {
include proxy_params;
proxy_pass http://unix:/tmp/myapi.sock;
}
}
listen 80
은 웹 서버를 80 포트로 서비스하도록 하는 설정이다. HTTP 프로토콜의 기본 포트는 80이므로 앞으로 3.37.58.70:8000
(예시 ip 주소)에서 포트 번호를 생략한 3.37.58.70
에 접속해도 서비스에 접속할 수 있을 것이다.
server_name은 우리가 사용하는 고정 IP를 등록한다.
location / { ... }
은 /
URL로 시작하는 모든 요청을 Gunicorn 소켓이 처리하게 한다.
이제 작성한 myapi 파일을 Nginx가 환경 파일로 읽을 수 있도록 설정해야 한다. 다음과 같이 etc/nginx/sites-enabled
디렉토리로 이동한다.
- AWS 터미널
$ cd /etc/nginx/sites-enabled/
sites-enabled 디렉토리는 sites-avaliabel 디렉토리에 있는 설정 파일 중에서 활성화 하고 싶은 것을 링크로 관리하는 디렉토리이다.
ls 명령을 수행하면 현재 default 설정 파일만 링크된 것을 확인할 수 있다.
(myapi) ubuntu@jumpto:/etc/nginx/sites-enabled$ ls
default
먼저 default를 삭제한다.
$ sudo rm default
그리고 myapi 파일을 링크한다.
$ sudo ln -s /etc/nginx/sites-avaliable/myapi
ls 명령을 수행하면 default는 사라지고 myapi 링크만 남는다는 것을 확인할 수 있다.
(myapi) ubuntu@jumpto:/etc/nginx/sites-enabled$ ls
myapi
Nginx 실행
앞에서 작성한 Nginx 설정을 적용하려면 Nginx를 재시작해야 한다.
$ sudo systemctl restart nginx
혹시 Nginx 설정 파일에 오류가 발생했다면?
Nginx의 설정 파일에 오류가 있는지 확인하는 방법은 다음과 같다.
(myapi) ubuntu@jumpto:/etc/nginx/sites-enabled$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
sudo nginx -t
명령을 수행할 때 오류가 발생하면 설정 파일이 올바르지 않은 것이므로 Nginx 서버가 정상으로 실행되지 않는다. 그런 경우에는 설정 파일을 다시 작성하 ㄴ뒤 Nginx를 다시 실행해야 한다. 다음은 Nginx를 종료하고 다시 실행하는 명령이다.
$ sudo systemctl stop nginx
$ sudo systemctl start nginx