NGINX is an open-source web server software designed to use as a web server, reverse proxy, HTTP cache, and load balancer. NGINX was created by Igor Sysoev to solve the C10k Problem. Below are the latest statistics of the market place of Apache and NGINX web servers on the internet by netcraft.com.
This solution allows the webserver to handle 10.000 concurrent connections with maximum efficiency. Rather than creating new processes as apache does, NGINX uses event-driven architecture. At the top, the master process spawns worker processes, each worker process can handle thousands of HTTP request simultaneously. NGINX is really good at serving static content when comparing with Apache. Reverse proxy and load balancer servers are common usages of NGINX.
There are several types of installation;
- Docker image
- Nginx repository
- Operating Systems repositories
- Already compiled package(.rpm .dep .exe)
- Compiling Binary
At this post, I will explain how to compile and install NGINX web server from source code. It is recommended to install NGINX web server from source code because of some benefits;
- Customize NGINX installation
- Remove unnecessary modules and methods
- Run NGINX with the specific application user
- Reliability
Step 1: Installing NGINX dependencies
- PCRE – supports regular expressions. Required by the NGINX Core and Rewrite modules.
- zlib – Supports header compression. Required by the NGINX Gzip module.
- OpenSSL – Supports the HTTPS protocol. Required by the NGINX SSL module and others.
- GCC - Compile binaries
For well-known Linux distribution, you only need to install "openssl-dev" package to the server that you will use for compiling NGINX web server. Openssl-dev package contains all dependencies added above. Also, you have another option to compile the dependencies package from the tar file. Check NGINX documentation for more details.
CentOS, RHEL, and Fedora
#yum install openssl-devel gcc #rpm -qa|egrep 'openssl-devel|pcre|zlib' pcre-devel-8.32-17.el7.x86_64 zlib-devel-1.2.7-18.el7.x86_64 zlib-1.2.7-18.el7.x86_64 openssl-devel-1.0.2k-19.el7.x86_64 pcre-8.32-17.el7.x86_64
Debian Base Operating Systems
#apt-get install libssl liblz-dev libpcre3 or #apt get install openssl-devel
Step 2: Download Binary
You need to download the stable source code from NGINX web site.
#mkdir -p /web/nginx #mkdir /web/nginx/modules #mkdir /web/nginx/run #cd /web/nginx/ #wget https://nginx.org/download/nginx-1.17.6.tar.gz #tar -xzvf nginx-1.17.6.tar.gz #ls -lrt total 1016 drwxr-xr-x 8 1001 1001 147 Nov 19 17:19 nginx-1.17.6 -rw-r--r-- 1 appuser appgroup 1037527 Nov 19 17:25 nginx-1.17.6.tar.gz # rm -rf nginx-1.17.6.tar.gz #mkdir binaries #mv nginx-1.17.6/* binaries/ #rm -rf nginx-1.17.6/ # pwd /web/nginx/binaries # ls -lrt total 764 -rw-r--r-- 1 1001 1001 49 Nov 19 17:18 README -rw-r--r-- 1 1001 1001 1397 Nov 19 17:18 LICENSE -rwxr-xr-x 1 1001 1001 2502 Nov 19 17:18 configure -rw-r--r-- 1 1001 1001 458631 Nov 19 17:19 CHANGES.ru -rw-r--r-- 1 1001 1001 300566 Nov 19 17:19 CHANGES drwxr-xr-x 2 1001 1001 20 Jan 16 15:30 man drwxr-xr-x 2 1001 1001 38 Jan 16 15:30 html drwxr-xr-x 9 1001 1001 84 Jan 16 15:30 src drwxr-xr-x 4 1001 1001 68 Jan 16 15:30 contrib drwxr-xr-x 2 1001 1001 4096 Jan 16 15:30 conf drwxr-xr-x 6 1001 1001 4096 Jan 16 15:30 auto
Step 3: Configure Options
Configure options can be specified with the script "./configure" under binaries directory. You can check all options with "./configure --help" command.
#./configure --help
--help print this message
--prefix=PATH set installation prefix
--sbin-path=PATH set nginx binary pathname
--modules-path=PATH set modules path
--conf-path=PATH set nginx.conf pathname
--error-log-path=PATH set error log pathname
--pid-path=PATH set nginx.pid pathname
--lock-path=PATH set nginx.lock pathname
Options | Describtion |
--prefix | set installation prefix |
--modules-path | set modules path |
--with-http_ssl_module | enable ngx_http_ssl_module |
--without-http_fastcgi_module | disable ngx_http_fastcgi_module |
--without-http_uwsgi_module | disable ngx_http_uwsgi_module |
--without-http_grpc_module | disable ngx_http_grpc_module |
--without-http_scgi_module | disable ngx_http_scgi_module |
--without-mail_imap_module | disable ngx_mail_imap_module |
--without-mail_pop3_module | disable ngx_mail_pop3_module |
# ./configure --prefix=/web/nginx --modules-path=/web/nginx/modules --with-http_ssl_module --without-http_fastcgi_module --without-http_uwsgi_module --without-http_grpc_module --without-http_scgi_module --without-mail_imap_module --without-mail_pop3_module checking for OS + Linux 3.10.0-327.28.3.el7.x86_64 x86_64 checking for C compiler ... found + using GNU C compiler + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 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 ... not 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 ... 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 ... not 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 eventfd() ... 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 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 OpenSSL library ... found checking for zlib library ... found creating objs/Makefile Configuration summary + using system PCRE library + using system OpenSSL library + using system zlib library nginx path prefix: "/web/nginx" nginx binary file: "/web/nginx/sbin/nginx" nginx modules path: "/web/nginx/modules" nginx configuration prefix: "/web/nginx/conf" nginx configuration file: "/web/nginx/conf/nginx.conf" nginx pid file: "/web/nginx/logs/nginx.pid" nginx error log file: "/web/nginx/logs/error.log" nginx http access log file: "/web/logs" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp"
Step 4: Make and Make Install
# make make -f objs/Makefile make[1]: Entering directory `/web/nginx/binaries' cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/nginx.o \ src/core/nginx.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/ngx_log.o \ src/core/ngx_log.c
# make install make -f objs/Makefile install make[1]: Entering directory `/web/nginx/binaries' test -d '/web/nginx' || mkdir -p '/web/nginx' test -d '/web/nginx/sbin' \ || mkdir -p '/web/nginx/sbin' test ! -f '/web/nginx/sbin/nginx' \ || mv '/web/nginx/sbin/nginx' \ '/web/nginx/sbin/nginx.old' cp objs/nginx '/web/nginx/sbin/nginx' test -d '/web/nginx/conf' \ || mkdir -p '/web/nginx/conf' cp conf/koi-win '/web/nginx/conf' cp conf/koi-utf '/web/nginx/conf' cp conf/win-utf '/web/nginx/conf' test -f '/web/nginx/conf/mime.types' \ || cp conf/mime.types '/web/nginx/conf' cp conf/mime.types '/web/nginx/conf/mime.types.default' test -f '/web/nginx/conf/fastcgi_params' \ || cp conf/fastcgi_params '/web/nginx/conf' cp conf/fastcgi_params \ '/web/nginx/conf/fastcgi_params.default' test -f '/web/nginx/conf/fastcgi.conf' \ || cp conf/fastcgi.conf '/web/nginx/conf' cp conf/fastcgi.conf '/web/nginx/conf/fastcgi.conf.default' test -f '/web/nginx/conf/uwsgi_params' \ || cp conf/uwsgi_params '/web/nginx/conf' cp conf/uwsgi_params \ '/web/nginx/conf/uwsgi_params.default' test -f '/web/nginx/conf/scgi_params' \ || cp conf/scgi_params '/web/nginx/conf' cp conf/scgi_params \ '/web/nginx/conf/scgi_params.default' test -f '/web/nginx/conf/nginx.conf' \ || cp conf/nginx.conf '/web/nginx/conf/nginx.conf' cp conf/nginx.conf '/web/nginx/conf/nginx.conf.default' test -d '/web/nginx/logs' \ || mkdir -p '/web/nginx/logs' test -d '/web' \ || mkdir -p '/web' test -d '/web/nginx/html' \ || cp -R html '/web/nginx' test -d '/web/nginx/logs' \ || mkdir -p '/web/nginx/logs' make[1]: Leaving directory `/web/nginx/binaries'
Step 5: Configure NGINX and run
# pwd /web/nginx #rm -rf binaries/ #vi /web/nginx/conf/nginx.conf # - define application user user appuser; # - define worker process worker_processes 5; # - define pid file that systemd checks for if application is running. pid run/nginx.pid; #/web/nginx/sbin/nginx -c /web/nginx/conf/nginx.conf
Step 5: Configure NGINX service for RHEL-CentOS
# cat /usr/lib/systemd/system/nginx.service [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] User=appuser Group=appgroup Type=forking PIDFile=/web/nginx/run/nginx.pid ExecStart=/web/nginx/sbin/nginx -c /web/web/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID [Install] WantedBy=multi-user.target
#systemctl enable nginx.service #systemctl start nginx