Install and configure nginx
nginx is a fast, lightweight web server that runs well on small VPS plans. In this guide you will install it from Ubuntu's package archive, open the firewall for web traffic, and serve your first page.
Before you begin
- A VPS running Ubuntu 24.04 LTS, with SSH access as root or as a sudo user, such as deploy.
- The server's IP address. This guide uses 203.0.113.10 as an example.
- Basic comfort typing commands into a terminal.
Step 1: Update the package lists
apt update refreshes the list of available software. apt upgrade installs updates to the software already on the server, including security fixes. Run both on a new server before installing anything. If you are logged in as root, leave out sudo.
sudo apt update
sudo apt upgrade -y
Step 2: Install nginx
The -y flag answers "yes" to the install prompt automatically.
sudo apt install -y nginx
Step 3: Check that nginx is running
Ubuntu starts nginx automatically after installation. systemctl status asks systemd, the service manager, whether it is running. The --no-pager flag prints the output straight to the screen.
systemctl status nginx --no-pager
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running) since Thu 2026-10-29 09:14:02 UTC; 1min ago
Step 4: Open the firewall
Ubuntu's firewall, UFW, blocks everything by default. Allow SSH first so you do not lock yourself out, then allow web traffic. The profile Nginx Full opens port 80 for HTTP and port 443 for HTTPS, which you will need later.
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx Full ALLOW Anywhere
sudo ufw allow OpenSSH before sudo ufw enable. Enabling the firewall without an SSH rule can disconnect you from the server.Step 5: View the default page
Ask the server for its own page with curl, a tool for sending web requests. The -I option asks for headers only.
curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)
Open http://203.0.113.10 in your browser. You should see the "Welcome to nginx!" page.
Step 6: Replace the test page
nginx serves files from /var/www/html by default. Overwrite the test page with your own HTML. The tee command writes the text to the file with the permissions sudo provides.
echo "<h1>Hello from my new server</h1>" | sudo tee /var/www/html/index.html
curl http://localhost
<h1>Hello from my new server</h1>
Step 7: Learn where the configuration lives
The main configuration file is /etc/nginx/nginx.conf. Each website gets its own file in /etc/nginx/sites-available/, which is enabled by linking it into /etc/nginx/sites-enabled/. After any change, test the configuration and then reload nginx:
sudo nginx -t
sudo systemctl reload nginx
The test command reports "syntax is ok" when your files are valid. Always run it before a reload so a typo does not take the site down.
sudo systemctl restart nginx only when a reload is not enough. A reload applies changes without dropping active connections.Troubleshooting
- The browser cannot connect. Run
sudo ufw statusand confirm the Nginx Full rule is listed. Also confirm nginx is running withsystemctl status nginx. - "Address already in use" on port 80. Another web server, such as Apache, is already listening. Find it with
sudo ss -tlnp | grep :80and stop it if you do not need it. - The browser still shows the old page. Try a private window or reload with Ctrl+Shift+R. Also check that the file was saved with
cat /var/www/html/index.html. - nginx -t reports an error. The message names the file and line number. Open that file, fix the problem, and test again.
Next steps
Once your server responds, add HTTPS with Enable HTTPS with free TLS certificates. To host more than one domain, read Host multiple sites with nginx server blocks.
Something not working? Chat or email our support team and we will help you get unstuck.
Contact support