---
title: How to Install Nginx Webserver on Rocky Linux 9
url: https://docs.vultr.com/how-to-install-nginx-webserver-on-rocky-linux-9
description: Learn how to install and configure Nginx webserver on Rocky Linux 9 with step-by-step instructions for a secure and efficient web hosting environment.
publish_date: 2025-01-17T11:39:53.713877Z
last_updated: 2025-01-17T11:39:53.713877Z
author: Vishwas Agrawal
header_image: https://docs.vultr.com/public/doc-assets/2028/3df7a104-4316-4bb5-b33f-9820ea11348d.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

## Introduction  

Nginx is an open-source, high-performance web server application widely used for hosting websites, reverse proxying, and load balancing to efficiently manage high traffic. It supports modern protocols like HTTP/2 and HTTPS, enabling the secure serving of static and dynamic web content, caching, and traffic encryption with SSL/TLS. To leverage its flexibility, scalability, and reliability, you can easily install Nginx webserver on Rocky Linux 9, making it a preferred choice for a wide range of web applications, from small websites to large-scale enterprise solutions.

This article explains how to install the Nginx webserver on Rocky Linux 9 and set up virtual hosts to run web applications on the server.

## Install Nginx on Rocky Linux 9 

Nginx is available in the default package repositories on Rocky Linux 9. Follow the steps below to update the server's package index and install Nginx using the `dnf` package manager.

1. Update the server's package information index.

    ```console
    $ sudo dnf update
    ```

1. Install Nginx.

    ```console
    $ sudo dnf install nginx -y
    ```  

1. View the installed Nginx version.

    ```console
    $ sudo nginx -version
    ```

    Output:

    ```
    nginx version: nginx/1.20.1
    ```

1. Allow network connections to the HTTP port `80` through the default firewall.

    ```console
    $ sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
    ```  

1. Reload `firewall-cmd` to apply the firewall changes.

    ```console
    $ sudo firewall-cmd --reload
    ```

## Manage the Nginx System Service  

The `nginx` systemd service profile controls the webserver's runtime and processes on a server. Follow the steps below to enable the Nginx system service to start at boot and manage it on your server.

1. Enable the Nginx service to automatically start at boot.

    ```console
    $ sudo systemctl enable nginx
    ```

    Output:

    ```
    Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
    ```

1. Start the Nginx service.

    ```console
    $ sudo systemctl start nginx
    ```

1. View the Nginx service status and verify that it's running.

    ```console
    $ sudo systemctl status nginx
    ```

    Output:

    ```
    ● nginx.service - A high performance webserver and a reverse proxy server
         Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enabled)
         Active: active (running) since Sat 2024-08-24 07:00:58 UTC; 1min 35s ago
           Docs: man:nginx(8)
        Process: 2560 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
        Process: 2561 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
       Main PID: 2584 (nginx)
          Tasks: 2 (limit: 1091)
         Memory: 1.7M
            CPU: 12ms
         CGroup: /system.slice/nginx.service
                 ├─2584 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
                 └─2585 "nginx: worker process"
    ```

    The Nginx service is active and running based on the above `Active: active (running)` output.

1. Stop the Nginx service.

    ```console
    $ sudo systemctl stop nginx
    ```

1. Restart the Nginx service.

    ```console
    $ sudo systemctl restart nginx
    ```

1. Access your server's IP using a web browser such as Chrome to verify that the default Nginx page displays.

    ```
    http://SERVER-IP
    ```

    ![Nginx default page](https://docs.vultr.com/public/doc-assets/2028/b74c7620-40af-4ad9-b8ec-014fa5b9fd4f.png)

## Create a New Nginx Virtual Host Configuration

Nginx virtual host configurations enable the webserver to map web application directories to specific network addresses or domains on a server. A virtual host configuration consists of multiple directives that include the server name, webroot directory, log paths, and index pages used to serve web applications. Follow the steps below to create a new Nginx virtual host to serve a basic HTML web application using the `app.example.com` domain.

1. Create a new virtual host webroot directory such as `app.example.com`.

    ```console
    $ sudo mkdir -p /var/www/html/app.example.com
    ```  

1. Create a new `index.html` web application file in the directory.

    ```console
    $ sudo nano /var/www/html/app.example.com/index.html
    ```

1. Add the following HTML application contents to the file.  

    ```html
    <html>
         <head></head>
         <body>
             <h1>Greetings from Vultr</h1>
         </body>
     </html>
    ```  

    Save and close the file.

    The above HTML web application displays a "Greetings from Vultr" message when accessed using the `app.example.com` virtual host domain.

1. Grant the `nginx` webserver user and group ownership privileges to the webroot directory.

    ```console
    $ sudo chown -R nginx:nginx /var/www/html/app.example.com
    ```

1. Create a new `app.example.com.conf` Nginx virtual host configuration in the `/etc/nginx/conf.d` directory.

    ```console
    $ sudo nano /etc/nginx/conf.d/app.example.com.conf
    ```  

1. Add the following configurations to the file. Replace `app.example.com` with your actual domain.

    ```nginx
    server {
        listen 80;
        listen [::]:80;
        server_name app.example.com;

        root /var/www/html/app.example.com;
        index index.html;

        location / {
            try_files $uri $uri/ =404;
        }
    }
    ```  

    Save and close the file.

    The above Nginx configuration creates a new virtual host that listens for incoming connections using the `app.example.com` domain and delivers web application files from the `/var/www/html/app.example.com` webroot directory.

1. Test the Nginx configuration for errors.

    ```console
    $ sudo nginx -t
    ```  

    Output:

    ```
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    ```

1. Reload the Nginx service to apply the configuration changes.

    ```console
    $ sudo systemctl reload nginx
    ```

1. View the SELinux status on your server and verify that it's set to `Enforcing`.

    ```console
    $ getenforce
    ```

    Output:

    ```
    Enforcing
    ```

1. Set the `httpd_sys_content_t` context on the webroot directory to allow Nginx to read the directory files.

    ```console
    $ sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/html/app.example.com(/.*)?"
    ```

1. Apply the SELinux context.

    ```console
    $ sudo restorecon -R /var/www/html/app.example.com
    ```

1. Access your domain in a new web browser window and verify that the HTML web application displays.

    ```
    http://app.example.com
    ```

    ![web application output](https://docs.vultr.com/public/doc-assets/2028/f654e91d-a270-46f3-bffe-0c93b1371770.png)

## Set Up Firewall Rules  

[Firewalld](https://docs.vultr.com/how-to-set-up-firewall-policies-using-firewalld) is available and active on Vultr Rocky Linux instances by default. Follow the steps below to configure the firewall to allow incoming HTTP and HTTPS network connections to the server.

1. Allow HTTP service through the firewall.

    ```console
    $ sudo firewall-cmd --permanent --add-service=http
    ```

1. Allow HTTPS service through the firewall.

    ```console
    $ sudo firewall-cmd --permanent --add-service=https
    ```

1. Reload the firewall to apply the changes.

    ```console
    $ sudo firewall-cmd --reload
    ```  

1. List all active firewall rules and verify that HTTP and HTTPS connections are allowed.

    ```console
    $ sudo firewall-cmd --list-all
    ```  

    Output:

    ```
    public (active)
      target: default
      icmp-block-inversion: no
      interfaces: enp1s0
      sources: 
      services: cockpit dhcpv6-client http https ssh
      ports: 
      protocols: 
      forward: yes
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules:
      ```

## Secure the Nginx Web Server

SSL certificates encrypt network connections between a user's web browser and the Nginx web server using HTTPS. Nginx listens for incoming connections using HTTP which is insecure by default. To secure network connections to the server using HTTPS, generate SSL certificates using a trusted certificate authority such as Let's Encrypt. Follow the steps to install the Certbot Let's Encrypt client and generate trusted SSL certificates to secure the Nginx web server.

1. Install the Certbot Let's Encrypt client tool and the Nginx plugin.

    ```console
    $ sudo dnf install certbot python3-certbot-nginx -y
    ```  

1. Generate new SSL certificates using the Nginx plugin. Replace `app.example.com` with your actual domain and `hello@example.com` with your active email address.

    ```console
    $ sudo certbot --nginx -d app.example.com -m hello@example.com --agree-tos
    ```  

1. Verify that Certbot autorenews the SSL certificate before expiry.

   ```console
   $ sudo certbot renew --dry-run
   ```  

1. Restart Nginx to apply the configuration changes.

    ```console
    $ sudo systemctl restart nginx
    ```

1. Access your `app.example.com` virtual host domain using HTTPS and verify that your web application loads correctly.
    
    ```
    https://app.example.com
    ```

    ![web application output](https://docs.vultr.com/public/doc-assets/2028/626f513b-e3f1-49c7-b9a6-88c4eed57675.png)

## Conclusion  

You have installed the Nginx web server on Rocky Linux 9 and created virtual host configurations to deliver web applications on the server. You can integrate Nginx with other application frameworks such as PHP to deliver dynamic web applications. In addition, you can use Nginx as a webserver, load balancer, or reverse proxy to securely serve backend applications. For more information and configuration options, visit the [Nginx documentation](https://nginx.org/en/docs/).
