How to install nginx on Ubuntu and serve your website from a folder

On an Ubuntu VPS, you install nginx with sudo apt install nginx, create a site file in /etc/nginx/sites-available/ that points at your folder, enable it with a link in sites-enabled, check it with sudo nginx -t and reload. And the site opens. This article is for an unmanaged VPS with root access: what runs inside is yours, and our support does not install or repair nginx (see how far our support goes).

The commands below are for Debian and Ubuntu. On AlmaLinux or Rocky Linux the installer (dnf) and the configuration folder differ; the table further down has them. Depending on your system, always check against the distribution’s own documentation.

Install it and get the site opening

1 Refresh the package list and install. sudo apt update then sudo apt install nginx. The service starts on its own and is set to start at boot.
2 Open ports 80 and 443 if a firewall is active (on Ubuntu, sudo ufw allow 80/tcp and sudo ufw allow 443/tcp). Without it the site opens only from inside the server. See ports and firewall on a VPS and setting up ufw without locking yourself out.
3 Put the site in a folder. For example /var/www/yourdomain.tld/public, with a test index.html in it. Create it with sudo mkdir -p /var/www/yourdomain.tld/public.
4 Create the site file with sudo nano /etc/nginx/sites-available/yourdomain.tld and this content:server {
listen 80;
server_name yourdomain.tld www.yourdomain.tld;
root /var/www/yourdomain.tld/public;
index index.html index.php;

location / {
try_files $uri $uri/ =404;
}
}
5 Enable it. sudo ln -s /etc/nginx/sites-available/yourdomain.tld /etc/nginx/sites-enabled/. The sites-enabled/default link can stay or go; if the name in a request matches no server_name, the nginx welcome page is what answers.
6 Test the configuration before reloading. sudo nginx -t must say “syntax is ok” and “test is successful”. Only then sudo systemctl reload nginx. A reload keeps open connections alive; a restart drops them.
7 Point the domain at the server. The domain’s A record, and the one for www, must hold the VPS IP (see where to find your VPS IP and pointing a domain at your own VPS). To try it before DNS has spread, use the hosts file: testing a site before you change the DNS.

If the site uses PHP

nginx does not run PHP itself: it hands it to PHP-FPM. Install it (on Ubuntu, sudo apt install php-fpm) and add to the server block a location ~ \.php$ with include snippets/fastcgi-php.conf; and a fastcgi_pass pointing at PHP-FPM’s socket. The socket name carries the PHP version, so look at what exists with ls /run/php/ instead of copying it from a guide. A “502 Bad Gateway” here is usually a socket with the wrong name or a stopped PHP-FPM.

Debian and Ubuntu, or AlmaLinux and Rocky

Debian and Ubuntu AlmaLinux and Rocky Linux
Install apt install nginx dnf install nginx then systemctl enable --now nginx
Where sites live /etc/nginx/sites-available and sites-enabled /etc/nginx/conf.d/name.conf
Service user www-data nginx
Firewall ufw firewalld: firewall-cmd --permanent --add-service=http
Logs /var/log/nginx/access.log and error.log the same
On AlmaLinux and Rocky, SELinux can block nginx without a word on the site: it will not read folders with the wrong label. If the file exists and the permissions are right but you still get a 403, see when SELinux or AppArmor blocks a service. And if the 403 is ordinary permissions, file permissions on a VPS.
The first place to look is the error log: sudo tail -n 30 /var/log/nginx/error.log. It nearly always says what is wrong (missing file, permission denied, missing socket).

Server not answering even to a ping, or locked out by SSH after touching the firewall? That we can check from our side.

Open a support ticket

SEE ALSO

Installing Apache and setting up a virtual host

Several websites on one VPS

Redirecting to HTTPS and to the bare domain

Ports and firewall on a VPS

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $6.59/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?