Installing and Using NGINX on AlmaLinux 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.

NGINX (pronounced “engine-X”) is an open-source web server that excels at load balancing, caching, and acting as a reverse proxy. NGINX was created to be a high-performing and scalable alternative to other popular web servers, like Apache. Its event-driven architecture continues to set it apart as one of the fastest and lightest web servers available. This guide shows you how to install NGINX on your AlmaLinux 8 server and how to get started using it.

Before You Begin

  1. If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.

  2. Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.

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

Install NGINX

  1. Install NGINX from the package manager.

     sudo yum install nginx
    
  2. Enable and start the NGINX service.

     sudo systemctl enable nginx
     sudo systemctl start nginx
    
  3. Open port 80 — the HTTP port — on your system’s firewall. FirewallD is the front end typically used to manage firewall rules on AlmaLinux. You can use the following commands to open port 80 and reload the rules so they take effect.

     sudo firewall-cmd --zone=public --add-service=http
     sudo firewall-cmd --zone=public --add-service=http --permanent
     sudo firewall-cmd --reload
    

    Refer to our Introduction to FirewallD on CentOS for more on how to use FirewallD for managing your server’s firewall.

  4. Visit the default NGINX page to see your installation in action. You can find it by navigating to your server’s domain name or its IP address.

    For example, if your domain name is example.com, navigate to http://example.com; if your IP address is 192.0.2.0, you can instead navigate to http://192.0.2.0.

    Default NGINX page.

Manage NGINX

The NGINX service runs on systemd, which means you can manage it using systemctl commands.

  • View the current status of the NGINX service using the following command:

      sudo systemctl status nginx
    
  • Stop the NGINX service using the following command:

      sudo systemctl stop nginx
    

    You can then restart the NGINX service using the following command:

      sudo systemctl start nginx
    
  • To disable the NGINX service, preventing it from beginning automatically at system startup, use:

      sudo systemctl disable nginx
    

    You can enable the NGINX service again using:

      sudo systemctl enable nginx
    
  • Restart the NGINX service using the following command:

      sudo systemctl restart nginx
    
  • To reload NGINX’s configuration files, use the following command:

      sudo systemctl reload nginx
    

Using NGINX

This section walks you through setting up your own website using NGINX. It also illustrates how to set up an NGINX proxy to serve static content.

NGINX Configuration

  1. Open the main NGINX configuration file — /etc/nginx/nginx.conf — and disable the default website by commenting out the root line.

    File: /etc/nginx/nginx.conf
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    # [...]
    
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        # root         /usr/share/nginx/html;
    
    # [...]
        
  2. Create an NGINX configuration file for your site.

    • In this example, replace example.com with your site’s domain, in both the filename and the file’s contents. Do the same wherever you see example.com throughout this guide.

      File: /etc/nginx/conf.d/example.com.conf
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      
      server {
          listen 80;
          listen [::]:80;
          server_name  example.com;
      
          root /var/www/example.com;
          index index.html;
      
          location / {
              try_files $uri $uri/ =404;
          }
      }
          
    • This configuration creates a new NGINX server. That server listens for requests on port 80 for the domain name example.com. It then defines the server’s root directory and index file name. The root directory is where NGINX maps requests to files, and the index file name is the name of the file NGINX serves for a request to the root directory.

    • So, for a request to example.com/, NGINX attempts to locate an index.html file in the /var/www/example.com directory, and serves the file if it finds it there.

  3. Run NGINX’s configuration test to verify your configuration files.

     sudo nginx -t
    
  4. Restart NGINX for the changes to take effect.

     sudo systemctl restart nginx
    

Launch the Site

  1. Create a directory for your NGINX site’s content.

     sudo mkdir -p /var/www/example.com
    
  2. Create an index.html page in the new NGINX site directory.

    File: /var/www/example.com/index.html
    1
    2
    3
    4
    5
    6
    7
    8
    
    <!doctype html>
    <html>
    <body>
        <h1>Hello, World!</h1>
        <p>This is an example website running on NGINX.</p>
    </body>
    </html>
        
  3. Give the NGINX user ownership of the directory for your website, and then add executable permissions to its parent directory.

     sudo chown -R nginx:nginx /var/www/example.com
     sudo chmod -R +x /var/www
    
  4. Use the chcon command to change the SELinux security context for the website directory to allow 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
    
  5. In a browser, visit the domain you set up for your website — example.com above.

    You should see your website’s “Hello, World!” page.

    Example web page hosted on NGINX.

Conclusion

To learn more about NGINX’s features and capabilities, check out our A Comparison of the NGINX and Apache Web Servers guide.

For more advanced configuration options, including security and performance optimizations and TLS setup, see our four-part Getting Started series on NGINX.

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.