Backend/Image Server

[2] Nginx를 이용한 이미지 서버 ⎻ Nginx(w/ upload module), fcgiwrap 설치

eraser 2022. 6. 6. 13:46
반응형

 개발을 위해 이미지 서버 구축에 필요한 스택을 설치한다. 개발 환경은 Ubuntu 20.04.이다.

 


 

# Nginx 설치

 

 Nginx 소스 코드로부터 nginx-upload-module을 함께 컴파일해서 설치한다. Nginx는 C로 개발되어 있으므로, gcc 등 C 컴파일 관련 라이브러리가 설치되어 있어야 한다.

 

 

 

## Nginx 의존 라이브러리 설치

 

 Nginx를 실행하기 위해서는 기본적으로 pcre, openssl, zlib 등의 라이브러리가 필요하다. 패키지 매니저를 통해 설치해도 되지만(추후 Dockerfile 작성 시에는 이 방법을 사용하였다), 애초에 컴파일해서 설치하는 김에 의존 라이브러리도 컴파일하는 방식으로 설치해보기로 하였다.

 Nginx 설치 시 빌드 경로 설정을 쉽게 하기 위하여, /usr/src/temp 경로 밑에서 진행했다.

 

 

1. pcre

 

 perl 정규식 관련 라이브러리라고 한다. Nginx에서 정규식이 필요한 부분(url 관련 설정?)에서 사용하는 듯하다. 소스코드를 다운받고 실행 파일을 만들어 설치하면 되는데, Nginx 공식 문서에 나와 있는 것과는 pcre 소스코드 위치가 달라진 듯하다.

$ cd /usr/src/temp
$ wget https://sourceforge.net/projects/pcre/files/pcre/pcre-8.44.tar.gz
$ tar -zxf pcre-8.44.tar.gz
$ cd pcre-8.44
$ ./configure
$ make
$ sudo make install

 

 

2. zlib

 

 C로 작성된 데이터 압축 관련 라이브러리라고 한다. Nginx에 gzip 등 컨텐츠 압축 설정을 할 수 있다고 하는데, 이 때 사용하는 듯하다. Nginx 공식 문서를 따라 진행하면 된다.

$ cd /usr/src/temp
$ wget http://zlib.net/zlib-1.2.11.tar.gz
$ tar -zxf zlib-1.2.11.tar.gz
$ cd zlib-1.2.11/
$ ./configure
$ make
$ sudo make install

 

 

3. openssl

 

 TLS, SSL 등 네트워크 데이터 통신에 쓰이는 프로토콜의 오픈소스 구현 라이브러리라고 한다. Nginx에서 HTTPS 프로토콜을 사용하기 위해 필요하다고 한다.

$ cd /usr/src/temp
$ wget http://www.openssl.org/source/openssl-1.1.1g.tar.gz
$ tar -zxf openssl-1.1.1g.tar.gz
$ cd openssl-1.1.1.g
$ ./Configure linux-x86_64 --prefix=/usr # /usr 디렉토리 밑에 openssl 헤더 파일 위치
$ make
$ sudo make install

 

 

## upload module 소스 코드 다운

 

 Nginx-upload-module 소스 코드를 받은 후, 압축 해제한다. 아래 명령어를 따를 경우, 압축 파일은 2.3.0.tar.gz라는 이름으로 다운로드되고, 해당 파일을 tar 명령어를 통해 압축 해제하면 nginx-upload-module-2.3.0 이름의 폴더가 생성된다.

 

$ cd /usr/src/temp
$ wget ttps://github.com/vkholodkov/nginx-upload-module/archive/2.3.0.tar.gz
$ tar -xzvf ${pwd}/2.3.0.tar.gz # 압축 파일은 버전명만으로 다운로드됨

 

 

 

## Nginx 설치

 

 Nginx 1.20.2. 버전의 소스 코드를 다운로드한 후, 의존 라이브러리 및 upload module의 경로와 함께 빌드 옵션을 설정한다. 의존 라이브러리 및 upload module 경로가 핵심이지만, 다른 빌드 옵션을 설정할 수도 있다(Nginx path 관련 설정, gcc 컴파일러 옵션 등을 모두 설정할 수 있다).

 아래 설정을 따르면, Nginx를 pcre, zlib, ssl, upload module과 함께 설치하게 되며, Nginx 에러 로그 출력 시 debug 옵션을 설정할 수 있게 된다. 또한, Nginx 프로그램 소유자 및 소유 그룹이 모두 nginx로 설정된다.

$ cd /usr/src/temp
$ wget https://nginx.org/download/nginx-1.20.2.tar.gz
$ tar -zxf nginx-1.20.2.tar.gz
$ cd nginx-1.20.2
$ .configure \
--with-pcre=../pcre-8.44 \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.1g \
--add-module=../nginx-upload-module-2.3.0 \ # upload module 추가
--with-debug
--user=nginx
--group=nginx

 

 빌드 옵션 설정까지 완료되면, 설치하면 된다. 설치 과정 중간에 addon으로 upload 모듈이 들어가는 것이 보이면 된다.

$ make
$ make install

>> (...)
>> objs/src/http/modules/ngx_http_upstream_random_module.o \
>> objs/src/http/modules/ngx_http_upstream_keepalive_module.o \
>> objs/src/http/modules/ngx_http_upstream_zone_module.o \
>> .objs/addon/nginx-upload-module-2.3.0/ngx_http_upload_module.o \
>> objs/ngx_modules.o \
>> -ldl -lpthread -lcrypt ../tmp/pcre-8.44/.libs/libpcre.a -lssl -lcrypto -ldl -lpthread ../tmp/zlib-1.2.11/libz.a \
>> (...)

 

 설치 후 nginx 명령어를 통해 Nginx를 실행한다. 오류 없이 실행되고, 브라우저에서 http://localhost로 접속했을 때 다음과 같은 화면이 나오면 된다.

$ nginx

ㅎㅇㅎㅇ

 

 

 

## nginx 설정 파일 위치

 

설치 후 /etc/nginx 디렉토리에 가면 nginx 설정 관련 파일들이 모여 있는 것을 확인할 수 있다. 앞으로 이미지 서버를 개발하며 필요한 nginx 관련 설정 파일들은 이 아래에 작성하게 될 것이다.

  • nginx.conf: nginx 기본 설정 파일. 로그, nginx 프로세스 관련 설정 등을 담게 될 것
  • conf.d/: include할 nginx config 파일 모음. 소스 코드(https://projectlog-eraser.tistory.com/65) 상 config/ 안에 들어갈 설정 파일들이 conf.d/ 아래 위치하게 된다
/etc/nginx# ls -al
total 88
drwxr-xr-x 1 root  root  4096 Apr 21 17:22 .
drwxr-xr-x 1 root  root  4096 Apr 26 19:06 ..
drwxr-xr-x 1 nginx nginx 4096 Apr 26 19:06 conf.d
-rw-r--r-- 1 root  root  1077 Apr 21 17:22 fastcgi.conf
-rw-r--r-- 1 root  root  1077 Apr 21 17:22 fastcgi.conf.default
-rw-r--r-- 1 root  root  1007 Apr 21 17:22 fastcgi_params
-rw-r--r-- 1 root  root  1007 Apr 21 17:22 fastcgi_params.default
-rw-r--r-- 1 root  root  2837 Apr 21 17:22 koi-utf
-rw-r--r-- 1 root  root  2223 Apr 21 17:22 koi-win
-rw-r--r-- 1 root  root  5231 Apr 21 17:22 mime.types
-rw-r--r-- 1 root  root  5231 Apr 21 17:22 mime.types.default
-rw-rw-r-- 1  1000  1000  547 Apr 26 19:55 nginx.conf
-rw-r--r-- 1 root  root  2656 Apr 21 17:22 nginx.conf.default
-rw-r--r-- 1 root  root   636 Apr 21 17:22 scgi_params
-rw-r--r-- 1 root  root   636 Apr 21 17:22 scgi_params.default
-rw-r--r-- 1 root  root   664 Apr 21 17:22 uwsgi_params
-rw-r--r-- 1 root  root   664 Apr 21 17:22 uwsgi_params.default
-rw-r--r-- 1 root  root  3610 Apr 21 17:22 win-utf

 

 


 

# fcgiwrap 설치

 

 fcgiwrap은 패키지 매니저를 통해 설치하면 된다.

 

$ apt-get update
$ apt-get install fcgiwrap

 

 설치하면 fcgiwrap 프로세스를 spawn할 수 있는 spawn-fcgi 프로그램이 같이 설치되는데, 이미지 서버에서 이 프로세스를 이용할 수 있도록 fcgiwrap 프로세스를 아래 명령어를 이용해 spawn한다. 

spawn-fcgi \
-s /var/run/fcgiwrap.socket \ # fcgiwrap unix domain socket
-a "0.0.0.0" \
-u 102 -g 102 -U 102 -G 102 \ # fcgiwrap user, group 설정
-M 766 /usr/sbin/fcgiwrap

 fcgiwrap의 기본 user, group은 www-data인데, 나중에 nginx와 user, group이 달라 에러가 발생하는 것을 막기 위해(https://sirzzang.github.io/dev/Dev-fcgiwrap-python/#%EA%B6%8C%ED%95%9C-%EA%B4%80%EB%A0%A8-%EB%AC%B8%EC%A0%9C) nginx user id, nginx group id를 이용해 fcgiwrap의 user, group을 변경해 준다. nginx user, nginx group의 id가 무엇인지 /etc/passwd, /etc/group에서 확인한 후 진행한다.

 기타 옵션은 spawn-fcgi documentation에서 확인할 수 있다.

반응형