How to Install Nginx on DigitalOcean | Code Boost
Description: 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 setti...

Article

# How to Install Nginx on DigitalOcean

By Matt · Dec 3, 2024

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:

1. bash
2. sudo ufw app list
3. You should see profiles like Nginx Full, Nginx HTTP, and Nginx HTTPS.

Allow 'Nginx Full' Profile:

1. bash
2. sudo ufw allow 'Nginx Full'

Enable UFW (if not already enabled):

1. bash
2. sudo ufw enable

#### Step 5: Verify Nginx Installation

To confirm that Nginx is running: Check Service Status:

1. bash
2. systemctl status nginx
3. You should see that the service is active (running).

Visit Your Droplet's IP Address:

1. Navigate to http://your_droplet_ip.
2. 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:

1. Create a Directory for Your Domain:
2. Replace yourtestdomain.com with your actual domain name.
3. bash
4. sudo mkdir -p /var/www/yourdomain.com/html

Set Ownership of the Directory:

1. bash
2. sudo chown -R $USER:$USER /var/www/yourdomain.com/html

Create a Sample Index File:

1. bash
2. nano /var/www/yourdomain.com/html/index.html
3. Add the following content:
4. html
5. <html>
6. <head>
7. <title>Welcome to YourDomain!</title>
8. </head>
9. <body>
10. <h1>Success! Nginx is working on your Droplet.</h1>
11. </body>
12. </html>
13. Save and exit the editor (Ctrl+O, Enter, Ctrl+X).

Create an Nginx Server Block Configuration File:

1. bash
2. sudo nano /etc/nginx/sites-available/yourdomain.com
3. Add the following configuration:
4. nginx
5. server {
6. listen 80;
7. listen [::]:80;
8. root /var/www/yourdomain.com/html;
9. index index.html;
10. server_name yourdomain.com www.yourdomain.com;
11. location / {
12. try_files $uri $uri/ =404;
13. }
14. }
15. Replace yourdomain.com with your actual domain.

Create a Symbolic Link & Enable the Server Block:

1. bash
2. sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

Test Nginx Configuration for Syntax Errors:

1. bash
2. sudo nginx -t
3. If the test is successful, proceed to the next step.

Reload Nginx to Apply Changes:

1. bash
2. sudo systemctl reload nginx

Update DNS Records (If Necessary):

1. 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:

1. Install Certbot and the Nginx Plugin:
2. bash
3. sudo apt install certbot python3-certbot-nginx -y
4. Obtain and Install the SSL Certificate:
5. bash
6. sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
7. Follow the Prompts:
   - Enter your email address.
   - Agree to the terms of service.
   - Choose whether to redirect HTTP traffic to HTTPS.
8. Verify HTTPS Access:
9. 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.