SERVER/Linux

[Linux] CentOS 7 에서 NGINX 설치하기

Dev.Moong 2023. 2. 16. 16:54

 

오늘의 포스팅 내용은 리눅스에서 Centos 7 에서 NGINX 설치하는 방법에 대해서 작성해 보겠습니다.

 

1. NGINX 란?

 

일단 NGINX에 알아보기 전에 웹서버(Web Server)에 대해 알아보도록 하겠습니다.😊

 

웹서버(Web Server)는 클라이언트(사용자)가 브라우저 주소창에 url을 입력하여 화면을 요청할때, HTML, CSS와 같은 정적 데이터를 클라이언트에 전달하고 동적 데이터가 필요하면 WAS(Web Application Server)에 전달하는 역할을 합니다.

 

대표적인 웹서버 종류에는 Apache, Nginx, IIS가 있습니다.

<웹 서비스 구성도>

 

이번에는 NGINX에 대해 알아보도록 하겠습니다.

 

NGINX란 트래픽이 많은 웹사이트의 서버(WAS)를 도와주는 비동기 이벤트 기반구조의 경량화 웹 서버 프로그램입니다. 위에 웹서버에 대해 설명한 것과 같이 클라이언트로부터 요청을 받았을 때 요청화면에 맞는 정적 데이터를 응답해주는 HTTP Web Server 역할을 합니다. 그 외 기능으로는 WAS 서버 로드밸런싱 역할도 하기도 하며, Mail 프록시 서버 역할을 하기도 합니다.

 

 

 

2. NGINX 설치하기

 

우선 리눅스 서버에 NGINX 설치하기 앞에 리눅스 OS 정보를 확인하는 명령어에 대해 알아보도록 하겠습니다.😃

  • OS 버전 확인 : rpm --query centos-release
  • CPU 정보 확인 : cat /proc/cpuinfo
  • CPU 코어 수 : nproc
  • 메모리 용량 확인 : free -h
  • 하드디스크 확인 : fdisk -l

해당 명령어를 알아두시면 도움이 많이 됩니다.👍

 

 


이제 NGINX 설치를 시작해 보겠습니다.


1. 패키지 설치

[root]$ yum install yum-utils createrepo


2. Repository 생성

[root]$ mkdir /var/tmp/nginx

[root]$ mkdir /var/tmp/nginx-installroot


3. yum repo에 "nginx" 패키지 저장소 설정 추가

[root]$ vi /etc/yum.repos.d/nginx.repo

 

여기서 nginx.repo에 아래와 같이 작성합니다.

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

4. yum install 명령어를 사용하여 nginx를 설치

[root]$ yum install -y nginx


5. 방화벽 확인

[root]$ systemctl status firewalld

= > Active 상태가 inactive면 방화벽 비활성화 상태이고, active이면 방화벽이 활성화 중인 상태입니다.

 

** 방화벽 실행 / 중지 명령어에 대해서는 추후에 포스팅 하도록 하겠습니다.**


6. default.conf 에서 NGINX 포트 설정

[root]$ cd /etc/nginx/conf.d

[root]$ vi default.conf

 

여기서 default.conf 에 아래와 같이 작성합니다.

server {
    listen       80;
    server_name  MoongProject;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        proxy_pass      http://localhost:8080;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_read_timeout 1800;
        proxy_connect_timeout 1800;
        proxy_send_timeout 1800;
        send_timeout 1800;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

7.  부팅시 자동시작 설정

[root]$ systemctl enable nginx

 

위 명령어는 서버 부팅시 자동으로 nginx 서비스를 구동하기 위해서 설정하는 명령어 입니다.


8.  서비스 시작 

[root]$ systemctl start nginx

 

 


8.  서비스 상태 확인

[root]$ systemctl status nginx

 

= > Active 상태가 inactive면 NGINX가 중지인 상태이고, active이면 NGINX가 실행중인 상태입니다.


9. 실행화면

 

정상적으로 NGINX가 실행되었다면, http://localhost:8080 접속시 아래와 같은 페이지가 보여집니다. 

 

 


NGINX 설치 방법에 대해 포스팅을 마치도록 하겠습니다.😄

 

 

 

참고 : https://velog.io/@bellpro/Web-Server-WAS-%EC%B0%A8%EC%9D%B4%EC%A0%90-2021.11.29