Nginx (pronounced “engine-ex”) is a high-performance web server, reverse proxy, and load balancer. It was specifically designed to solve the “C10k problem”—the challenge of handling 10,000 concurrent connections—and uses an asynchronous, event-driven architecture that makes it far less memory-intensive than Apache. Whether you are hosting a simple static HTML site or load-balancing a cluster of Node.js applications, Nginx is the modern standard for web delivery on Ubuntu.
Step 1: Install Nginx
Nginx is available directly from the default Ubuntu repositories, making installation straightforward.
- Open your terminal and update the package index:
sudo apt update - Install the Nginx package:
sudo apt install nginx - Press
Ywhen prompted to confirm the installation.
Once the installation finishes, the Nginx service will start automatically.
Step 2: Adjust the Firewall
If you are using the Uncomplicated Firewall (UFW) on Ubuntu, you must grant Nginx permission to accept incoming HTTP and HTTPS traffic.
sudo ufw allow 'Nginx Full'
You can verify the change by running sudo ufw status.
Step 3: Verify the Web Server is Running
To ensure everything is working correctly, check the status of the systemd service:
systemctl status nginx
The output should indicate that the service is active (running). You can also test it by opening a web browser and navigating to your server’s public IP address (or http://localhost if you are on the same machine). You should see the default “Welcome to nginx!” landing page.
Step 4: Understand the Configuration Structure
To host your own websites, you need to understand how Nginx organizes its configuration files on Ubuntu:
/etc/nginx/nginx.conf: This is the primary configuration file. It contains global settings that affect the entire server, such as worker process limits and logging formats. You rarely need to edit this file for basic hosting./etc/nginx/sites-available/: This directory contains individual configuration files (Server Blocks) for every website you want to host. However, Nginx ignores files in this directory until they are activated./etc/nginx/sites-enabled/: This directory contains symbolic links to the files insites-available. When Nginx starts, it only reads configurations from this folder.
Step 5: Create a Basic Server Block (Virtual Host)
To host a custom website (e.g., example.com), you create a Server Block.
- Create the document root directory for your website:
sudo mkdir -p /var/www/example.com/html - Create a basic HTML file inside it to test:
sudo nano /var/www/example.com/html/index.html
(Type some sample text like<h1>Hello World!</h1>and save). - Create a new configuration file in
sites-available:
sudo nano /etc/nginx/sites-available/example.com - Add the following minimal configuration block:
server { listen 80; server_name example.com www.example.com; root /var/www/example.com/html; index index.html; location / { try_files $uri $uri/ =404; } } - Enable the site by creating a symbolic link to the
sites-enableddirectory:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Finally, test the configuration for syntax errors by running sudo nginx -t. If the test passes, reload Nginx (sudo systemctl reload nginx) to apply the new configuration. Your Ubuntu server is now actively hosting your custom website.