Nginx is an open source web server which is robust, fast and very easy to work with. It can be used to replace Apache or configured as a reverse proxy for Apache server. Nginx can also be configured to act as a load balancer.
This guide explains how to install and perform the initial configuration of Nginx Web Server on CentOS/RHEL based systems.
Nginx Repository
It is recommended to install nginx packages directly from the nginx yum repository.
To add the yum repository, create a file named /etc/yum.repos.d/nginx.repo, and paste one of the configurations below.
For CentOS :
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
For RHEL:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/
gpgcheck=0
enabled=1
Manually replace $releasever with either “5” (for 5.x) or “6” (for 6.x), depending upon your OS version.
Installation
Install the nginx web server with yum :
# yum install nginx
Set the nginx to start at boot :
# chkconfig nginx on
Basic Configuration
Backup the configuration files :
# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.back
# cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.back
Edit the nginx configuration file /etc/nginx/nginx.conf. Change the worker_processes value. It should be equal to the number of CPUs in your server.
worker_processes 1;
To list the number of CPUs, you can use the following command :
# lscpu | grep ‘^CPU(s)’
Enable compression by uncommenting the following line :
gzip on;
Save and close the file.
Edit the file /etc/nginx/conf.d/default.conf. Set the server’s name :
server_name example.com;
Save and close the file. Start Nginx :
# service nginx start
Confirming the installation
Enter the server’s name or IP address in your web-browser. If you see the following text, it means Nginx is successfully installed.
“Welcome to Nginx!”
Following are the default Nginx configuration files.
/etc/nginx/ – Nginx server configuration directory;
/etc/nginx/conf.d/ – SSL and vhost configuration directory;
/etc/nginx/nginx.conf – main configuration file;
/var/log/nginx/error.log – error logs;
/var/log/nginx/access.log – access logs;
/usr/share/nginx/html/ – Nginx default document root directory;
Following are some Nginx commands
# /etc/init.d/nginx start
# /etc/init.d/nginx stop
# /etc/init.d/nginx restart
# /etc/init.d/nginx condrestart
# /etc/init.d/nginx try-restart
# /etc/init.d/nginx force-reload
# /etc/init.d/nginx upgrade
# /etc/init.d/nginx reload
# /etc/init.d/nginx status
# /etc/init.d/nginx help
# /etc/init.d/nginx configtest