How to Install NGINX on CentOS, Redhat and Fedora

How to Install NGINX on CentOS, Redhat and Fedora

NGINX (Engine X) is a web server, Its getting popular very fast due to its fast speed. NGINX is also used as reverse proxy server. This tutorial will help you to setup NGINX on CentOS, Redhat 6/5 and Fedora Systems.

Step 1: Install Nginx

At the time of updating this article latest version of NGIX 1.6.0 is available under atomic repository for CentOS/Redhat 6/5. So first we need to add atomic repository in our system using following command.

# rpm -Uvh http://www6.atomicorp.com/channels/atomic/centos/6/i386/RPMS/atomic-release-1.0-19.el6.art.noarch.rpm

Now use the following command to install NGINX web server on your CentOS/Redhat system.

# yum install nginx

Step 2: Nginx Basic Configuration

Do some initial setting before starting nginx. Edit Nginx main configuration file /etc/nginx/nginx.conf and update following values.

# vim /etc/nginx/nginx.conf

Update following values:

worker_processes  4;  # Number of CPU available in system.
listen  80;  # Port on which nginx will listen.

Step 3: Start Nginx Service

Use following commands to start Nginx web server and configure Nginx to auto start on system boot.

# service nginx start
# chkconfig nginx on

Now access your server on port 80 with your favorite browser, It will show default nginx page.

Step 4: Create First VirtualHost in NGINX

After making initial configuration changes in NGINX, let’s start with configuring first virtual host. Firstly create document root for your application

# mkdir -p /var/www/example.com/httpdocs
# chown -R nginx.nginx /var/www/example.com/

Now create an index file with sample text

# echo "Welcome to NGINX" > /var/www/example.com/index.html

Now create a virtual host configuration file. I just create a copy of default.conf file with name example.com.conf and edit file in editor.

# cp /etc/nginx/conf.d/virtual.conf /etc/nginx/conf.d/example.com.conf
# vim /etc/nginx/conf.d/example.com.conf

Make few changes in configuration file like below and keep other settings as it is.

server {
    listen       80;
    server_name  example.com;

    location / {
        root   /var/www/example.com/httpdocs;
        index  index.html index.htm;

    }

Finally restart NGINX service using following command

# service nginx restart

Congratulation’s! You have successfully installed Nginx. Read out other articles Install Nginx, MySQL, PHP (LEMP) Stack with PHP-FPM

Was this Tutorial helpful? Help others share on Facebook, Twitter, and Google Plus!

 
Enjoyed this video?
How to Install NGINX on CentOS, Redhat and Fedora
"No Thanks. Please Close This Box!"