Nginx is the web server we use on a LochStudios KVM VPS when you want a light HTTP front end, a reverse proxy, or a LEMP stack. This article installs the package from Ubuntu or Debian apt, starts it, and checks that the welcome page answers on this server's IPv4.
This is for a LochStudios KVM VPS. Credentials, the IPv4, and an out-of-band console sit on that server in the portal. Sign in at the portal, open the VPS service, and copy the login from there. Do not guess a hostname.
On Beginner and Standard shared hosting (cPanel) you do not install Nginx. Open the hosting service and click Log in to cPanel.
If you also need MariaDB and PHP-FPM in the same sitting, use Install a LEMP stack instead of this article alone.
Before you install
- Sign in at the portal, open the VPS service, and copy the IPv4, username, and password.
- Connect over SSH.
- Windows: Connect to your VPS via SSH from Windows
- macOS or Linux: Connect to your VPS via SSH from macOS or Linux
- Patch the image and use a sudo user for daily work: First steps on a new VPS.
If SSH from your computer will not connect, open the console on the same service. That session does not need port 22 from your network.
Do not install Nginx on a box that already has Apache listening on port 80. One process owns that port. If you want Apache instead, see Install Apache2 on Ubuntu/Debian.
Update package lists
sudo apt update
Install Nginx
sudo apt install -y nginx
On AlmaLinux or Rocky Linux:
sudo dnf install -y nginx
Debian-style site files live under /etc/nginx/sites-available/ and /etc/nginx/sites-enabled/. AlmaLinux and Rocky Linux use /etc/nginx/conf.d/ and /etc/nginx/nginx.conf instead. The rest of this article uses the Ubuntu and Debian layout.
Start and enable Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
enable starts Nginx again after a reboot.
Confirm it is running
sudo systemctl status nginx
You should see active (running).
From the server itself:
curl -I http://127.0.0.1
You should get HTTP/1.1 200 (or HTTP/1.1 301 if you already forced HTTPS). The default page on Ubuntu and Debian is served from /var/www/html.
Open it in a browser
Use the IPv4 on the VPS service in the portal:
http://203.0.113.10
Replace that address with yours. You should see the Nginx welcome page.
If the service is running and curl on the server works, but your browser does not, the host firewall is usually still closed on port 80.
Allow HTTP and HTTPS on UFW
UFW is the host firewall on our Ubuntu images. Allow SSH before you enable it, or you will cut off SSH from your computer. The console in the portal still works if that happens.
If UFW is already active:
sudo ufw allow 'Nginx Full'
sudo ufw status
Nginx Full is the package profile for ports 80 and 443. HTTP only:
sudo ufw allow 'Nginx HTTP'
If UFW is still inactive, do not run sudo ufw enable from this article. Set the defaults and allow SSH first: Set up a UFW firewall on Ubuntu. That guide then opens 80 and 443 the same way.
On AlmaLinux or Rocky Linux, open the ports with firewalld if that service is running:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Everyday commands
Reload after a config change (keeps current connections):
sudo systemctl reload nginx
Restart:
sudo systemctl restart nginx
Stop:
sudo systemctl stop nginx
Check the config before you reload:
sudo nginx -t
Only reload when that prints syntax is ok and test is successful.
Where the files live (Ubuntu and Debian)
- Main config:
/etc/nginx/nginx.conf - Site configs you edit:
/etc/nginx/sites-available/ - Sites Nginx actually loads:
/etc/nginx/sites-enabled/(symlinks) - Default document root:
/var/www/html
Leave the default site in place until you have a server block that answers. Then add your own site: Create an Nginx Server Block.
There is no AutoSSL on a stock VPS. After the name points at this IPv4, issue TLS with Certbot.
If Nginx will not start
Address already in use on port 80 or 443
Something else is bound there (often Apache). Check:
sudo ss -tlnp | grep -E ':80|:443'
Stop the other web server, or pick one stack and stay with it.
nginx.service failed after a config edit
sudo nginx -t
sudo journalctl -u nginx -n 50 --no-pager
Fix the file nginx -t names, then sudo systemctl start nginx.
Browser cannot reach the welcome page
sudo systemctl is-active nginxshould printactive.curl -I http://127.0.0.1should succeed on the server.sudo ufw statusshould allow 80/tcp (and 443/tcp once you want HTTPS).- Browse to the IPv4 shown on the VPS in the portal, not a hostname you have not pointed yet.
Still stuck? Open a support ticket. Tell us the VPS service and what sudo systemctl status nginx and sudo nginx -t printed.
Next
- Point a name at this VPS, then add a server block: Create an Nginx Server Block
- Add MariaDB and PHP-FPM: Install a LEMP stack
- Issue TLS: Get a Free SSL Certificate with Certbot
- Host firewall: Set up a UFW firewall on Ubuntu