There are many scenarios when a server admin has to install the NGINX web server by compiling it and the fact is, compiling and installing from the sources provides you more flexibility. Also compiling give you the ease of adding particular NGINX modules or 3rd party modules and helps you in applying the 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 versions for NGINX
- 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.
- The stable version: It includes critical bugs but with no new feature. The stable version is recommended for production servers.
Host OS: CentOS/RHEL 6/7
Host IP: 192.168.1.62
RAM: 4GB Memory
Network Port: 1GB/s
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.
yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel
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 the link
cd /usr/share/ NginxVersion="1.10.2" wget http://nginx.org/download/nginx-$NginxVersion.tar.gz tar -xvzf nginx-$NginxVersion.tar.gz rm -rf nginx-$NginxVersion.tar.gz ln -sf nginx-$NginxVersion nginx
Step 3: Preparation for Compiling
When we compile NGINX we need to know a few of its options with which we are going to compile it. The below command will give you every option
cd nginx ./configure --help --help print this message --prefix=PATH set installation prefix --sbin-path=PATH set nginx binary pathname ..................................................... ................ It's A Long List ................... ..................................................... --with-libatomic force libatomic_ops library usage --with-libatomic=DIR set path to libatomic_ops library sources--with-openssl=DIR set path to OpenSSL library sources --with-openssl-opt=OPTIONS set additional build options for OpenSSL --with-debug enable debug logging
Well, it’s a huge list and you can choose options according to your need but here we need to choose basic Requirements 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 options are used to run the NGINX worker processes in non-privileged.
--user --group --prefix --sbin-path --conf-path --pid-path --lock-path --error-log-path --http-log-path
Learn few more Options
- –with-http_gzip_static_module option is used enable gzip in NGINX. It is recommended for reducing the size of information sent
- –with-http_stub_status_module option enables other plugins over NGINX to allow us to get the status. It is recommended for getting stats
- –with-http_ssl_module – required if you want to run an HTTPS server.
- –with-pcre option will enable Regular Expression Matching. Using this you will find more use once you start adding and matching routes
- –with-file-aio – enables asynchronous I/O to use this if are allowing users to download static files)
- –with-http_realip_module is used for getting the IP of the client when behind a load balancer.
- –without-http_scgi_module – Disable SCGI module (normally used when running CGI scripts)
- –without-http_uwsgi_module – Disable UWSGI module (normally used when running CGI scripts)
- –without-http_fastcgi_module – Disable FastCGI module (normally used when running CGI scripts)
Step 4: Compile NGINX
Now from step 3, we have chosen our option and now we can compile our NGINX with our specified option like below
./configure \ --user=nginx \ --group=nginx \ --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --pid-path=/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-pcre \ --with-file-aio \ --with-http_realip_module \ --without-http_scgi_module \ --without-http_uwsgi_module \ --without-http_fastcgi_module
After Completion Run the below command
make make install
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
useradd -r 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
wget -O /etc/init.d/nginx https://raw.githubusercontent.com/kapendra007/NGINX/eadb7459650c04caeb210fcd3c1691046e1feb89/NginxFilesDefault/etc-init-nginx-centos6.txt
Make File executable
chmod +x /etc/init.d/nginx
Enable service For system boots:
chkconfig --add nginx chkconfig --level 345 nginx on
Step 6: Configuring Nginx
We have successfully set up basic requirements for the NGINX server. Now, we need to set a configuration file for running an 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 backup of nginx.conf
wget -O /etc/nginx/nginx.conf https://raw.githubusercontent.com/kapendra007/NGINX/master/NginxFilesDefault/nginx.conf
Now, start the server. This will start it on Port 80 according to the downloaded
service nginx start
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/
And if you can see the above 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 to the NGINX server.
For this example, I am updating 1.10.2 to 1.10.3. Well if you are looking for some other different version then you may get the version number from the link
cd /usr/share/ NginxVersion="1.10.3"
Download The TAR And Extract
wget http://nginx.org/download/nginx-$NginxVersion.tar.gz tar -xzf nginx-$NginxVersion.tar.gz rm -rf nginx-$NginxVersion.tar.gz
Remove Softlink From the Previous Version And Recreate Softlink With New Version
rm nginx ln -sf nginx-$NginxVersion nginx
Compile New NGINX Version
cd nginx ./configure \ --user=nginx \ --group=nginx \ --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --pid-path=/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-pcre \ --with-file-aio \ --with-http_realip_module \ --without-http_scgi_module \ --without-http_uwsgi_module \ --without-http_fastcgi_module make make install
Restart Your Web Server
service nginx restart
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 an NGINX web server. If you like this article please do share this. In our next article, we will learn to learn reverse proxy.