This guide explains how to install Nginx on Ubuntu, configure it to proxy a domain to localhost:3000, and set up SSL using Certbot for HTTPS encryption.
First, ensure that your package index is up to date:
sudo apt update
Install the Nginx web server:
sudo apt install nginx
Start the Nginx service and enable it to start on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
Create a New Configuration File Create a new configuration file for your domain:
sudo nano /etc/nginx/sites-available/yourdomain.com
Add the Following Configuration Add the following content to the file:
server {
listen 80;
server_name hellomm.com www.yourdomain.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Create a symbolic link to enable the configuration:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
Test the Nginx configuration for syntax errors:
sudo nginx -t
If the test is successful, you will see:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart Nginx to apply the changes:
sudo systemctl restart nginx
Point your domain (www.yourdomain.com) to your server's IP by setting an A record in your domain registrar's control panel.
If you are using UFW (Uncomplicated Firewall), allow HTTP and HTTPS traffic:
sudo ufw allow 'Nginx Full'
sudo ufw reload
Install Certbot and the Nginx plugin:
sudo apt install certbot python3-certbot-nginx
Run the following command to obtain and install the SSL certificate:
sudo certbot --nginx --non-interactive --keep-until-expiring --renew-with-new-domains --agree-tos --email [email protected] --no-eff-email --domains yourdomin.com,www.yourdomain.com
Certbot automatically sets up renewal via cron or systemd. To test certificate auto-renewal, run:
sudo certbot renew --dry-run
If the test is successful, Certbot will renew the certificate automatically as needed.
After following these steps, your domain www.yourdomain.com should be securely proxied to localhost:3000 with an SSL certificate installed and auto-renewal configured.
By following this guide, you have successfully set up Nginx on your Ubuntu server, configured it to proxy requests to localhost:3000, and secured your domain with an SSL certificate using Certbot.