Nginx Web-Server Installation and Initial Configuration on CentOS/RHEL

Nginx is an open source web server and a reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols, with a strong focus on high concurrency, performance and low memory usage.

This guide explains how to install and perform the initial configuration of Nginx Web Server on CentOS/RHEL based systems.

Adding Nginx Repository

If you want the latest version of nginx with all features and bugfixes, it’s recommended to use packages directly from the nginx yum repository.

Packages from the usual repositories are often somewhat out-of-date.

To add the nginx 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.

Nginx Installation

Install the nginx web server with yum:

# yum install nginx

Set the nginx to start at boot:

# chkconfig nginx on

Basic Nginx 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

Firewall opening for Nginx

Edit the file /etc/sysconfig/iptables, if you use the firewall. Append the following line before the REJECT line, to open http port 80:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

Save and close the file. Restart the firewall.

# service iptables restart

Confirming the installation

To confirm that the Nginx has been successfully installed, you can enter your server’s name or IP address in your web-browser.

You should see “Welcome to Nginx!” page.

If you see this page, the Nginx Web Server is successfully installed and working.

Additional information

Default Nginx configuration files and ports

  • /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;
  • TCP 80 – Nginx HTTP default port;
  • TCP 443 – Nginx HTTPS default port.

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
Was it useful? Share this post with the world!

One Reply to “Nginx Web-Server Installation and Initial Configuration on CentOS/RHEL”

  1. Please stop telling people to run # curl | sudo bash –
    https://nginx.org/en/linux_packages.html#RHEL-CentOS has secure instructions

Leave a Reply