How to Install Nginx on DigitalOcean
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
Click here for $200 of DigitalOcean credit
- 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.
- Basic Command-Line Knowledge: Familiarity with Linux terminal commands.
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:
bash
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:
bash
sudo apt update
Step 3: Install Nginx
Install Nginx using the apt package manager:
bash
sudo apt install nginx -y
The -y flag automatically answers “yes” to any prompts during the installation.
Step 4: Adjust Firewall Settings
If you have UFW (Uncomplicated Firewall) enabled, allow Nginx traffic:
Check Available Application Profiles:
- bash
- sudo ufw app list
- You should see profiles like Nginx Full, Nginx HTTP, and Nginx HTTPS.
Allow ‘Nginx Full’ Profile:
- bash
- sudo ufw allow ‘Nginx Full’
Enable UFW (if not already enabled):
- bash
- sudo ufw enable
Step 5: Verify Nginx Installation
To confirm that Nginx is running:
Check Service Status:
- bash
- systemctl status nginx
- You should see that the service is active (running).
Visit Your Droplet’s IP Address:
- Navigate to http://your_droplet_ip.
- The default Nginx welcome page, indicating that the installation was successful.
Step 6: Manage Nginx Service (Optional)
Basic commands to control the Nginx service:
- Stop Nginx:
- bash
- sudo systemctl stop nginx
- Start Nginx:
- bash
- sudo systemctl start nginx
- Restart Nginx:
- bash
- sudo systemctl restart nginx
- Reload Nginx (for configuration changes):
- bash
- sudo systemctl reload nginx
- Enable Nginx to Start on Boot:
- bash
- sudo systemctl enable nginx
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 yourtestdomain.com with your actual domain name.
- bash
- sudo mkdir -p /var/www/yourdomain.com/html
Set Ownership of the Directory:
- bash
- sudo chown -R $USER:$USER /var/www/yourdomain.com/html
Create a Sample Index File:
- bash
- nano /var/www/yourdomain.com/html/index.html
- Add the following content:
- html
- <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+O, Enter, Ctrl+X).
Create an Nginx Server Block Configuration File:
- bash
- 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:
- bash
- sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
Test Nginx Configuration for Syntax Errors:
- bash
- sudo nginx -t
- If the test is successful, proceed to the next step.
Reload Nginx to Apply Changes:
- bash
- sudo systemctl reload nginx
Update DNS Records (If Necessary):
- Ensure your domain’s DNS records point to your Droplet’s IP address.
Step 8: Secure Nginx with SSL (Optional but Recommended)
To enhance security, set up HTTPS using a free Let’s Encrypt SSL certificate:
- Install Certbot and the Nginx Plugin:
- bash
- sudo apt install certbot python3-certbot-nginx -y
- Obtain and Install the SSL Certificate:
- bash
- sudo certbot –nginx -d yourdomain.com -d www.yourdomain.com
- Follow the Prompts:
- Enter your email address.
- Agree to the terms of service.
- Choose whether to redirect HTTP traffic to HTTPS.
- Verify HTTPS Access:
- Visit https://yourdomain.com to confirm that your site is accessible over HTTPS.
Troubleshooting Tips
- Firewall Issues: If you can’t access your site, ensure that the firewall allows HTTP (port 80) and HTTPS (port 443) traffic.
- Nginx Configuration Errors: If Nginx fails to start or reload, check the syntax in your configuration files and look for error messages using sudo nginx -t.
- Permission Denied: Ensure that the user has the correct permissions for the web root directory.
Conclusion
You’ve successfully installed Nginx on your DigitalOcean Droplet and configured it to serve web content. Nginx is now ready to handle web traffic, and you can proceed to deploy your websites or applications.
Next Steps
- Deploy Applications: Install and configure web applications or frameworks (e.g., WordPress, Django).
- Optimize Nginx Performance: Tweak Nginx settings for better performance and resource utilization.
- Implement Security Measures: Harden your server by configuring firewalls, fail2ban, and keeping software up to date.