How To Install Nginx on Rocky Linux 9 | Full Steps

Posted on

How To Install Nginx on Rocky Linux 9 | Full Steps

How To Install Nginx on Rocky Linux 9 | Full Steps

This guide aims to teach you How To Install Nginx on Rocky Linux 9. Furthermore, you will discover how to Set up Nginx Server Blocks on Rocky Linux 9 (Blue Onyx). This comprehensive guide focuses on providing you with the knowledge and skills to effectively manage your web server environment.

NGINX is a web server that is commonly used as a reverse proxy. It exhibits excellent scalability, functioning efficiently both as a web server and a reverse proxy. Unlike traditional models that allocate a process to each connection, Nginx creates a process pool that is shared among multiple connections within the network. This design allows for efficient resource utilization and the ability to handle a large number of concurrent connections. When a request is made, a resource is allocated from the pool to the process, optimizing resource allocation.

Nginx also plays a crucial role in establishing secure connections between your data centers and external networks. Additionally, it functions effectively as an HTTP load balancer, providing you with the flexibility to implement various load-sharing mechanisms. This capability is essential for maintaining high availability and performance in your web infrastructure. The steps outlined below will guide you through the installation and configuration process, empowering you to leverage the full potential of Nginx on your Rocky Linux 9 system. Understanding How To Install Nginx on Rocky Linux 9 is crucial for modern web development.

Steps To Install Nginx Web Server on Rocky Linux 9 Blue Onyx

Before proceeding, ensure that you are logged in to your server as a non-root user with sudo privileges and that you have a basic firewall configured. You can refer to our guide, Initial Server Setup with Rocky Linux 9, for detailed instructions on setting up the initial server environment.

Additionally, you will need a domain name that is pointed to your server’s IP address.

Now, follow the steps outlined below to complete this tutorial.

1. Install Nginx on Rocky Linux 9

First, update your local package index using the following command:

sudo dnf update -y

Next, install the Nginx web server on Rocky Linux 9 using the following command:

sudo dnf install nginx -y

Start and Enable Nginx on Rocky Linux 9

By default, Nginx is not enabled on Rocky Linux 9. Therefore, you need to manually start and enable Nginx using the following command:

sudo systemctl enable nginx --now

You can verify that your Nginx service is active and running on your server by executing the following command:

sudo systemctl status nginx

The output will resemble the following:

**Output**
 nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor prese>
Active: **active** (**running**) since Wed 2022-10-26 08:51:53 EDT; 6s ago
Process: 74025 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 74023 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 74021 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, statu>
...

Configure Firewall For Nginx Web Server

We assume that you have already enabled firewalld as part of the initial server setup.

Now, enable HTTP and HTTPS connections using the following commands:

# sudo firewall-cmd --permanent --zone=public --add-service=http
# sudo firewall-cmd --permanent --zone=public --add-service=https

Reload the firewall to apply the new rules:

sudo firewall-cmd --reload

At this point, your Nginx web server is installed on Rocky Linux 9 and configured to allow web traffic.

Access Default Nginx Landing Page

Verify that your Nginx server is running by accessing your server’s public IP address in a web browser. If you do not know your server’s public IP address, you can obtain it using the following command:

hostname -I

Alternatively, you can use the curl tool to retrieve your IP address from icanhazip.com:

curl -4 icanhazip.com

Then, type your IP address in your web browser:

http://your_server_IP_address

If you see the default Nginx landing page, it indicates that your Nginx web server is correctly installed and running on Rocky Linux 9.

Manage Nginx Service on Rocky Linux 9

Now that your web server is operational, you can manage the Nginx process on Rocky Linux 9.

To stop your web server, use the following command:

sudo systemctl stop nginx

To start the Nginx web server after stopping it, use the following command:

sudo systemctl start nginx

To stop and then immediately start the service again, use the following command:

sudo systemctl restart nginx

Whenever you make configuration changes to Nginx, you need to reload the service. To do this, run the following command:

sudo systemctl reload nginx

Nginx is configured to start automatically when the server boots. If you want to prevent this, run the following command:

sudo systemctl disable nginx

To re-enable Nginx to start at boot, use the following command:

sudo systemctl enable nginx

Now, you can proceed to set up server blocks to host multiple websites on the same Nginx web server on Rocky Linux 9.

2. Set up Nginx Server Block on Rocky Linux 9

Nginx server blocks function similarly to Apache virtual hosts, enabling a single server to respond to multiple domain names and serve different content for each.

To create an Nginx Server block on Rocky Linux 9, follow the steps below.

First, create a directory for your domain using the following command:

sudo mkdir -p /var/www/example.com/

Then, assign ownership of the directory to the nginx user:

sudo chown -R nginx:nginx /var/www/example.com/

Create a sample index.html page

Now, create a sample index.html page to test the server block configuration. You can use your preferred text editor, such as vi:

sudo vi /var/www/example.com/index.html

Add the following HTML code to the file:

<html>
<head>
<title>Welcome to example.com</title>
</head>
<body>
<h1>Success! Your Nginx server is successfully configured for <em>example.com</em>. </h1>
<p>This is a sample page.</p>
</body>
</html>

Save and close the file.

Create Nginx Server Block on Rocky Linux 9

Creating an Nginx server block on Rocky Linux 9 is similar to creating Apache server blocks. First, ensure that the necessary directories exist. If they don’t, run the command below:

sudo mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled

Now, create the server block configuration file for your website using any text editor:

sudo vi /etc/nginx/sites-available/example.com.conf

Paste the following configuration block into the file:

server {
 listen 80;
 listen [::]:80;

 root /var/www/example.com/;

  index index.html index.htm index.nginx-debian.html;
  server_name example.com www.example.com;

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

Save and close the file.

Enable the Nginx Server block on Rocky Linux 9 by creating a symbolic link:

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

Edit Nginx Configuration File on Rocky Linux 9

Now, make configuration changes to the default Nginx configuration file. Open the file with your preferred text editor:

sudo vi /etc/nginx/nginx.conf

Uncomment the following line within the HTTP{} section of the nginx.conf configuration file:

server_names_hash_bucket_size 64;

Add the sites-enabled path to your Nginx configuration file. Locate the existing include /etc/nginx/conf.d/*.conf; line and add the following line directly below it:

include /etc/nginx/sites-enabled/*.conf;

Save and close the file.

Run the following command to ensure that there are no syntax errors in your Nginx configuration files:

sudo nginx -t

The output should resemble the following:

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

If the test fails, a common issue is a missing or incorrect symlink. Navigate to your /etc/nginx/sites-enabled/ directory:

cd /etc/nginx/sites-enabled/

List the files using the ls command:

ls

Remove any unnecessary configuration files, including the default file:

sudo rm default

Re-run the test. If it passes, restart the Nginx web server on Rocky Linux 9:

sudo systemctl restart nginx

Test Nginx Server Blocks

Test your custom domain setup by typing your domain name in your web browser:

http://your_domain_name

If you see the page you created, your Nginx server is correctly configured to serve your domain.

For more information, you can visit the Nginx Documentation page.

Conclusion

At this point, you have successfully learned How To Install Nginx on Rocky Linux 9 and how to set up Nginx Server Blocks. Installing Nginx and configuring Server Blocks enables you to host multiple websites on a single server. This setup enhances speed, improves security, and simplifies site management. Knowing How To Install Nginx on Rocky Linux 9 can greatly improve the capabilities of your server.

Hope you enjoy it. You may also like these articles:

Install Nginx Web Server on AlmaLinux 9

Install Nginx Web Server on Ubuntu 22.04

Set up Nginx Reverse Proxy on Ubuntu 24.04

Install Nginx Proxy Manager on Ubuntu 22.04

Alternative Solutions for Deploying Web Applications on Rocky Linux 9

While the guide focuses on directly installing and configuring Nginx on Rocky Linux 9, alternative solutions provide different levels of abstraction and management capabilities. Here are two such alternatives:

1. Using Docker and Docker Compose

Docker allows you to containerize your web application and its dependencies, ensuring consistent behavior across different environments. Docker Compose simplifies the management of multi-container Docker applications.

Explanation:

Instead of installing Nginx directly on the host system, you can create a Docker container that includes Nginx and your web application. This container can then be deployed on any system that has Docker installed. Docker Compose allows you to define and manage multiple containers (e.g., Nginx, application server, database) as a single unit.

Steps:

  1. Create a Dockerfile for your Nginx container:

    FROM nginx:latest
    
    # Remove default Nginx configuration
    RUN rm /etc/nginx/conf.d/default.conf
    
    # Copy your custom Nginx configuration
    COPY nginx.conf /etc/nginx/conf.d/
    
    # Copy your website files
    COPY website /usr/share/nginx/html
  2. Create an nginx.conf file (similar to the server block configuration in the original article):

    server {
        listen 80;
        server_name example.com www.example.com;
        root /usr/share/nginx/html;
        index index.html;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
  3. Create a docker-compose.yml file:

    version: "3.8"
    services:
      web:
        build: .
        ports:
          - "80:80"
        volumes:
          - ./website:/usr/share/nginx/html
        restart: always
  4. Place your website files in a directory named website:

    This directory should contain your index.html and any other static assets.

  5. Build and run the Docker Compose application:

    docker-compose up --build

Benefits:

  • Isolation: Your application runs in a container, isolated from the host system.
  • Reproducibility: The Dockerfile ensures that your application can be built and deployed consistently across different environments.
  • Scalability: Docker makes it easier to scale your application by running multiple containers.

2. Using a Web Hosting Control Panel (e.g., CyberPanel)

Web hosting control panels provide a graphical interface for managing your web server, including installing Nginx, configuring virtual hosts, and managing databases.

Explanation:

CyberPanel is an open-source web hosting control panel that supports Nginx. It simplifies the process of installing and configuring Nginx, as well as other common web server tasks.

Steps:

  1. Install CyberPanel on your Rocky Linux 9 server:

    Follow the instructions on the CyberPanel website for installing the control panel.

  2. Log in to the CyberPanel interface:

    Access the CyberPanel interface through your web browser.

  3. Create a new website:

    Use the CyberPanel interface to create a new website, specifying the domain name, document root, and other settings.

  4. Upload your website files:

    Upload your website files to the document root directory specified in the previous step.

  5. Configure DNS records:

    Point your domain name to your server’s IP address.

Benefits:

  • Simplified management: CyberPanel provides a graphical interface for managing your web server, making it easier to perform common tasks.
  • Automation: CyberPanel automates many of the tasks involved in setting up and managing a web server, such as installing Nginx, configuring virtual hosts, and managing databases.
  • Security: CyberPanel includes security features such as a firewall and automatic updates.

These alternative solutions offer different approaches to deploying web applications on Rocky Linux 9, each with its own advantages and disadvantages. Docker provides isolation and reproducibility, while CyberPanel simplifies management and automation. The choice of solution depends on your specific needs and preferences. Knowing How To Install Nginx on Rocky Linux 9 provides a solid foundation for understanding these more complex deployment strategies.