How to Install and Configure Nginx from Source on Ubuntu 18.04
To Install and Configure Nginx from Source on Linux
Nginx is an open source web server that is similar to Apache, but very lightweight and also nginx acts as both web server and reverse proxy server. Nginx is packed with several features and some of them are listed below.
- It serves static and index files
- Reverse proxy with caching
- Supports SSL
- Supports rewrite module
- Supports gzip, XSLT, SSI and image resizing filters
Installing Nginx
Download Nginx' s latest version from the official site using the wget command from the terminal.
root@linuxhelp:~# wget http://nginx.org/download/nginx-1.12.2.tar.gz
--2018-03-28 19:23:57-- http://nginx.org/download/nginx-1.12.2.tar.gz
Resolving nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ...
Connecting to nginx.org (nginx.org)|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 981687 (959K) [application/octet-stream]
Saving to: ‘ nginx-1.12.2.tar.gz’
nginx-1.12.2.tar.gz 100%[==========================================================> ] 958.68K --.-KB/s in 0.1s
2018-03-28 19:24:06 (8.96 MB/s) - ‘ nginx-1.12.2.tar.gz’ saved [981687/981687]
After the download completes, extract the package using the following command.
root@linuxhelp:~# tar xvfz nginx-1.12.2.tar.gz
nginx-1.12.2/
nginx-1.12.2/auto/
nginx-1.12.2/conf/
nginx-1.12.2/contrib/
nginx-1.12.2/src/
nginx-1.12.2/configure
nginx-1.12.2/LICENSE
nginx-1.12.2/README
nginx-1.12.2/html/
nginx-1.12.2/man/
nginx-1.12.2/CHANGES.ru
nginx-1.12.2/CHANGES
nginx-1.12.2/man/nginx.8
.
.
nginx-1.12.2/auto/lib/geoip/conf
nginx-1.12.2/auto/cc/clang
nginx-1.12.2/auto/cc/acc
nginx-1.12.2/auto/cc/bcc
nginx-1.12.2/auto/cc/ccc
nginx-1.12.2/auto/cc/conf
nginx-1.12.2/auto/cc/gcc
nginx-1.12.2/auto/cc/icc
nginx-1.12.2/auto/cc/msvc
nginx-1.12.2/auto/cc/name
nginx-1.12.2/auto/cc/owc
nginx-1.12.2/auto/cc/sunc
Navigate to the extracted directory and list all the files in it.
root@linuxhelp:~# cd nginx-1.12.2//nginx
root@linuxhelp:~/nginx-1.12.2# ls &ndash l
total 724
drwxr-xr-x 6 1001 1001 4096 Mar 28 19:24 auto
-rw-r--r-- 1 1001 1001 278202 Oct 17 18:46 CHANGES
-rw-r--r-- 1 1001 1001 423948 Oct 17 18:46 CHANGES.ru
drwxr-xr-x 2 1001 1001 4096 Mar 28 19:24 conf
-rwxr-xr-x 1 1001 1001 2481 Oct 17 18:46 configure
drwxr-xr-x 4 1001 1001 4096 Mar 28 19:24 contrib
drwxr-xr-x 2 1001 1001 4096 Mar 28 19:24 html
-rw-r--r-- 1 1001 1001 1397 Oct 17 18:46 LICENSE
drwxr-xr-x 2 1001 1001 4096 Mar 28 19:24 man
-rw-r--r-- 1 1001 1001 49 Oct 17 18:46 README
drwxr-xr-x 9 1001 1001 4096 Mar 28 19:24 src
The directory contains the configuration file and you have to run the configuration file using the below mentioned command.
root@linuxhelp:~/nginx-1.12.2# ./configure
checking for OS
+ Linux 4.4.0-21-generic x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2)
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
.
.
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 usage temporary files: " uwsgi_temp"
nginx http scgi temporary files: " scgi_temp"
After the configuration, we have to make install using the below command.
root@linuxhelp:~/nginx-1.12.2# make
make -f objs/Makefile
make[1]: Entering directory ' /home/user/nginx-1.12.2'
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
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_palloc.o
src/core/ngx_palloc.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/co
.
.
objs/ngx_modules.o
-ldl -lpthread -lcrypt -lpcre -lz
-Wl,-E
sed -e " s|%%PREFIX%%|/usr/local/nginx|"
-e " s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|"
-e " s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|"
-e " s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|"
< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory ' /home/user/nginx-1.12.2'
After making configuration enter the following command to install the nginx.
root@linuxhelp:~/nginx-1.12.2# make install
make -f objs/Makefile install
make[1]: Entering directory ' /home/user/nginx-1.12.2'
test -d ' /usr/local/nginx' || mkdir -p ' /usr/local/nginx'
test -d ' /usr/local/nginx/sbin'
|| mkdir -p ' /usr/local/nginx/sbin'
test ! -f ' /usr/local/nginx/sbin/nginx'
|| mv ' /usr/local/nginx/sbin/nginx'
' /usr/local/nginx/sbin/nginx.old'
cp objs/nginx ' /usr/local/nginx/sbin/nginx'
test -d ' /usr/local/nginx/conf'
|| mkdir -p ' /usr/local/nginx/conf'
.
.
test -d ' /usr/local/nginx/logs'
|| mkdir -p ' /usr/local/nginx/logs'
test -d ' /usr/local/nginx/html'
|| cp -R html ' /usr/local/nginx'
test -d ' /usr/local/nginx/logs'
|| mkdir -p ' /usr/local/nginx/logs'
make[1]: Leaving directory ' /home/user/nginx-1.12.2'
By default, it runs on port 80. if u have an Apache web server already running on your server you should change the port number so we have to Change the listen port.
root@linuxhelp:~/nginx-1.12.2# vi /usr/local/nginx/conf/nginx.conf
server {
listen 8081
server_name localhost
Now navigate to the /usr/local/nginx/sbin/ to start the nginx.
root@linuxhelp:~/nginx-1.12.2# cd /usr/local/nginx/sbin/ root@linuxhelp:/usr/local/nginx/sbin# ./nginx
You can verify the status by using the following command
root@linuxhelp:/usr/local/nginx/sbin# ps -ef | grep -i Nginx
root 31605 1350 0 19:27 ? 00:00:00 Nginx: master process ./nginx
nobody 31606 31605 0 19:27 ? 00:00:00 nginx: worker process
root 31608 7285 0 19:28 pts/4 00:00:00 grep --color=auto -i nginx
You can verify it by accessing from the browser by using the following URL: http://ipaddress:8081.

If you want to stop the nginx server you can do that by running the following command.
root@linuxhelp:/usr/local/nginx/sbin# ./nginx -s stop
Check the version of the nginx server by making use of the following command.
root@linuxhelp:/usr/local/nginx/sbin# ./nginx -v
nginx version: nginx/1.12.2
To check the log making to the below-mentioned directory.
root@linuxhelp:/usr/local/nginx/sbin# ls /usr/local/nginx/logs/
access.log error.log
With this, the method to Install and Configure Nginx from Source on Linux comes to an end.
Comments ( 0 )
No comments available