Views: 32
If you’re hosting your app on a Virtual Private Server (VPS), the next crucial step is to connect a custom domain, secure it with SSL, and make sure it’s always served over HTTPS.
In this tutorial, you’ll learn how to:
- Connect a domain registered with any domain registrar to your VPS
- Issue a free SSL certificate using Let’s Encrypt & Certbot
- Automate SSL renewals
- Force HTTP to HTTPS redirection with Nginx
✅ Step 1: Point Your Domain to Your VPS IP
- Log in to your domain registrar (e.g., Namecheap, DreamHost, GoDaddy).
- Go to your domain’s DNS Management panel.
- Add an A record:
Type: A Host/Name: @ Value: YOUR_VPS_IP_ADDRESS TTL: Automatic or 3600
Optional:Type: A Host/Name: www Value: YOUR_VPS_IP_ADDRESS
🛠 Step 2: Create Nginx Server Block
SSH into your VPS (example username: deployuser
, port: 2222
):
ssh deployuser@YOUR_VPS_IP -p 2222
Create a new Nginx config file:
sudo nano /etc/nginx/sites-available/example.com
Paste the following:
# Redirect HTTP to HTTPS
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
# HTTPS block with SSL
server {
listen 443 ssl;
server_name example.com www.example.com;
root /var/www/example-app;
index index.html;
location / {
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://localhost:8000/;
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;
}
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
Enable the site and reload Nginx:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
🔐 Step 3: Install Certbot and Get SSL Certificate
Install Certbot:
sudo apt update
sudo apt install certbot python3-certbot-nginx
Obtain SSL certificate:
sudo certbot --nginx -d example.com -d www.example.com
Certbot will automatically configure your Nginx SSL block and reload the server.
🔁 Step 4: Automate SSL Renewal
Certbot installs a systemd timer. You can confirm it’s scheduled with:
sudo systemctl list-timers | grep certbot
You can test it using:
sudo certbot renew --dry-run
No extra setup needed — the timer handles auto-renewals every 60 days.
🔁 Step 5: Force HTTPS Redirection
As shown in the Nginx config earlier, the HTTP block listens on port 80 and redirects everything to the HTTPS version. This improves both security and SEO.
🎉 Final Result
- Your domain now points to your VPS.
- It’s secured with a free SSL certificate from Let’s Encrypt.
- The SSL certificate automatically renews before expiration.
- All HTTP traffic is redirected to HTTPS.
This method works for any VPS provider (Hostinger, DigitalOcean, etc.) and any domain registrar (DreamHost, Namecheap, GoDaddy, etc.).