Nginx Web Server

How To Configure The Nginx Web Server On Ubuntu 24.04

Welcome back to the Greenhost.cloud blog! Today, we’re diving into one of the most popular web servers in the world: Nginx. Known for its high performance, stability, rich feature set, and low resource consumption, Nginx is a favorite among developers and system administrators alike. In this post, we’ll guide you through the steps to configure Nginx on Ubuntu 24.04, ensuring your web applications run smoothly and efficiently.

Prerequisites

Before we get started, ensure you have the following:

  1. Ubuntu 24.04 Server: You should have a fresh installation of Ubuntu 24.04. A cloud server or a local machine will work.
  2. Sudo Access: You will need a user account with sudo privileges to install and configure Nginx.
  3. Basic Command Line Skills: Familiarity with the terminal will be beneficial.

Step 1: Update Your System

It’s always a good practice to update your package list and upgrade your system before installing new software. Open your terminal and run:

sudo apt update
sudo apt upgrade -y

Step 2: Install Nginx

Now, let’s install Nginx. You can easily do this using the package manager:

sudo apt install nginx -y

Once the installation is complete, we can verify that Nginx is running.

Step 3: Start and Enable Nginx

After installation, Nginx should start automatically. You can check the status of the Nginx service with:

sudo systemctl status nginx

If it’s not running, you can start it using:

sudo systemctl start nginx

To ensure that Nginx starts automatically at boot, run:

sudo systemctl enable nginx

Step 4: Adjust Firewall Settings

If you have a firewall running, you’ll need to allow HTTP and HTTPS traffic. Use the following commands to allow this traffic through UFW (Uncomplicated Firewall):

sudo ufw allow 'Nginx Full'

You can verify the changes with:

sudo ufw status

Step 5: Test Nginx

To confirm that Nginx is installed and running, open a web browser and type in your server’s IP address. You should see the default Nginx welcome page, indicating that the server is up and running.

Step 6: Configure Nginx

Now that Nginx is running, let’s configure it. The main configuration file for Nginx is located at /etc/nginx/nginx.conf. However, for best practices, we will create a new configuration file for your site.

Create a New Server Block

  1. Navigate to the Nginx sites-available directory: cd /etc/nginx/sites-available/
  2. Create a new configuration file for your website. Replace example.com with your domain name: sudo nano example.com
  3. Add the following configuration block: server { listen 80; server_name example.com www.example.com;root /var/www/example.com/html; index index.html index.htm; location / { try_files $uri $uri/ =404; }}
  4. Save and exit the file (Ctrl + X, then Y, and Enter).

Create the Document Root

Next, create the document root directory where your website files will reside:

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

Set the correct permissions:

sudo chown -R $USER:$USER /var/www/example.com/html

You can create a simple HTML file to test:

echo "<h1>Welcome to Nginx on Ubuntu 24.04</h1>" | sudo tee /var/www/example.com/html/index.html

Enable the Server Block

To enable your new server block, create a symbolic link to the sites-enabled directory:

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

Test Nginx Configuration

Before reloading Nginx, it’s important to test the configuration file for syntax errors:

sudo nginx -t

If everything is set up correctly, you should see a message indicating that the syntax is okay.

Reload Nginx

Finally, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 7: Set Up SSL (Optional but Recommended)

For secure connections, consider setting up SSL. You can use Let’s Encrypt to obtain a free SSL certificate. Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Then run:

sudo certbot --nginx -d example.com -d www.example.com

Follow the prompts to complete the SSL setup.

Conclusion

Congratulations! You’ve successfully configured Nginx on Ubuntu 24.04. You can now host your web applications with a server that is highly efficient and reliable. Remember, this is just the beginning. Nginx has many advanced features, including load balancing, caching, and reverse proxying, which you can explore as your needs grow.