Setting up an Nginx Web Server
This guide covers the installation and configuration of Nginx, a powerful and widely-used web server. We’ll walk through installation, basic setup, and configuring a simple website on Ubuntu.
Prerequisites
To follow this guide, you’ll need:
- A server running Ubuntu (20.04 or newer is recommended)
- User access with
sudo
privileges
Step 1: Update Package List
First, update the system's package list to ensure you get the latest version of Nginx.
sudo apt update
Step 2: Install Nginx
Use the package manager to install Nginx:
sudo apt install nginx
Once installed, Nginx should start automatically. You can check its status using:
sudo systemctl status nginx
You should see output indicating that Nginx is active and running.
Step 3: Allow Nginx Through the Firewall
If your server has a firewall, you’ll need to allow HTTP and HTTPS traffic. To do this, run:
sudo ufw allow 'Nginx Full'
Check the status of the firewall to confirm the rules:
sudo ufw status
Step 4: Test the Installation
Open your web browser and navigate to the server's IP address to verify that Nginx is running. You should see the Nginx default welcome page.
http://your_server_ip
Step 5: Configure a Basic Website
Nginx uses configuration files located in /etc/nginx/
. You can create a new configuration file for each website. By default, Nginx has the configuration file /etc/nginx/sites-available/default
, but we’ll create a new configuration file for a sample website.
Create the Configuration File:
sudo nano /etc/nginx/sites-available/example.com
Add Basic Configuration:
Paste the following configuration into the file:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Replace example.com
with your domain name.
Create the Document Root:
Now, create the directory to hold the website files:
sudo mkdir -p /var/www/example.com
Add a Sample HTML File:
Create an index.html
file for testing:
sudo nano /var/www/example.com/index.html
Add some sample content to the file:
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is the home page for example.com.</p>
</body>
</html>
Enable the Configuration:
Create a symbolic link to enable the site configuration:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Test the Configuration and Restart Nginx:
Test the syntax of the configuration:
sudo nginx -t
If there are no errors, restart Nginx to apply the changes:
sudo systemctl reload nginx
Step 6: Access the Website
Open a web browser and navigate to http://example.com
to view your test page. If DNS is set up correctly, you should see your "Hello, World!" message.
Additional Configuration Options
Enabling HTTPS
To secure your website with HTTPS, you can use Let’s Encrypt for a free SSL certificate. Install Certbot and configure SSL with the following commands:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
Follow the prompts to complete the setup.
Conclusion
You've successfully set up an Nginx web server on your Ubuntu server, configured a basic website, and enabled HTTP access.