본문 바로가기
Research/Server

서버_AWS EC2 Ubuntu에 NGinx 설치하는 방법

by RIEM 2023. 3. 6.
728x90

들어가기

AWS EC2 Ubuntu에 Nginx를 설치하는 방법을 알아보자. (Ubuntu 버전은 18.04)

설치

# Nginx 설치
sudo apt-get install nginx

# Nginx 폴더로 이동
cd /etc/nginx

# Nginx 서버 실행
sudo service nginx start

# Nginx 실행 확인
ps -ef | grep nginx

접속

Screen Shot 2023-03-06 at 9 37 05 PM
퍼블릭 IPv4 주소로 이동하면 Nginx가 반겨주는 화면이 뜬다.

만약 나오지 않는다면 80번 포트가 닫혀있을 가능성이 크니, EC2 인스턴스 보안그룹의 인바운드에 80포트를 열어주도록 하자.

환경설정 생성

Nginx config 구조

넘어가기 전에 nginx.conf 구조를 한번 체크하고 넘어가자면 이렇다.

worker_processes  1;
events {
    worker_connections  1024;
}
http { 
    include       mime.types;
    server {
        listen       80;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}
  • work_processes : 코어 모듈을 설정
  • http 블록 : server, location의 루트 블록
  • server 블록 : 웹사이트 선언 시 사용. Virtual Host 개념
  • location 블록 : 특정 URL 처리 방법 정의.
# 환경설정 파일 위치 찾기
sudo find / -name nginx.conf

보통 /etc/nginx/nginx.conf 이 경로에 있다. 가서 vim 에디터로 http{} 내 이 내용을 추가해주자.

...
http {

  server {
    listen 80;
    server_name localhost;

  location / {
    proxy_pass http://localhost:3000/;
  }
}
...

포트 포워딩만 설정해주었다. 이 외 다양한 옵션들을 설정해줄 수 있긴한데 지금은 생략하겠다.

# Ubuntu 버전에 따라 다른 반영 커맨드

# service(설정 파일 변경 반영)
sudo service nginx reload;

# systemctl(설정 파일 변경 반영)
sudo systemctl restart nginx

# 실행 확인
sudo service nginx status;

주요 명령어

service nginx status
service nginx start
service nginx stop
service nginx restart
service nginx reload

참조

728x90

댓글