How To Compile And Install Custom Version Of NGINX ON CentOS/RHEL 6/7
CentOs Nginx RHEL Tutorials Web Servers

How To Compile And Install Custom Version Of NGINX ON CentOS/RHEL 6/7

There are many scenarios when a server admin has to install Nginx web server by compiling it and the fact is, compiling and installing from the sources provides you more flexibility. Also compiling give you ease of adding particular NGINX modules or 3rd party modules and helps you in applying latest security patches.

Well, when you are installing NGINX or any application from the source you need to choose any one version given below. There are two kinds of version for NGINX

  1. The mainline Version: It’s the latest one and has almost every bug fixed with new features. It is included some experimental modules but it’s reliable to use.
  2. The stable Version: It includes critical bug but with no new feature. The stable version is recommended for production servers.

Scenario:

Host OS: CentOS/RHEL 6/7
Host IP: 192.168.1.62
RAM: 4GB Memory
Network Port: 1GB/s
HDD: 1000GB of disk space

So Let’s Start

Step 1: Install Dependencies for NGINX

To complete NGINX from the source we need to install few dependencies. This includes development libraries along with source code compilers.

Step 2: Download Source Code

Now get the download link for NGINX source code from its official website I am using the “1.10.2” version. Let’s get this NGINX version number from link http://nginx.org/en/download.html

Step 3: Preparation for Compiling

When we compile NGINX we need to know few of its option with which we are going to compile it. The below command will give you every option

Well, it’s a huge list and you can choose option according to your need but here we need to choose basic Requirement for installation because we need the basic variables to override and to use default system paths at /etc/. We want a thing to look similar like we are installing NGINX using via rpm. The user and group option are used to run the NGINX worker processes in non-privileged.

Learn few more Options

  1.  –with-http_gzip_static_module option is used enable gzip in NGINX. It is recommended for reducing size of information sent
  2.  –with-http_stub_status_module option enables other plugins over NGINX to allow us to get the status. It is recommended for getting stats
  3. –with-http_ssl_module – required if you want to run a HTTPS server.
  4. –with-pcre option will enable Regular Expression Matching. Using this  you will find more use of this once you start adding and matching routes
  5. –with-file-aio – enables asynchronous I/O use this if are allowing users to download static files)
  6. –with-http_realip_module is used for getting the IP of the client when behind a load balancer.
  7. –without-http_scgi_module – Disable SCGI module (normally used when running CGI scripts)
  8. –without-http_uwsgi_module – Disable UWSGI module (normally used when running CGI scripts)
  9. –without-http_fastcgi_module – Disable FastCGI module (normally used when running CGI scripts)

Step 4: Compile NGINX

Now from the step 3 we have chosen our option and now we can compile our NGINX with our specified option like below

After Completion Run below command

Step 5: Setting UP Scripts and NGINX

To initialize or to run NGINX we need a user. The given below command is the one-time command which will add the user for running NGINX

Now We will set up init/systemd scripts to manage the nginx service.

For Centos/RHEL 6/7: Run The Following Command

We will save this file as /etc/init.d/nginx

Make File executable

Enable service For system boots:

Step 6: Configuring Nginx

We have successfully setup basic requirements for NGINX server. Now, we need to set configuration file for running a NGINX server by configuring /etc/nginx/nginx.conf. To configure this file you may download this from my repository or copy paste your required blocks.

But we really need to set up  types_hash_bucket_size and server_names_hash_bucket_size . To get these setup you should run the following command but before doing this I would recommend taking e a back up of nginx.conf

Now, start the server. This will start it on Port 80 according to the downloaded nginx.conf file

Step 7: Check Your Work

We have successfully compiled the nginx as same as it would have been done through rpm or yum. Now you may extend its capabilities by adding modules. But Before this, we have to check that our compiled NGINX server is working or not. So just open your browser and hit http://ServerIP/ Well in my case I will hit http://192.168.1.62/

How To Compile And Install Custom Version Of NGINX ON CentOS/RHEL 6/7

And if you can see below page then it’s a success.

Upgrading to the Latest Version

Now quickly learn how to update your running compiled running NGINX server to a new version or downgrading it. This technique is also useful in adding modules in NGINX server.

For this example, I am updating 1.10.2 to 1.10.3. Well if you are looking for your some other different version then you may get the version number from http://nginx.org/en/download.html

Download The TAR And Extract

Remove Softlink From the Previous Version And Recreate Softlink With New Version

Compile New NGINX Version

Restart Your Web Server

NOTE : Don’t forget to move your HTML content from /usr/share/nginx-1.10.2/html/ To /usr/share/nginx-1.10.3/html/

Well, this was a really quick method of compiling a NGINX web server. If you like this article please do share this. In our next article, we will learn to learn reverse proxy.

Kapendra
Love to write technical stuff with personal experience as I am working as a Sr. Linux Admin. and every day is a learning day and Trust me being tech geek is really cool.
http://kapendra.com

Leave a Reply