Installing Nginx on your DigitalOcean Droplet allows you to host websites, manage traffic, and enhance your server’s capabilities. This guide will walk through installing and setting up Nginx on a Ubuntu DigitalOcean Droplet.
Prerequisites to Installing Nginx:
- DigitalOcean Droplet: An active Droplet running Ubuntu or a similar Linux distribution.
- SSH Access: Ability to connect to your Droplet via SSH using a non-root user with sudo privileges.
Step-by-Step Guide:
Step 1: Connect to Your Droplet via SSH
Use SSH to connect to your Droplet. Replace username with your sudo user and your_droplet_ip with your Droplet’s IP address:
ssh username@your_droplet_ip
Step 2: Update the Package Index
Before installing new software, update your package list to ensure you have the latest information:
sudo apt update
Step 3: Install Nginx
Install Nginx using the apt package manager:
sudo apt install nginx -y
// -y auto answers yes to installation
Step 4: Adjust Firewall Settings
If you have UFW (Uncomplicated Firewall) enabled, allow Nginx traffic:
Check Available Application Profiles:
# sudo ufw status
Expected Output:
Status: active
To Action From
-- ------ ----
Nginx Full ALLOW Anywhere
22/tcp ALLOW Anywhere
8080 ALLOW Anywhere
Nginx Full (v6) ALLOW Anywhere (v6)
22/tcp (v6) ALLOW Anywhere (v6)
8080 (v6) ALLOW Anywhere (v6)
Allow ‘Nginx Full’ Profile:
sudo ufw allow 'Nginx Full'
sudo ufw reload
Enable UFW (if not already enabled):
sudo ufw enable
Step 5: Verify Nginx Installation
To confirm that Nginx is running:
Check Service Status:
systemctl status nginx
Visit Your Droplet’s IP Address:
Navigate to http://your_droplet_ip
The default Nginx welcome page, indicating that the installation was successful.
Step 7: Configure Nginx Server Blocks (Virtual Hosts)
To host multiple websites or configure Nginx for your domain, set up server blocks:
Create a Directory for Your Domain:
Replace yourdomain.com with your actual domain name.
sudo mkdir -p /var/www/yourdomain.com/html
Set Ownership of the Directory:
sudo chown -R $USER:$USER /var/www/yourdomain.com/html
Create a Sample Index File:
nano /var/www/yourdomain.com/html/index.html
Add the following content:
<html>
<head>
<title>Welcome to YourDomain!</title>
</head>
<body>
<h1>Success! Nginx is working on your Droplet.</h1>
</body>
</html>
Save and exit the editor (Ctrl+x, Y, Enter).
Create an Nginx Server Block Configuration File:
sudo nano /etc/nginx/sites-available/yourdomain.com
Add the following configuration:
nginx
server {
listen 80;
listen [::]:80;
root /var/www/yourdomain.com/html;
index index.html;
server_name yourdomain.com www.yourdomain.com;
location / {
try_files $uri $uri/ =404;
}
}
Replace yourdomain.com with your actual domain.
Create a Symbolic Link & Enable the Server Block:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
Test Nginx Configuration for Syntax Errors:
sudo nginx -t
Expected Output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload Nginx to Apply Changes:
sudo systemctl reload nginx
Update DNS Records:
Ensure your domain’s DNS records point to your Droplet’s IP address.
Step 8: Secure Nginx with SSL (Recommended):
To enhance security, set up HTTPS using a free Let’s Encrypt SSL certificate:
Install Certbot and the Nginx Plugin:
sudo apt install certbot python3-certbot-nginx -y
Obtain and Install the SSL Certificate:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
- Follow the Prompts:
- Enter your email address.
- Agree to the terms of service.
Visit https://yourdomain.com to confirm that your site is accessible over HTTPS.