How to compile nginx from source code on Centos Server

nginx pronounced as “engine x” an HTTP and reverse mail proxy server and a generic TCP/UDP proxy server. For a long time, it has been running on many heavily loaded Russian sites including YandexMail.Ru and Rambler. Today I briefly explain you how to compile nginx from source code.

Why we compile from source code rather than precompiled?

I know compiling source code is not an easy task it has many disadvantages which are as follows:

  • Time taken.
  • Separately install all the dependencies for your software.
  • Complete grip over software compilation.

But recompilation is useful when:(advantages)

  • Compile your useful module rather than all one.
  • Use latest version rather than older version.
  • Can use your own configuration that is useful for your project.

Create a new Centos Machine:

For compilation you need Centos machine. You can buy new VPS or install Centos on virtual machine software like Virtual Box or VMware.

  • First of all you have to connect VM from SSH or boot your machine if you are using your own computer.

ssh root@example.com

  • Now you have to install all dependencies for nginx

yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel

  • For compilation you need source code. So, you can download latest version of nginx from the following website:

http://nginx.org/download/

or

https://github.com/nginx/nginx

  • Here I am using nginx version 1.9.15. You can download it by the following command:

wget http://nginx.org/download/nginx-1.9.15.tar.gz

or

git clone git@github.com:nginx/nginx.git

  • Now you have to unzip source code by use following command.

tar –xzf nginx-1.9.5.tar.gz

Download and extraction complete finally compilation process start here:

As I earlier said nginx is very big software and it has many module inside. Here you have to choose which module is useful for your website. You can review the list below. Also you can check by your own system by following the command:

cd nginx-1.9.5

./configure –help

Here I am using following modules for my website server:

./configure \
--user=nginx \
--group=nginx \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/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

You can copy the following modules from above if you did not want to change any of the modules.

Configuration complete now we have to compile our source code by the follow commands:

make

make install

Congratulations your compilation is complete. Now you are few steps away from your goal.

 1.  We need to create nginx startup file in init.d folder. Because every time server restarts init process automatically starts our web server

 Follow these commands:

      Cd /etc/init.d

Download this file from the following command:

wget https://gist.github.com/sairam/5892520/raw/b8195a71e944d46271c8a49f2717f70bcd04bf1a/etc-init.d-nginx

Now make this file to become executable:

chmod +x nginx

Create new account with name nginx

useradd -r nginx

Set the service to start whenever the system boots:

chkconfig --add nginx
chkconfig --level 345 nginx on

Open configuration file /etc/nginx/nginx.conf find http{ in this file and copy paste these two lines

types_hash_bucket_size 64;
server_names_hash_bucket_size 128;

Start the server.

service nginx start

You can view detail video down below:

 


If you have any queries feel free to ask. I hope you like my article please share your thoughts down below.

You can also submit your problems and solutions under HOW TO category at Pakistanblogs.com

code source: DigitalOcean and nginx

Author: Muhammad Ilyas

Journalist, Blogging & Engineering Student by Profession, Website designer and Developer by Passion. Also a co-owner at Pakistan Blogs.

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.