Install Nginx From source

Install Nginx From source

Photo by Joan Gamell on Unsplash

NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. In this article I present a step by step guide to install Nginx server from source on a RHEL server. There are two ways to install Nginx server. We can simply install using the “epel” repository or take a bit complex approach by installing it from source. If you need to have a more control over the packages and the configuration of the installation it is necessary to install Nginx from source.

The simple way to install Nginx.

1.First add the epel repository

$sudo yum install epel-release

2.Install the Nginx server

$sudo yum install nginx

3.yum package manager does not automatically start the Nginx service. Start the Nginx service by,

 $nginx start

Install Nginx from the Source

  1. Update your repositories
$yum check update

2.Download the source file from nginx download link. This includes the the changes in each version, source file and the windows binary installer. we can use the wget command to download the source file. copy the download link and use following command to download the source file and unzip the download file.

$wget https://nginx.org/download/nginx-1.21.4.tar.gz
$tar -zxvf <file name>  #use this command to unzip the file

3.we need to install few development tools to compile the Nginx source code. Therefore run the following command to install necessary tool.

$yum group install “Development Tools”

4. Install following Packages as well. we need these packages to compile and run the nginx source code. And some packages are necessary to run the nginx server.

$ yum install \ gcc \ zlib-devel \ openssl-devel \ make \ pcre-devel \ libxml2-devel \ libxslt-devel \ libgcrypt-devel \ gd-devel \ perl-ExtUtils-Embed

5 Now go to the resource folder and run ./configure to add configuration. If you need to install external modules we need to pass modules names as arguments to the ./configure command. all the available packages can be seen in http://nginx.org/en/docs/configure.html. I have include several useful modules in the example. After running the following command you can see the generated configuration files at the given nginx configuration location given in the command. ex: /etc/nginx

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

after completely generating configuration files use make and make install to execute the source.

we can simply start nginx by simply running nginx command in the terminal.

Adding Nginx as a system service

in this section we are gonna discuss how to add nginx as a systemd service.

What is systemd?

systemd enables it to start, stop or restart services in a more standard way. we can stop nginx by nginx -s stop. all the available nginx commands can be seen by nginx -h .

systemd service script can be found in URL : https://www.nginx.com/resources/wiki/start/topics/examples/systemd/. create the /lib/systemd/system/nginx.service and include the following script in the file.

do not forget to change the following paths based on your configuration made during the installation.

PIDFile=/run/nginx.pid 
ExecStartPre=/usr/sbin/nginx -t 
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload

After successfully adding the script you can start the nginx service by systemctl start nginx

Hope this guide help you to install Nginx server from source. If you need any clarification feel free to reach me on navindabc@gmail.com. Cheers!! Happy Hacking :D