How to Host a Website on a VPS: Nginx, Domain and SSL
In 20–30 minutes you will have your own website running on a VPS, with a domain, the Nginx web server and a free HTTPS certificate. This guide works for static sites (HTML, CSS, JS) and for React, Vue and other framework builds. PHP and WordPress are covered briefly in step 6.
What you need:
- A VPS running Ubuntu 24.04 or 22.04 (1 vCPU and 1 GB RAM is enough for a small site)
- The server's IP address and root password or SSH key, sent by your hosting provider after purchase
- A domain, for example
example.com, and access to its DNS settings - Your website files on your computer
In every command below, replace example.com with your domain and 203.0.113.10 with your server's IP.
Step 1. Connect to the server and update the system
Open Terminal (macOS, Linux) or PowerShell (Windows) and connect over SSH: ssh root@203.0.113.10
On the first connection SSH asks whether you trust the server. Type yes, then enter your password. The characters are not shown as you type, which is normal.
Update the packages: apt update && apt upgrade -y
Step 2. Point your domain to the server
Open your domain registrar's or DNS provider's panel (for example, Cloudflare) and create two A records:
- Type
A, name@, value203.0.113.10 - Type
A, namewww, value203.0.113.10
Records usually start working within 5–30 minutes, but it can take up to 24 hours. Check from your computer with ping example.com. If the reply shows your server's IP, the domain is connected.
Step 3. Install Nginx and open the ports
Nginx is a fast web server that will serve your site to visitors. Install it: apt install nginx -y
Allow SSH and the web ports 80 and 443 in the firewall. Allow SSH first, otherwise you will lose access once the firewall is on. Run in order:
ufw allow OpenSSHufw allow 'Nginx Full'ufw enable
Open http://203.0.113.10 in your browser. If you see the "Welcome to nginx!" page, the web server is running.
Step 4. Upload your site and configure Nginx
Create a folder for the site on the server: mkdir -p /var/www/example.com
Upload the files. Run this command on your computer, not on the server, from inside your site's folder: scp -r ./* root@203.0.113.10:/var/www/example.com/
Prefer a graphical tool? Use FileZilla or Cyberduck: protocol SFTP, host = server IP, port 22. The folder root must contain an index.html file.
Create the site config on the server: nano /etc/nginx/sites-available/example.com
Paste in this text:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / { try_files $uri $uri/ /index.html; }
}
Save the file with Ctrl + O, Enter, then Ctrl + X. Then enable the site, test the config and reload Nginx by running in order:
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/rm /etc/nginx/sites-enabled/defaultchown -R www-data:www-data /var/www/example.comnginx -tsystemctl reload nginx
nginx -t should reply syntax is ok. Your site now opens at http://example.com.
Step 5. Add a free SSL certificate (HTTPS)
Without HTTPS, browsers mark your site as "Not secure" and search engines rank it lower. Let's Encrypt issues certificates for free. Install certbot: apt install certbot python3-certbot-nginx -y
Get the certificate: certbot --nginx -d example.com -d www.example.com
Certbot asks for an email for notifications and for agreement to the terms. It then adds the certificate to the Nginx config and sets up the HTTP-to-HTTPS redirect.
The certificate is valid for 90 days and renews automatically. To check that renewal works: certbot renew --dry-run
Done: the site opens at https://example.com with a padlock in the address bar.
Step 6. If your site uses PHP or WordPress
For PHP sites, also install PHP and a database: apt install php-fpm php-mysql mariadb-server -y
In the Nginx config from step 4, change index index.html; to index index.php index.html; and add this PHP line before the final }:
location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.3-fpm.sock; }
The version in the path (php8.3) depends on your system: on Ubuntu 22.04 it is php8.1. Run ls /run/php/ to see the exact one. After editing, run nginx -t and systemctl reload nginx.
For WordPress, download the archive from wordpress.org, unpack it into /var/www/example.com, create a database in MariaDB and open the site in your browser. The WordPress installer will guide you through the rest.
Common errors
- Site doesn't open by domain. DNS hasn't updated yet. Wait up to 24 hours and check
ping example.com. - 403 Forbidden. There's no
index.htmlor the permissions are wrong. Check the files and runchown -R www-data:www-data /var/www/example.com. - 502 Bad Gateway. PHP-FPM isn't running. Run
systemctl restart php8.3-fpm. - Certbot fails. The domain doesn't point to the server or port 80 is closed. Check the A records and
ufw status. - WordPress page links don't work. In the
location /line, replace/index.htmlwith/index.php?$args. - Nginx won't reload. There's an error in the config:
nginx -tshows the line where it is.
Summary
Your site now runs on your own VPS with a domain, the fast Nginx web server and free HTTPS. To update the site, just upload new files to /var/www/example.com. There is no need to restart Nginx.