Building a website on your own domain (Nginx, free HTTPS)
Here's how to install a web server (Nginx) on an Ubuntu server, connect your domain, and then add HTTPS (the padlock) with a free certificate.
1. Install Nginx
sudo apt update
sudo apt install -y nginx
If you've turned on the firewall (ufw), open the web ports. (Opening firewall ports)
sudo ufw allow 'Nginx Full'
Go to http://server-ip in your browser; if you see the "Welcome to nginx!" page, it worked.
2. Connect your domain
Add records in the DNS management menu of the place you bought your domain from (Gabia, Hosting.kr, Cloudflare, etc.).
| Type | Host (name) | Value |
|---|---|---|
| A | @ | Server IP |
| A | www | Server IP |
DNS changes usually take a few minutes, and sometimes a few hours, to take effect. If the command below on your PC shows your server IP, it's connected.
nslookup example.com
3. Set up your site
Create a folder for your web files and write the Nginx configuration file. (Replace example.com with your domain.)
sudo mkdir -p /var/www/example.com
echo '<h1>Hello Nevv!</h1>' | sudo tee /var/www/example.com/index.html
sudo nano /etc/nginx/sites-available/example.com
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
}
Enable the configuration and reload Nginx.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t # Check the configuration syntax — it should say "ok"
sudo systemctl reload nginx
If you want to connect a Node.js or Python app
If your app is running on the server on, for example, port 3000, put the following in place of the root and index lines in the configuration above.
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
4. Add HTTPS (free certificate)
Use certbot, which issues and installs a Let's Encrypt certificate for you automatically. Do this after you've finished connecting your domain in step 2.
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
After you enter your email and agree to the terms, it's set up automatically. The certificate renews automatically every 90 days, and you can check in advance that renewal works with the command below.
sudo certbot renew --dry-run
If it doesn't work
- The site won't open — Check with
sudo systemctl status nginxthat Nginx is running, and check that 80/443 are open in the firewall. - certbot fails — Most of the time, the domain isn't pointing to the server IP yet. Check with
nslookupfirst. - 502 Bad Gateway — The app connected with
proxy_passis down. Start the app first.