A complete guide to install, configure and run nginx on RHEL/CentOS

Updated on September 2, 2017

In our previous article we saw How to install nginx using yum repository for RHEL/CentOS. Now lets see how to quickly configure nginx and start the service.

Step 1: Make nginx start automatically during system startup.

The chkconfig command allows you to configure services to start and stop automatically during the system startup. Run the below command to make nginx to start automatically during the system startup.

#chkconfig nginx on

Below gif image shows you whether nginx service already listed in system startup and if not how to include.

chkconfig nginx on

Step 2: How to manually start/stop nginx web-server

If you have installed nginx using yum, automatically startup scripts would be installed in /etc/init.d/. You can start/stop/restart nginx service using the below commands:

#service nginx start
#service nginx stop
#service nginx restart
#service nginx status
#service nginx reload

Note: reload does not restart, but just loads any configuration changes in nginx. This is used because it avoids stopping of all the traffic for that moment, cutting down all the active connections to the server.

Step3: Nginx configuration files location

nginx stores its configuration files in /etc/nginx/ directory as follows:

1. Default configuration file: /etc/nginx/nginx.conf
2. Default SSL and vhost config directory : /etc/nginx/conf.d/
3. Default logging directory : /var/log/nginx/
4. Default document root directory : /usr/share/nginx/html

Step 4: Basic nginx configuration procedure

With your favorite editor open the default configuration file /etc/nginx/nginx.conf and update worker_processes to the number of CPUs or cores. A worker process is a single-threaded process. You can use the command : lscpu |grep ‘^CPU(s)’ to find out the number of CPUs in the server.

worker_processes 2;

With your favorite editor update the file : /etc/nginx/conf.d/default.conf with below configuration changes.

Set IP address and TCP port number:

listen <IP>:80;

Set server name:

server_name www.techglimpse.com;

location of your document directory:

location / {
root /usr/share/nginx/html;
index index.html index.htm;

Step 5: Start nginx service

Start the nginx web server using the below command:

#service nginx start

Step 6: Verify nginx service and port open

# netstat -tulpn |grep :80
# ps aux|grep nginx

Step 7: Goto your browser and access your domain or system ip

nginx installation and configuration complete

Hurray you successfuly completed the nginx installation and configuration

This tutorial is also available in a quick video format.

Was this article helpful?

Related Articles

Leave a Comment