In this tutorial, we will learn about how to install nginx webserver from source on CentOS and RHEL. At present time, many companies prefer Nginx over apache. One of the biggest reason is, it can handle 10,000 simultaneous connections with a low memory footprint (~2.5 MB per 10k inactive HTTP keep-alive connections). Nginx is also light weight and fast.
Nginx is used by many big companies.No doubt, only because of its awesome features and reliability.
Nginx ( pronounced as Engine X ) is an HTTP and reverse proxy server,Load balancer, as well as a mail proxy server, written by Igor Sysoev.
I generally prefer to install nginx with source code. The reason is, there are many modules which require compilation with nginx source code.
Nginx web server administrator should have good Regex knowledge. You may find many regex in its configuration file.
Nginx has lots of feature, you can read about these feature from Nginx website.
Installing Nginx Web Server from source on Linux
In this section, we will install the nginx on linux server. We will install default modules which comes with, at time of compilation. Whereas some module require prerequisite, we will also install it before going to compilation.
Login to your Linux Server as a root user. And follow the given below steps :
Step 1 : Install EPEL repo
First we are installing EPEL repo on our linux system. The reason I selected EPEL repo is for installing the package called libxml2 and libxslt .
With respect of your operating system architecture, install the respective epel repo file. At the time of writing this post epel-release-6-8.noarch.rpm was latest one.
I have CentOS 6.5, with O.S architecture x86_64 . (Use command arch
and cat /etc/release
for getting these information) .
Hence, for installing EPEL repo we will run below given command
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
Read more about EPEL
Step 2 : Install prerequisite packages by using yum command
Now install prerequisite packages by using yum command.
yum install gcc pcre-devel zlib-devel make wget openssl-devel gd gd-devel libxml2 libxml2-devel libxslt libxslt-devel GeoIP GeoIP-devel
Step 3 : Download Nginx source tarball
Now download the latest stable nginx source from Nginx website.
At the time of writing this post, Nginx version 1.6.0 was available latest stable release.
wget nginx.org/download/nginx-1.6.0.tar.gz
Step 4: Create Nginx user
Create nginx user with nologin shell
useradd nginx usermod -s /sbin/nologin nginx
Step 5 : Extract the Nginx tarbal
Use tar command to extract the nginx tarball package.
tar -xvzf nginx-1.6.0.tar.gz
After extraction, I got the directory called nginx-1.6.0. The name may be different in your case if you have downloaded latest or other version.
Step 7 : Compiling Nginx source
Now we will compile the Nginx source. Hence, first change to directory which was extracted from Nginx tar ball (Read Step 4)
After extraction I got directory named nginx-1.6.0. I will change to this directory.
cd nginx-1.6.0
Important Note: I will suggest you to read some more available options with configure script. You can get this information by running the command
./configure help
Now run the below given command. With configure script, we have installed and enable almost all default modules. In case, you do not want any particular module, you can remove it from given below command.
./configure --user=nginx --group=nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module
After running successfully you will get the summary. See below given screenshot
Now run make script.
make && make install
Step 8 : Create init script
Create init script for Nginx service. With this script you will get start/restart/stop/reload/status options.
Create a init script file called nginx in /etc/init.d
vi /etc/init.d/nginx
Paste the below given script content in file /etc/init.d/nginx . Do save and exit from file.
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=([^ ]*).*/1/g' -` if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
Step 9 : Give executable permission to nginx init script
Give executable permission to nginx init script
chmod 750 /etc/init.d/nginx
Step 10 : Nginx service start/stop/restart/status
Now you can manage the nginx service with this ngin init script (which we have create on Step 8)
For starting nginx service
/etc/init.d/nginx start
For re-starting nginx service
/etc/init.d/nginx restart
For stoping nginx service
/etc/init.d/nginx stop
For status check of nginx service
/etc/init.d/nginx status
For reloading nginx service
/etc/init.d/nginx reload
For nginx configuration testing
/etc/init.d/nginx configtest
Step 11 : IPTABLE for nginx
By Default, nginx web server listen on port number 80 . It is default port number for HTTP .
Hence, we can simply use IPTABLE which will allow port number 80.
Add the below given line in /etc/sysconfig/iptables file . It should be in Accept rules section.
-A INPUT -p tcp --dport 80 -j ACCEPT
Given below is the reference from my system. I have written the rule in ACCEPT section.
[root@localhost ~]# cat /etc/sysconfig/iptables # Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp --dport 80 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT [root@localhost ~]#
Now restart the iptables service
service iptables restart
Step 12 : Now start the nginx service
Start the Nginx service
/etc/init.d/nginx start
OR
/etc/init.d/nginx restart
Step 14 : Open the Browser and check the default web page of Nginx
Check the default web page of nginx by typing IP Address of Server in URL field of web browser.
In my case, server ip address is 192.168.56.104 . Hence, we will write on URL field –
http://192.168.56.104
Replace 192.168.56.104 with your Server IP Address
James says
How do I enable nginx to start automatically after reboot
Sharad Chhetri says
In RHEL 6.5 / CentOS 6.5 or below version,
chkconfig nginx on
In RHEL 7.x/CentOS 7.x or above
systemctl enable nginx
Regards
Sharad
James says
Thanks ๐