Installing NGINX on CentOS 8

Select distribution:
Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $100 credit.
This credit will be applied to any valid services used during your first 60 days.

What is NGINX?

NGINX is an open source web server with powerful load balancing, reverse proxy, and caching features. It was initially designed to solve scaling and concurrency problems with existing web servers. Its event-based, asynchronous architecture has made it one of the most popular and best-performing web servers available today.

Before You Begin

  1. Set up your Linode in the Creating a Compute Instance and Setting Up and Securing a Compute Instance guide.

  2. If you want a custom domain name for your site, you can set this up using our DNS Manager guide.

  3. Install the SELinux core policy Python utilities. This will give you the ability to manage SELinux settings in a fine-grained way.

     sudo dnf install -y policycoreutils-python-utils
    
    Note

    This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, visit our Users and Groups guide.

    All configuration files should be edited with elevated privileges. Remember to include sudo before running your text editor.

Install NGINX

Currently, the best way to install NGINX on CentOS 8 is to use the version included in CentOS’s repositories:

sudo dnf clean all
sudo dnf update
sudo dnf install nginx

Add a Basic Site

  1. Create a new directory for your site. Replace example.com with your site’s domain name.

    sudo mkdir -p /var/www/example.com
    
  2. Use SELinux’s chcon command to change the file security context for web content:

     sudo chcon -t httpd_sys_content_t /var/www/example.com -R
     sudo chcon -t httpd_sys_rw_content_t /var/www/example.com -R
     sudo ls -dZ /var/www/example.com
    
  3. You can add your site’s files in your /var/www/example.com directory. Create an index file with a simple “Hello World” example. Using the text editor of your choice, create a new file, /var/www/example.com/index.html. Replace example.com with your website’s domain name or your Linode’s public IP address.

    File: /var/www/example.com/index.html
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    <!DOCTYPE html>
    <html>
        <head>
            <title>My Basic Website</title>
        </head>
        <body>
            <header>
                <h1>Hello World!</h1>
            </header>
        </body>
    </html>

Configure NGINX

NGINX site-specific configuration files are kept in /etc/nginx/sites-available and symlinked to /etc/nginx/sites-enabled/. Generally, you will create a new file containing a server block in the sites-available directory for each domain or subdomain you will be hosting. Then, you will set up a symlink to your files in the sites-enabled directory.

  1. Create the directories for your configuration files:

    sudo mkdir -p /etc/nginx/{sites-available,sites-enabled}
    
  2. Create your site’s configuration file in the text editor of your choice. Replace example.com in the server_name directive with your site’s domain name or IP address and /var/www/example.com in the root directive with your own root directory’s location.

    File: /etc/nginx/sites-available/example.com
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    server {
        listen 80;
        listen [::]:80;
        server_name  example.com;
    
        root /var/www/example.com;
        index index.html;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
  3. Set up a new symlink to the /etc/nginx/sites-enabled/ directory to enable your configuration:

    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

  4. Update the NGINX configuration file, /etc/nginx/nginx.conf, to add an include directive to the /etc/nginx/sites-enabled/* directory. This include must be within your configuration files' http block. Place the include directive below the include /etc/nginx/conf.d/*.conf; line.

    File: /etc/nginx/nginx.conf
    1
    2
    3
    4
    5
    6
    7
    
    ...
    http {
    ...
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
    ...
    }
  5. Open the firewall for traffic:

    sudo firewall-cmd --zone=public --permanent --add-service={http,https}
    sudo firewall-cmd --list-all
    sudo firewall-cmd --reload
    

Test and Enable NGINX

  1. You can test your NGINX configuration with this command:

    sudo nginx -t
    
  2. Start the service with the following commands:

    sudo systemctl enable nginx
    sudo systemctl start nginx
    
  3. Verify that it’s running:

    sudo systemctl status nginx
    
  4. Navigate to your Linode’s domain name or IP address in a browser. You should see your simple page displayed.

Advanced Configuration

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide made it easy to get the answer you needed.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.