IT기술/미들웨어
[M/W][NGINX] NGINX 다이나믹 모듈 사용을 위한 컴파일
IT곰곰
2022. 5. 31. 08:41
반응형
안녕하세요.
오늘은 WEB 혹은 Proxy 서버로 많이 사용하고 있는 NGINX의 확장 기능을 설치하기 위한 방법을 알아보도록 하겠습니다.
NGINX의 확장 기능이 필요할 경우, NGINX의 3rd-party 모듈을 추가할 수 있음. 모듈을 추가할 때 NGINX를 다시 컴파일하여 설치해야 합니다.
다이나믹 모듈
NGINX 1.9.11 버전 이후부터 지원. 설정을 통해 필요한 모듈을 동적으로 불러 사용이 가능합니다.
(Apache의 a2enmod, a2dismod 명령어와 동일)
Mac OSX 또는 Linux(CentOS)에서 NGINX 모듈 설치
* 이미 NGINX가 설치된 상황을 전제로 아래 과정 진행
- mac에서 설치: brew install nginx
- CentOS에서 설치: https://nginx.org/en/linux_packages.html#RHEL-CentOS
1. 임의의 위치에 NGINX와 사용할 모듈의 소스파일을 다운로드 할 빈 폴더를 생성
mkdir ~/nginx-temp && cd ~/nginx-temp
2. 설치된 nginx의 버전과 동일한 버전의 NGINX 소스코드 다운로드
nginx -v # nginx 버전 확인
curl -OL https://nginx.org/download/nginx-X.XX.X.tar.gz
tar xvzf nginx-X.XX.X.tar.gz && rm nginx-X.XX.X.tar.gz
* 결과 > mkdir 폴더에 nginx-X.XX.X 폴더 생성
3. 사용하려는 nginx 모듈의 소스코드를 다운로드.
- 모듈 사용 예시 > ngx_http_js_module(NJS) 사용
curl -OL http://hg.nginx.org/njs/archive/tip.tar.gz
tar xvzf tip.tar.gz && rm tip.tar.gz
* 결과 > mkdir 폴더에 njs-XXXXXXXXXXX 폴더 생성
4. NGINX를 컴파일하기 위하여, 현재 설치된 NGINX의 compile flag 확인
nginx -V
* 결과 예시 > 결과에서 configure arguments의 값이 compile flag
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --with-ld-opt=' -Wl,-E'
5. (생략 가능) C, C++ 컴파일러 및 NGINX 의존성 패키지 설치
sudo yum install gcc
sudo yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel
sudo yum install -y libxml2 libxml2-devel libxslt libxslt-devel php-gd gd gd-devel perl-ExtUtils-Embed mod-geoip GeoIP GeoIP-devel GeoIP-data gperftools
6. ~/nginx-temp/nginx-X.XX.X 폴더에서 configure 명령 실행
sudo ./configure
- 4.의 compilie flag를 복사하고, 맨 마지막에 --add-dynamic-module 을 추가하여 명령어 실행.
- 모듈 폴더에서 config 파일이 있는 경로를 입력
--add-dynamic-module=../module-name-XXXXXXXXXXXX
sudo ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --with-ld-opt=' -Wl,-E' --add-dynamic-module=../njs-XXXXXXXXXXXX/nginx
* 결과 > nginx-X.XX.X 폴더 내 Makefile, objs 폴더 생성
.
├── CHANGES
├── CHANGES.ru
├── LICENSE
├── Makefile
├── README
├── auto
├── build
├── conf
├── configure
├── contrib
├── html
├── man
├── objs
└── src
7. NGINX 컴파일
sudo make modules
- 컴파일이 완료되면 /objs 폴더 아래로 .so 파일이 생성됨
8. 컴파일된 NGINX 모듈 설치
- 4.의 compilie flag에서 출력된 NGINX의 --prefix 경로의 하위로 modules 폴더를 생성
- 7.에서 생성된 .so 파일을 복사해 넣음
sudo mkdir /usr/share/nginx/modules
cp objs/ngx_*.so /usr/share/nginx/modules
9. nginx.conf 파일 상단에 load_module 명령 추가하여 다이나믹 모듈 로드
load_module modules/ngx_http_js.module.so;
반응형