Installing NGINX on CentOS 8

Updated by Linode Written by Linode

Contribute on GitHub

Report an Issue | View File | Edit File

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 Getting Started and Securing your Server guides.

  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 yum 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 yum 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
    
  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.

    /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.

    /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.

    /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
    sudo firewall-cmd --zone=public --permanent --add-service=https
    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.

Join our Community

Find answers, ask questions, and help others.

This guide is published under a CC BY-ND 4.0 license.