Menu

메모용 개발 블로그

전체보기 > Nginx >

Nginx 1.19.0 컴파일 방식으로 설치

2022-08-09 00:59:25

서론

개인적으로 컴파일 방식으로 무언가를 설치하는 것을 선호하지 않는다.

패키지매니저로 편리하게 설치하고 관리하는 것을 좋아하는데. 가끔은 그렇지 못한경우가 있는데. 하필 지금이다.

WebDav 구축 중 원하는 확장 모듈이 full버전에서 설치된걸로 보이는데... 제대로 동작하지도 않고 검색해도 결과가 잘 나오지도 않으며 대부분 컴파일 설치로 진행하는 모양이였다.

설치 준비

컴파일 방식 설치이므로 당연하게도 컴파일러가 필요하다.

컴파일러까지 컴파일해야하면 설치할 방도가 없으므로 이미 빌드된 패키지를 각자 배포판에 맞는 패키지 매니저를 통하여 편리하게 설치한다.

sudo apt update
sudo apt install gcc g++
sudo apt install libxslt-dev

설치

1. 다운로드

적당한 경로를 생성하고 이동 한 후 작업한다.

다운로드 후 압축을 푸는 것의 연속이다.

1. Nginx

wget wget https://nginx.org/download/nginx-1.19.0.tar.gz
tar zxvf nginx-1.19.0.tar.gz

2. OpenSSL

wget http://www.openssl.org/source/openssl-1.1.1g.tar.gz
tar -zxvf openssl-1.1.1g.tar.gz

3. zlib

wget https://sourceforge.net/projects/libpng/files/zlib/1.2.11/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz

4. pcre

wget https://sourceforge.net/projects/pcre/files/pcre/8.44/pcre-8.44.tar.gz
tar -zxvf pcre-8.44.tar.gz

5. nginx-dav-ext-module

여기는 필수는 아니다. webdav 관련한 외부 모듈이다.

git clone https://github.com/arut/nginx-dav-ext-module.git --depth=1

2. 빌드

1. pcre

cd pcre-8.44
./configure
make
sudo make install

2. OpenSSL

cd openssl-1.1.1g
./Configure linux-x86_64 --prefix=/usr
make
sudo make install

OpenSSL은 Configure 명령 첫 인수에 아키텍처 명시가 있다.

아래 명령어로 자신의 환경에 맞는 것으로 선택하자.

./Configure LIST

3. zlib

cd zlib-1.2.11
./configure
make
sudo make install

3. Nginx

편리하게 nginx-extra니 nginx-full이니 하는 친절한 통합 패키지가 그리워지는 첫번째 순간이다.

우선 자세한 설명에 대한 공식 문서이다.

http://nginx.org/en/docs/configure.html

필요한 외부 의존라이브러리 경로나 같이 빌드할 내부 모듈, 외부 모듈 등을 설정하여 컴파일할 준비를 해줘야한다.

sudo ./configure \
--prefix=/opt/nginx \
--with-compat \
--with-file-aio \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gunzip_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_dav_module \
--with-http_mp4_module \
--with-http_slice_module \
--with-stream \
--with-stream_realip_module \
--with-threads \
--with-http_v2_module \
--with-pcre=../pcre-8.44 \
--with-zlib=../zlib-1.2.11 \
--add-module=../nginx-dav-ext-module \
--user=www-data \
--group=www-data

--with-file-aio : 비동기 파일 IO 관련을 사용할 수 있음.

--with-http_ssl_module : https프로토콜 지원 모듈

--with-http_realip_module : 리버스 프록시 등을 사용할 때. 클라이언트의 실제 아이피로 쉽게 대체 가능하는데 사용하는 모듈

--with-http_gunzip_module : gzip 관련

--with-http_gunzip_module : gzip 관련

--with-http_dav_module : webdav 관련

--with-http_mp4_module : mp4 스트리밍 기능

--with-http_slice_module : 응답분할관련 기능

--with-stream : TCP/UDP를 프록시 가능한 스트림 기능

--with-stream_realip_module : stream realip 관련 기능

--with-threads : 스레드 관련

--with-http_v2_module : http2 지원

--add-module=nginx-dav-ext-module : webdav 관련 확장 모듈

--with-pcre : pcre 모듈 위치 지정

--with-openssl : openssl 모듈 위치 지정(이 글 대로 하면 알아서 찾으니 빼도 무방하다. 독특한 곳에 설치했다면 명시할 것.)

--user : 서버 실행 유저 지정

--group : 서버 실행 그룹 지정

유저와 그룹의 경우 해당 글에서는 이미 nginx를 패키지로 운영하다가 전환하는 것이라 기존에 기본 계정을 지정했지만 그렇지 않다면 적절한 계정을 만들어서 지정해도 된다.

대충 실행하면

checking for getaddrinfo() ... found
configuring additional modules
adding module in ../nginx-dav-ext-module
 + ngx_http_dav_ext_module was configured
checking for libxslt ... found
checking for libexslt ... found
creating objs/Makefile

Configuration summary
  + using threads
  + using PCRE2 library: ../pcre2
  + using OpenSSL library: ../openssl
  + using zlib library: ../zlib

  nginx path prefix: "/opt/nginx"
  nginx binary file: "/opt/nginx/sbin/nginx"
  nginx modules path: "/opt/nginx/modules"
  nginx configuration prefix: "/opt/nginx/conf"
  nginx configuration file: "/opt/nginx/conf/nginx.conf"
  nginx pid file: "/opt/nginx/logs/nginx.pid"
  nginx error log file: "/opt/nginx/logs/error.log"
  nginx http access log file: "/opt/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

이런 로그와 함께 마무리된다.

이제 컴파일 이후 설치

sudo make
sudo make install

실행, 기타 설정

우선 항상 nginx를 입력해서 실행할 수 없으므로 시스템 데몬으로 등록하도록 한다.

기존 패키지에서 작성된 스크립트를 조금 손봐서 그대로 사용하도록 하였다.

sudo vim /lib/systemd/system/nginx.service
# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/opt/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/opt/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

ExecStartPre, ExecStart, ExecReload 항목에 대해서 경로를 변경해주었다.

이후 변경된 사항에 대하여 적용

sudo systemctl enable nginx

그리고 기존 nginx가 설치되었다면 이를 제거하고 새로 설치한 nginx를 PATH에 등록해야 한다.

여러 방법이 있지만 그냥 기존 위치에 삭제 해주고 그대로 링크를 만들었다.

where nginx
cd /usr/sbin
sudo rm -rf ./nginx
sudo ln -s /opt/nginx/sbin/nginx nginx

끝.