
Nginx is a high-performance, open-source web server that serves static and dynamic content, and also functions as a reverse proxy, load balancer, and HTTP cache. Its event-driven architecture handles thousands of concurrent connections efficiently, making it a popular choice for production deployments and application delivery.
This article explains how to install Nginx on an Ubuntu 26.04 server, configure a virtual host to serve a sample web application, secure the server with a trusted SSL certificate from Let's Encrypt, and set up firewall rules for HTTP and HTTPS traffic.
Before you begin, you need to:
app.example.com.The default Ubuntu 26.04 APT repositories include the Nginx package. The following steps update the package index and install the web server.
Update the APT package index.
Install Nginx.
Confirm the installed Nginx version.
Your output should be similar to the one below:
Nginx runs as a systemd service on Ubuntu, providing standard controls for starting, stopping, and restarting the web server process. The following steps enable automatic startup on boot and demonstrate the core service management commands.
Enable Nginx to start automatically at boot time.
Start the Nginx service.
Stop the Nginx service.
Restart the Nginx service.
Verify that Nginx is active and running.
Your output should be similar to the one below:
The Active: active (running) status confirms that Nginx is operational. If the status shows active (failed), stop any process occupying HTTP port 80 and restart the Nginx service.
Uncomplicated Firewall (UFW) comes preinstalled but is inactive by default on Ubuntu 26.04. Nginx requires open ports for HTTP and HTTPS traffic. The following steps allow SSH, HTTP, and HTTPS connections, then enable the firewall.
Allow SSH connections so that enabling the firewall does not drop your session and lock you out of the server.
Allow HTTP traffic on port 80.
Allow HTTPS traffic on port 443.
Enable the firewall.
Enter y when prompted to confirm. UFW starts immediately and re-enables automatically on boot.
Verify the active firewall rules.
Your output should be similar to the one below:
The web root directory stores the files that Nginx serves when a client requests the domain. The following steps create the directory structure and a sample HTML page.
Create the web root directory for the virtual host.
Create a sample index.html file in the web root directory.
Add the following HTML content to the file.
Save and close the file.
Virtual host configurations direct Nginx to serve specific content based on the requested domain name and the corresponding document root directory. The following steps define a server block for the sample domain, enable it, and verify that Nginx serves the expected content.
Create a new virtual host configuration file.
Add the following server block configuration.
Save and close the file.
This configuration instructs Nginx to listen on port 80 for requests matching the app.example.com domain and serve files from the /var/www/app.example.com directory.
Enable the virtual host by creating a symbolic link in the sites-enabled directory.
Test the Nginx configuration for syntax errors.
Restart Nginx to activate the new virtual host.
Verify the virtual host by sending a request to the domain using curl.
A successful response returns the HTML content:
SSL certificates encrypt traffic between a client browser and the Nginx web server over HTTPS. By default, Nginx listens on the unencrypted HTTP port 80. The following steps install Certbot and generate a trusted Let's Encrypt SSL certificate to enable HTTPS on your virtual host.
Install the Certbot Let's Encrypt client and the Nginx plugin.
Verify the installed Certbot version.
Your output should be similar to the one below:
Generate an SSL certificate for your domain. Replace app.example.com with your actual domain name.
Follow the on-screen prompts to complete the certificate generation. Certbot automatically configures the Nginx virtual host to use the new SSL certificate.
Verify that HTTPS is active by sending a request to your domain. Replace app.example.com with your actual domain name.
A successful response returns the HTML content served over HTTPS:
You have installed and configured Nginx on an Ubuntu 26.04 server with a virtual host, secured it with a Let's Encrypt SSL certificate, and configured firewall rules for HTTP and HTTPS traffic. Nginx supports multiple virtual hosts and integrates with backend services such as PHP-FPM and database servers for dynamic application delivery. For more configuration options, refer to the official Nginx documentation.
0 Comments
Be the first to comment and share your perspective with the community.