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.
|
|
| 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.
|
|
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 |
|
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
|
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 |