How to install Nginx web server on CentOS, Debian & Ubuntu Cloud Servers

2020-06-19 By Robert 15505 Views linux nginx development
6 reviews

The Nginx is a free, open-source, lightweight and high-performance web server designed for high-traffic use cases. It is also acting as a popular Web server behind the Apache Web server and Microsoft’s IIS. Nginx is more resource-friendly than Apache. It can be served in front of other web servers as a reverse proxy.


All Nginx configuration files are stored in the /etc/nginx/ directory and /etc/nginx/nginx.conf is the primary configuration file.

The default server root directory in CentOS is /usr/share/nginx/html and in Ubuntu and Debian it is /var/www/html.

Also, any additional server block (virtual hosts in Apache2) for a website can be added in the location /etc/nginx/conf.d.


See the instructions for installing, configuring and testing the Nginx server on CentOS, Debian and Ubuntu below.


Start to install Nginx


CentOS 7 & 8

  1. Run the following commands to install Nginx:

    # yum install epel-release
    
    # yum install nginx
    

    nginx1

    nginx2

  2. Nginx does not start automatically even after the installation is completed. Run the following command to start the Nginx process.

    # systemctl start nginx
    

    nginx3

  3. Run the following command to make sure the service is running.

    # systemctl status nginx
    

    nginx4

  4. Run the following command to restart Nginx.

    # systemctl restart nginx
    

    nginx5


CentOS 6

  1. Run the following commands to install Nginx:

    # yum install epel-release
    
    # yum install nginx
    

    nginx6

    nginx7

  2. Nginx does not start automatically even after the installation is completed. Run the following command to start the Nginx process.

    # service nginx start
    
  3. Run the following command to make sure the service is running:

    # service nginx status
    

    nginx8

  4. Run the following command to restart Nginx.

    # service nginx restart
    

    nginx9


Ubuntu / Debian

  1. Run the following command to install Nginx.

    # apt update
    
    # apt install nginx
    

    nginx10

    nginx11

  2. Nginx does not start automatically even after the installation is completed. Run the following command to start the Nginx process.

    # /etc/init.d/nginx start
    

    nginx12

  3. Run the following command to make sure the service is running.

    # /etc/init.d/nginx status
    

    nginx13

  4. Run the following command to restart Nginx:

    # /etc/init.d/nginx restart
    

    nginx14

  5. After installation is completed, you can check whether the Nginx is installed or not by entering a server IP address into your browser’s address bar:

    http://your_server_ip
    


    You can see the default Nginx web page as shown below if it is successfully installed.

    nginx15


To Configure Nginx

The default Nginx configuration files are kept in /etc/nginx/sites-available and it is symbolically linked with /etc/nginx/sites-enabled/. Commonly needs to create a separate file in the sites-available directory for each domain/subdomain and set up a symlink in the sites-enabled directory.


  1. Remove the symlink in /etc/nginx/sites-enabled/ to disable the default configuration file.

    # unlink /etc/nginx/sites-enabled/default
    

    nginx16

  2. Create a new directory and the configuration file for the website and add the below configurations in the configuration file and save. Also, create a basic index file in /var/www/layerstack.com.

    # vi /var/www/layerstack.com/index.html
    

    nginx17

    # mkdir /var/www/layerstack.com
    
    # vi /etc/nginx/sites-available/layerstack.com
    

    nginx18

    server {
    listen  80;
    listen [::]:80;
    server_name layerstack.com;
    root /var/www/layerstack.com
    index index.html;
    location / {
    try_files $uri $uri/ =404;
    }
    }
    

    nginx19

  3. Create a new symlink to the /etc/nginx/sites-enabled/ directory for enabling the configuration.

    # ln -s /etc/nginx/sites-available/layerstack.com /etc/nginx/sites-enabled/
    

    nginx16


To Test Nginx


  1. The below command is used to test the configuration for errors.

    # nginx -t
    

    nginx18

  2. Reload the configuration using the below command.

    # nginx -s reload
    

    nginx22

    NOTE: Replace the website name layerstack.com with the original website name

  3. Now load the website name/ IP address in a browser and the index page will load.

    nginx23


Related Tutorials

What do you think about this article?

Rate this article
LayerStack Promotion
Need assistance?

Try this guide to receive free bundled services at signup on a new free account.

Sign Up

Your Feedback Is Important

We hope you’ll give the new products and updates a try. If you have an idea for improving our products or want to vote on other user ideas so they get prioritized, please submit your feedback on our Community platform. And if you have any questions, please feel free to ask in the Community or contact our Technical Support team.