This guide will walk you through the steps to install Nginx on an Ubuntu server, set up a domain to proxy to localhost:3000, and install an SSL certificate using Certbot.
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
Ensure that your domain www.yourdomain.com points to the IP address of your Ubuntu server through 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 youremail@gmail.com --no-eff-email --domains yourdomin.com,www.yourdomain.com
Certbot sets up a cron job or systemd timer to renew the certificate automatically before it expires. Test the auto-renewal process:
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.