본문 바로가기

NGINX

NGINX 설치

Package Manager로 설치

linux의 Package Manager로 NGINX를 설치할 경우 NGINX를 쉽게 설치할 수 있지만 설치를 하며 줄 수 있는 옵션에 제한이 있고 추가적인 모듈의 설치가 불가능하기 때문에 추천되지 않는 방법이다.

우분투의 경우에는 apt-get update로 apt-get 패키지 들을 업데이트 한 후 apt-get install nginx로 설치가 가능하다.

NGINX를 소스코드로 설치

NGINX를 설치하기 전에 NGINX는 http://nginx.org/https://www.nginx.com/ 이 있는데 대부분의 documentation은 nginx.org에서 확인이 가능하고 nginx.com은 제품으로서의 소개가 주를 이루고 유료 서비스인 NGINX plus에 대한 documentaion들이 있다.

nginx.org

NGINX의 설치를 위해서는 nginx.org의 우측 네비게이션 바의 download에 들어가야한다.

설치하고 싶은 버전에서 우클릭을 하여 링크 주소를 복사한 후 wget 복사한링크를 하여 다운로드를 한다.

그 후 다운로드 한 컨텐츠를 tar -zxvf 를 이용하여 압축해제한다.

압축을 해제한 폴더를 들어간 후 ./configure을 실행하면 의존성 패키지를 설치라하는 에러가 나온다. 만약 의존성 패키지를 설치하라는 에러 전에 ./configure: error: C compiler cc is not found 라는 에러가 뜨면 apt-get install build-essential로 필요한 개발 툴들을 설치한다.

dong5854@hostname:~$ ls -l
total 1088
drwxr-xr-x 1 dong5854 dong5854    4096 Nov 16 23:44 nginx-1.20.2
-rw-r--r-- 1 dong5854 dong5854 1062124 Nov 16 23:51 nginx-1.20.2.tar.gz
dong5854@hostname:~$ cd nginx-1.20.2/
dong5854@hostname:~/nginx-1.20.2$ ./configure
checking for OS
 + Linux 4.4.0-19041-Microsoft x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)
 checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... found
checking for eventfd() ... found
checking for O_PATH ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for prctl(PR_SET_KEEPCAPS) ... found
checking for capabilities ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for nobody group ... not found
checking for nogroup group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for sched_setaffinity() ... found
checking for SO_SETFIB ... not found
checking for SO_REUSEPORT ... found
checking for SO_ACCEPTFILTER ... not found
checking for SO_BINDANY ... not found
checking for IP_TRANSPARENT ... found
checking for IP_BINDANY ... not found
checking for IP_BIND_ADDRESS_NO_PORT ... found
checking for IP_RECVDSTADDR ... not found
checking for IP_SENDSRCADDR ... not found
checking for IP_PKTINFO ... found
checking for IPV6_RECVPKTINFO ... found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for int size ... 4 bytes
checking for long size ... 8 bytes
checking for long long size ... 8 bytes
checking for void * size ... 8 bytes
checking for uint32_t ... found
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 8 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 8 bytes
checking for AF_INET6 ... found
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for pwritev() ... found
checking for strerrordesc_np() ... not found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for clock_gettime(CLOCK_MONOTONIC) ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for ioctl(FIONREAD) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

apt-get install libpcre3와 apt-get install libpcre3-dev를 통해 패키지들을 받아준 후 다시 configure를 해주면 에러가 사라진 것을 확인할 수 있다.

apt-get install libpcre3
apt-get install libpcre3-dev
dong5854@hostname:~/nginx-1.20.2$ ./configure
checking for OS
 + Linux 4.4.0-19041-Microsoft x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... found
checking for eventfd() ... found
checking for O_PATH ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for prctl(PR_SET_KEEPCAPS) ... found
checking for capabilities ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for nobody group ... not found
checking for nogroup group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for sched_setaffinity() ... found
checking for SO_SETFIB ... not found
checking for SO_REUSEPORT ... found
checking for SO_ACCEPTFILTER ... not found
checking for SO_BINDANY ... not found
checking for IP_TRANSPARENT ... found
checking for IP_BINDANY ... not found
checking for IP_BIND_ADDRESS_NO_PORT ... found
checking for IP_RECVDSTADDR ... not found
checking for IP_SENDSRCADDR ... not found
checking for IP_PKTINFO ... found
checking for IPV6_RECVPKTINFO ... found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for int size ... 4 bytes
checking for long size ... 8 bytes
checking for long long size ... 8 bytes
checking for void * size ... 8 bytes
checking for uint32_t ... found
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 8 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 8 bytes
checking for AF_INET6 ... found
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for pwritev() ... found
checking for strerrordesc_np() ... not found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for clock_gettime(CLOCK_MONOTONIC) ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for ioctl(FIONREAD) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/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

configure의 flag들을 방금은 하나도 주지 않았는데 이런 configure flag는 configure --help으로 확인하거나 NGINX.org의 도큐먼트에서 더 자세히 확인이 가능하다.

$ ./configure --help

기본적인 플래그들을 준 설정은 아래와 같다.

$ ./configure --sbin-path=/usr/bin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-pcre --pid-path=/var/run/nginx.pid --with-http_ssl_module

--sbin-path는 nginx executable의 위치가 있고 /usr/bin/ 는 우분투 executable의 일반적인 경로이다.

--conf-path는 nginx 설정파일의 경로를 나타내는 /etc/nginx 또한 우분투에서 설정 파일의 일반적인 경로로 패키지 매니저로 설치를 할 때에도 nginx의 설정 파일은 여기에 위치하게 된다.

--.error-log-path는 에러로그가 남겨지는 위치를 설정해준다.

--http-log-path는 access로그를 남겨준다.

--with-pcre를 통해 NGINX에게 정규 표현식을 위해 pcre라이브러리를 사용한다는 것을 알린다.

--pid-path를 통해 pid의 경로를 설정해준다.

--with-http_ssl_module를 통해 ssl모듈을 사용할 것을 알린다.

--with-http_ssl_module을 위해서는 sudo apt install libssl-dev를 통해 패키지를 설치해주어야 할 수 있다.

설정을 마친 후 make를 통해 설정을 컴파일을 해준다. 그 후 make install을 이용해 컴파일된 소스를 설치한다.

마지막으로 설정 파일이 제대로 있는 지 /etc/nginx에서 확인한다.

$ make
...생략
$ make install
...생략
$ ls -l /etc/nginx
total 68
-rw-r--r-- 1 root root 1077 Jan 20 01:17 fastcgi.conf
-rw-r--r-- 1 root root 1077 Jan 20 01:17 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 Jan 20 01:17 fastcgi_params
-rw-r--r-- 1 root root 1007 Jan 20 01:17 fastcgi_params.default
-rw-r--r-- 1 root root 2837 Jan 20 01:17 koi-utf
-rw-r--r-- 1 root root 2223 Jan 20 01:17 koi-win
-rw-r--r-- 1 root root 5231 Jan 20 01:17 mime.types
-rw-r--r-- 1 root root 5231 Jan 20 01:17 mime.types.default
-rw-r--r-- 1 root root 2656 Jan 20 01:17 nginx.conf
-rw-r--r-- 1 root root 2656 Jan 20 01:17 nginx.conf.default
-rw-r--r-- 1 root root  636 Jan 20 01:17 scgi_params
-rw-r--r-- 1 root root  636 Jan 20 01:17 scgi_params.default
-rw-r--r-- 1 root root  664 Jan 20 01:17 uwsgi_params
-rw-r--r-- 1 root root  664 Jan 20 01:17 uwsgi_params.default
-rw-r--r-- 1 root root 3610 Jan 20 01:17 win-utf

마지막을 nginx를 실행한 후 프로세스를 확인해본다.

dong5854@hostname:~/nginx-1.20.2$ sudo nginx
dong5854@hostname:~/nginx-1.20.2$ ps aux | grep nginx
root     17003  0.0  0.0  16376   464 ?        Ss   01:27   0:00 nginx:
nobody   17004  0.0  0.0  17056  1656 ?        S    01:27   0:00 nginx:
dong5854 17006  0.0  0.0  16208  1288 tty2     S    01:27   0:00 grep --color=auto nginx

그리고 해당 IP를 들어가보면 NGIX의 기본 페이지를 볼 수 있다.

NGINX의 설치가 끝난 후 NGINX를 systemd 서비스에 추가해야한다.

이를 위해 우선 NGINX를 멈춰주어야한다.

$ nginx -s stop

그리고 https://www.nginx.com/resources/wiki/start/topics/examples/initscripts/ 의 systemd에서 initscript를 복사에 해당 경로에 넣어준다.

하지만 위의 내용과 우리의 실제 설정 경로에 조금 차이가 있기 때문에 PIDFile과 ExecStartPre 와 ExecStart를 조금씩 수정해준다.

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile= /var/run/nginx.pid
ExecStartPre=/usr/bin/nginx -t
ExecStart=/usr/bin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

그 후 systemctl을 이용하여 nginx를 끄고 켤 수 있다.

ex) systemctl start nginx, systemctl stop nginx, systemctl status nginx, systemctl enable nginx...