Skip to content

How to Install NGINX Web Server on CentOS/RHEL 7/6 and Fedora 27/26

Install Nginx on CentOS. Nginx Installation on Fedora/CentOS. Steps to install Nginx web server on RedHat based system. Best way to Install Nginx on CentOS

Steps
4
Category
Web Servers
Covers
CentOS, Fedora, Nginx
On this page 4 steps

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

Step 1 of 4Install Nginx

Nginx packages are available under default repositories. Execute the following command to install latest Nginx available version on your system.

$ yum install nginx    
$ dnf install nginx    #On Fedora 22+ systems 

Step 2 of 4Nginx 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 of 4Start Nginx Service

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

$ systemctl enable nginx.service
$ systemctl start nginx.service

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

Install Nginx on CentOS

Step 4 of 4Create VirtualHost in NGINX

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

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

Now create an index file with sample text

$ echo "Welcome to NGINX" > /var/www/example.com/httpdocs/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 the 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 the 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 the NGINX service using the following command

$ sudo systemctl restart nginx.service

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

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

2 Comments

  1. I think that you could do with some pics to drive the message home a little bit, but other than that, this is great blog. A great read. I will certainly be back.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top