By Arman Ali
NGINX was created in 2004 by Russian developer Igor Sysoev as he was frustrated with Apache and wanted to build a replacement capable of handling 10,000 concurrent connections with a focus on performance, high concurrency, and low memory usages.
Today NGINX servers the majority of the world's top 1000 websites and while this growth is largely due to, it's also because NGINX is relatively easy to get started with.
There are two methods of installing NGINX on Ubuntu 20.04.
- Via the operating system's build-in packages manager.
- Via building NGINX from the source.
In this guide, weโll build NGINX from the source on Ubuntu 20.04.
Prerequisites
To complete this tutorial, you will need to have an Ubuntu 20.04 server. for the sake of simplicity, we'll be working as root during this guide.
Step 1 - Update and Install Dependencies for NGINX
In order to build NGINX from the source first, we need to install a couple of dependencies for NGINX.
Login to your server via SSH terminal.
ssh username@you-IP-address
Update the Ubuntuโs package manager
sudo apt-get update
Now, install development libraries along with source code compilers.
sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev
Press Y
for yes
Step 2 - Download NGINX Source Code and Configure
We Now have all the necessary tools to compile NGINX.
Now, we need to download the NGINX source from their Official website.
Run the following command to download the source code.
wget http://nginx.org/download/nginx-1.20.0.tar.gz
We have now NGINX source code in tarball format extract it by using this command
tar -zxvf nginx-1.20.0.tar.gz
Go to the extracted directory by using this command
cd nginx-1.20.0
Now we have to use configure flag for configuring Nginx by using this command.
./configure --prefix=/var/www/html --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-pcre --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_image_filter_module=dynamic --modules-path=/etc/nginx/modules --with-http_v2_module --with-stream=dynamic --with-http_addition_module --with-http_mp4_module
In the above command, we configured our custom path for the NGINX configuration file, access, and Error Log path with some NGINX's module.
Step 3 - Build NGINX & Adding Modules
There are many configuration options available in NGINX. you can use it as per your need. To find all the configuration options available in NGINX
you can visit nginx.org.
There are some modules that come pre-installed in NGINX.
Modules Built by Default
If you do not need a module that is built by default, you can disable it by naming it with the --without-<MODULE-NAME>
option on the configure script, as in this example which disables the Empty GIF module (should be typed as a single line):
./configure --without-http_empty_gif_module
There are two types of Modules in NGINX that you can use.
Statically Linked Modules
Dynamically Linked Modules
1- Statically Linked Modules
These modules load by default
Most modules built into NGINX are statically linked: they are built into NGINX at compile time and are linked to the NGINX binary statically. These modules can be disabled only by recompiling NGINX.
To compile NGINX with a statically linked thirdโparty module, include the --add-module=<PATH>
option on the configure command, where <PATH>
is the path to the source code (this example is for the RTMP module):
./configure ... --add-module=/usr/build/nginx-rtmp-module
2 - Dynamically Linked Modules
These modules are not loaded by default you have to load them in the NGINX configuration file
For example - load_module modules/ngx_mail_module.so;
NGINX modules can also be compiled as a shared object (*.so file) and then dynamically loaded into NGINX Open Source at runtime. This provides more flexibility, as the module can be loaded or unloaded at any time by adding or removing the associated load_module directive in the NGINX configuration file and reloading the configuration. Note that the module itself must support dynamic linking.
To compile NGINX Open Source with a dynamically loaded thirdโparty module, include the --add-dynamic-module=<PATH>
option on the configure command, where <PATH>
is the path to the source code:
./configure ... --add-dynamic-module=<PATH>
Compiling the NGINX source
After custom configuration complete we can now compile NGINX source code by using this command :
make
This will be taking quite a bit of time and once that's done install the complied source code by using this command.
make install
Start NGINX by using this command
nginx
Now we have successfully installed NGINX. To verify this check the NGINX version by using this command.
nginx -V
Or you can visit your IP to see the holding page NGINX.
http://your-IP-address
Step 4 - Adding an NGINX Service
With our custom-built of NGINX working and listening on HTTP Port 80, the next step is configuring system service for NGINX.
More specifically will be adding NGINX as a systemd service the newer and more popular standard for services.
Now, before we continue to note that systemd is only available since Ubuntu 15.04.
Creating an NGINX service will not only allow us to manage starting stopping and reloading NGINX in a more standardized way but also make starting NGINX on boot much simpler.
Standard NGINX command-line tools
Before we start, however, let's quickly see how to use the standard NGINX command-line tools to execute service signals.
We can confirm that NGINX is running by checking for the process.
ps aux | grep nginx
We can see here the master and worker process.
So with NGINX running in the background, let's see how to send it a stop signal. Using the standard command-line tools.
For example, with NGINX running, we can send the stop signal with NGINX by using this command.
nginx -s stop
You can check the NGINX status by visiting your IP address you will not see any holding page as NGINX is now stopped or more accurately terminated.
You can again start NGINX by using
nginx -s start
So next, let's add that systemd service.
To enable the service, we're going to have to add a small script, which is the same across operating systems.
Create an Nginx systemd unit file by using nano editor
nano /lib/systemd/system/nginx.service
and paste this script
[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/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
You can change the PIDfile location as per your custom configuration path.
Now, save the file by pressing key CLR+X, Y,
and Enter to save this file.
Now you can manage your NGINX by using Systemd for example
systemctl restart nginx
You can also check the status of NGINX whether it is running or not by using this command.
systemctl status nginx
This gives us a really informative printout of the NGINX server status.
Step 5 - Enable NGINX on Boot
Now, as we mentioned, the other very useful feature of a systemd service is enabling NGINX to start automatically when the system Boots at the moment, when this machine is shut down or rebooted NGINX will no longer be running.
Obviously not good for a web server in particular.
So to enable start-up on boot run this command.
systemctl enable nginx
So we get confirmation of a start-up, symlink being created for this service.
We can test this by rebooting the machine reboot.
Conclusion
In this guide, we have build NGINX from source on Ubuntu 20.04. if you have any question regarding this please let me know in the comment section.
Top comments (0)