How to install Nginx from the source code on Ubuntu 18.04 and Ubuntu 19.04?

1
596
Nginx
Nginx

There are many system administrators who prefer to install programs from their source code. Although it may seem strange to you, it is a very good practice, so you have total control of the program. That’s why today I’ll show you how to install Nginx from the source code on Ubuntu 18.04.

A web server can be cataloged as an essential element of the network. Therefore, you must have a program that is the best and that is Nginx. Nginx is a lightweight, open source and widely popular web server that bases its success on performance when processing websites. Along with Apache web server are the two references within Linux for that purpose.

So, let’s start.

1. Upgrade the system

The first step in this process is to keep your system up to date. Therefore, connect to your server and run.

:~$ sudo apt update && sudo apt upgrade
1.- Upgrade the system
1.- Upgrade the system

With this, you will have a more stable system, robust and ready to start the installation.

2. Install required packages

To compile the Nginx source code, it is necessary to install some previous packages.

:~$ sudo apt install build-essential libpcre3-dev zlib1g-dev libssl-dev libatomic-ops-dev libxml2-dev libxslt1-dev libgeoip1 libgeoip-dev libgd-dev google-perftools libgoogle-perftools-dev libperl-dev
2.- Install required packages for the installation
2.- Install required packages for the installation

So, as you can see in the image, many packages related to compilation and package construction are installed.

3. Download and install Nginx

Now proceed to download the latest stable version of Ngnix from your website. Use the following command to do it

:~$ wget -c http://nginx.org/download/nginx-1.15.8.tar.gz
3.- Downloading Nginx source code
3.- Downloading Nginx source code

Note: At the time of writing this post the latest stable version of Ngnix is 1.15.8. First, find out which version you want to download and modify the previous command.

Now, decompress the file.

:~$ tar -xvzf nginx-1.15.8.tar.gz
4.- Decompressing the file
4.- Decompressing the file

Now, create a new user for Nginx.

:~$ sudo useradd -s /sbin/nologin nginx

Next, enter the created directory and start the configuration.

:~$ cd nginx-1.15.8/
:~$ sudo ./configure --user=nginx --group=nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_xslt_module=dynamic --with-http_image_filter_module --with-http_image_filter_module=dynamic --with-http_geoip_module --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_auth_request_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 --with-http_perl_module=dynamic --with-mail --with-mail=dynamic --with-mail_ssl_module --with-stream --with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module --with-stream_geoip_module=dynamic --with-stream_ssl_preread_module --with-google_perftools_module --with-cpp_test_module --with-compat --with-pcre --with-pcre-jit  --with-zlib-asm=CPU --with-libatomic --with-debug --with-ld-opt="-Wl,-E"
5.- Configuring the package
5.- Configuring the package

As you can see, the command is quite extensive because it requires that you indicate which modules or options will be enabled.

When you finish the configuration, you should see something like this.

6.- End of the configure command
6.- End of the configure command

So, install it.

:~$ sudo make && sudo make install
7.- Install Ngnix from the source code
7.- Install Ngnix from the source code

And that’s it.

4. Create a systemd service file for Nginx

To better manage Ngnix, create a file to make it a systemd service.

:~$ sudo nano /lib/systemd/system/nginx.service

And add the following:

[Unit]
Description=The Nginx 1.15.8 service
After=syslog.target network.target remote-fs.target nss-lookup.target

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

[Install]
WantedBy=multi-user.target
8.- Creating the systemd file for Ngnix
8.- Creating the systemd file for Ngnix

For these changes to take effect, it is necessary to reload the systemd configuration.

:~$ sudo systemctl daemon-reload

Now, start Ngnix.

:~$ sudo systemctl start nginx
9.- Start Nginx service
9.- Start Nginx service

Finally, open your web browser and go to http://IP_SERVER. You should see this.

10. Nginx welcome page
10. Nginx welcome page

And that’s it.

Conclusion

Nginx is a great application to set up a web server. The installation from the source code brings the advantage of being lighter. This is useful if you want to have total control over its operation.

Please share this article with your friends.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here