Study/Linux

[NginX] NGINX 소스 컴파일로 설치하기

Dream Amal 2024. 5. 27.

Nginx 로 TPC Proxy를 구축하기 위해서는 stream모듈을 사용해야 합니다.
기존 RPM 관리 도구 (dnf, yum) 를 사용하여 nginx를 설치할 경우 stream 모듈을 활용할 수 없습니다.
때문에 소스 컴파일을 통해 nginx를 설치하고, tcp proxy를 설정하는 방법에 대해 2 part로 나누어 설명해보고자합니다.

환경 구성

nginx 설치 환경

  • OS : Rocky Linux 8
  • Account : root
  • SSH 접속

패키지 관리자 업데이트

  • dnf 란?
    • yum 의 update 버전으로, 패키지 설치, 의존성 관리를 쉽게 해주는 tool
dnf -y update

Nginx 소스 컴파일 설치

01. 의존성 패키지 설치

dnf -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel

02. 원하는 버전의 nginx 소스 코드 다운로드

stream 모듈에서 server_name 지시어를 사용하여 Domain을 분기해주기 위해서는

nginx 1.25.5 이상의 버전을 다운로드 받아야 합니다.

# 설치하기 원하는 경로로 이동
cd /usr/local/src

# 원하는 버전의 nginx 다운로드
wget http://nginx.org/download/nginx-1.25.5.tar.gz

# 압축 해제
tar xvf nginx-1.25.2.tar.gz

03. 컴파일 옵션 설정

컴파일 할 때, --with-stream 옵션과 함께 실행하여야 stream 모듈 사용시 발생하는 에러를 방지할 수 있습니다.

# 압축 해제한 nginx 디렉토리로 이동
cd nginx-1.25.5

# 컴파일 옵션 설정
./configure --prefix=/usr/local/nginx **--with-stream** --with-http_ssl_module --with-http_v2_module --with-http_v3_module
  • 컴파일 옵션에 대한 설명
    • --prefix=/usr/local/nginx : nginx 를 /usr/local/nginx 디렉토리에 설치
    • stream, SSL, HTTP/2, HTTP/3 모듈을 활성화

04. 컴파일 설치 진행

# 위의 옵션을 바탕으로 소스를 컴파일함
make

# make를 통해 만들어진 설치파일을 설치 (알맞은 디렉터리에 복사)
make install

# 설치가 완료된 nginx 버전 확인
/usr/local/nginx/sbin/nginx -v

05. nginx 서비스 시작 및 자동 실행 설정

nginx 소스 컴파일 설치할 경우 PID 파일 경로 설정nginx.service 파일수동으로 생성해줘야 합니다.

  1. nginx PID 파일 경로 설정

     vi /usr/local/nginx/conf/nginx.conf
    
     # vi 편집기에서 아래 내용을 찾아 수정해야 함
    
     ## 변경 전
     #pid        logs/nginx.pid;
    
     ## 변경 후
     pid        /run/nginx.pid;
  2. nginx.service 파일 생성

     vi /etc/systemd/system/nginx.service
    
     # service 파일의 위치는 반드시 위와 같아야 함
     # vi 편집기에서 아래 내용을 복사하여 붙혀넣야 함
    
     [Unit]
     Description=nginx - high performance web server
     Documentation=http://nginx.org/en/docs/
     After=network-online.target remote-fs.target nss-lookup.target
     Wants=network-online.target
    
     [Service]
     Type=forking
     PIDFile=/run/nginx.pid
     ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
     ExecReload=/usr/local/nginx/sbin/nginx -s reload
     ExecStop=/usr/local/nginx/sbin/nginx -s stop
    
     [Install]
     WantedBy=multi-user.target
  3. nginx.service 등록

     # 서비스 설정을 daemon에 반영
     systemctl daemon-reload
    
     # 자동 실행 설정
     systemctl enable nginx
    
     # nginx 상태 확인
     systemctl status nginx

    Nginx 소스 컴파일 버전 업그레이드

소스 컴파일 후, nginx 버전을 업그레이드하고싶다면, 초기 설치 과정과 동일하게 진행하되

컴파일 옵션을 설치 시와 동일하게 설정해야 합니다.

아래는 예시로, nginx-1.25.5 에서 nginx-1.3.0 으로 업그레이드를 진행한다고 가정하겠습니다.

cd /usr/local/src
wget http://nginx.org/download/nginx-1.3.0.tar.gz
tar xvf nginx-1.3.0.tar.gz
cd nginx-1.3.0

# 최소 설치시 컴파일했던 옵션을 동일하게 사용
.**/configure --prefix=/usr/local/nginx --with-stream --with-http_ssl_module --with-http_v2_module --with-http_v3_module
make**
make install

systemctl start nginx

# 버전 확인 시, 업그레이드 된 버전이 출력된다면 성공
/usr/local/nginx/sbin/nginx -v

# 업그레이드 후에는 서비스의 상태를 확인해보는 것이 좋음
systemctl status nginx
728x90

댓글