Verified guide
This guide was verified by creating a cloud server and running every command in it, from start to finish, exactly as written.
- OS
- Ubuntu 26.04.1 LTS · x86_64
- Installed version
- Nginx 1.28.3 (1.28.3-2ubuntu1.12)
- Test server
- DigitalOcean · 1 vCPU / 2 GB RAM / 50 GB SSD (s-1vcpu-2gb, sgp1)
- Last checked
- 2026-10-27
This guide contains affiliate links. If you sign up through them, Codesand may earn a commission. This doesn't affect the content or our assessments.
This guide installs Nginx 1.28 from the default repository on an Ubuntu 26.04 server and opens ports 80 and 443 in the ufw firewall so you can connect from another PC. It also covers service management commands, where the configuration files are, common errors, and how to uninstall.
1. Requirements
- One Ubuntu 26.04 LTS server
- SSH access with a regular user account that has
sudoprivileges - A PC to test access from outside (used in the firewall step)
If you don't have a server, try creating a DigitalOcean Droplet (1 vCPU, 2 GB). Every step in this guide was verified on a Droplet (1 vCPU, 2 GB memory, 50 GB SSD, Ubuntu 26.04, Singapore) using a regular user account with sudo privileges. External access was tested from another PC (example IP 203.0.113.10) to the server (example IP 203.0.113.20).
2. Check the version to install
First, update the package list. The beginning of the output (the Hit and Get lines) is omitted.
sudo apt update78 packages can be upgraded. Run 'apt list --upgradable' to see them.Candidate is the version that will be installed. The default Ubuntu 26.04 repository ships Nginx 1.28. If you use Ubuntu 24.04, see How to Install Nginx on Ubuntu 24.04 and Open the Firewall.
apt-cache policy nginxnginx:
Installed: (none)
Candidate: 1.28.3-2ubuntu1.12
Version table:
1.28.3-2ubuntu1.12 500
500 http://mirrors.digitalocean.com/ubuntu resolute-updates/main amd64 Packages
500 http://security.ubuntu.com/ubuntu resolute-security/main amd64 Packages
1.28.3-2ubuntu1 500
500 http://mirrors.digitalocean.com/ubuntu resolute/main amd64 Packages3. Install Nginx
-y automatically answers "yes" when asked whether to install. nginx is installed together with nginx-common, the package that contains the configuration files. The middle of the output is omitted.
sudo apt install -y nginxInstalling:
nginx
Installing dependencies:
nginx-common
Suggested packages:
fcgiwrap nginx-doc ssl-cert
Summary:
Upgrading: 0, Installing: 2, Removing: 0, Not Upgrading: 78
Download size: 656 kB
Space needed: 1864 kB / 48.5 GB available
…
Setting up nginx (1.28.3-2ubuntu1.12) ...
* Upgrading binary nginx
...done.
Processing triggers for man-db (2.13.1-1build1) ...
Processing triggers for ufw (0.36.2-9build1) ...Check the installed version.
nginx -vnginx version: nginx/1.28.3 (Ubuntu)4. Check the service status
The Ubuntu Nginx package starts as soon as the installation finishes. Active: active (running) means it is running normally.
systemctl status nginx --no-pager | head -3● 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 Tue 2026-10-27 08:18:52 UTC; 8s agoenabled means Nginx starts automatically when the server reboots.
systemctl is-enabled nginxenabledCheck that Nginx is listening on port 80. 0.0.0.0:80 means it accepts connections on all IPv4 addresses, and [::]:80 on all IPv6 addresses.
sudo ss -tlnp | grep nginxLISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=3295,fd=5),("nginx",pid=3292,fd=5))
LISTEN 0 511 [::]:80 [::]:* users:(("nginx",pid=3295,fd=6),("nginx",pid=3292,fd=6))Request the default page from inside the server. -I fetches only the response headers without the body. If you see HTTP/1.1 200 OK, Nginx is responding normally.
curl -I http://localhostHTTP/1.1 200 OK
Server: nginx/1.28.3 (Ubuntu)
Date: Tue, 27 Oct 2026 08:19:16 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 27 Oct 2026 08:18:50 GMT
Connection: keep-alive
ETag: "6ae05e6a-267"
Accept-Ranges: bytes5. Open ports 80 and 443 in the firewall (ufw)
On a new Ubuntu server created on DigitalOcean, the ufw firewall is turned off. That is why other PCs can already connect. The following is the result of a request sent to the server from a terminal on the PC (203.0.113.10).
curl -I http://203.0.113.20HTTP/1.1 200 OK
Server: nginx/1.28.3 (Ubuntu)sudo ufw statusStatus: inactiveDecide which ports to open before you turn on the firewall. Installing Nginx registers Nginx profiles in ufw.
sudo ufw app listAvailable applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
Nginx QUIC
OpenSSHNginx HTTP opens only port 80, and Nginx HTTPS only port 443. If you plan to add an HTTPS certificate later, use Nginx Full, which opens both ports.
sudo ufw app info "Nginx Full"Profile: Nginx Full
Title: Web Server (Nginx, HTTP + HTTPS)
Description: Small, but very powerful and efficient web server
Ports:
80,443/tcpAllow SSH (port 22) before you turn on the firewall. If you turn on the firewall without allowing SSH, your current SSH session is dropped and you cannot connect again.
sudo ufw allow OpenSSHRules updated
Rules updated (v6)Turn on the firewall. --force skips the confirmation prompt warning that the SSH connection may be disrupted.
sudo ufw --force enableFirewall is active and enabled on system startupNow only SSH is open, so a web request from the PC gets no response and times out. --max-time 10 waits for at most 10 seconds.
curl -I --max-time 10 http://203.0.113.20curl: (28) Connection timed out after 10005 millisecondsOpen ports 80 and 443.
sudo ufw allow "Nginx Full"Rule added
Rule added (v6)sudo ufw statusStatus: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)Send the request from the PC again, and the server responds.
curl -I --max-time 10 http://203.0.113.20HTTP/1.1 200 OK
Server: nginx/1.28.3 (Ubuntu)
Date: Tue, 27 Oct 2026 08:19:44 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 27 Oct 2026 08:18:50 GMT
Connection: keep-alive
ETag: "6ae05e6a-267"
Accept-Ranges: bytesExtracting just the page title confirms that it is the Nginx default page. Opening http://203.0.113.20 (the server IP) in a browser shows the same page.
curl -s http://203.0.113.20 | grep -o "<title>.*</title>"<title>Welcome to nginx!</title>6. Configuration file locations
Nginx configuration lives in the /etc/nginx folder.
ls /etc/nginx/conf.d koi-win nginx.conf sites-enabled
fastcgi.conf mime.types proxy_params snippets
fastcgi_params modules-available scgi_params uwsgi_params
koi-utf modules-enabled sites-available win-utfThese are the files you usually edit.
/etc/nginx/nginx.conf: The main configuration. At the bottom, it loads the files in theconf.dandsites-enabledfolders./etc/nginx/sites-available/: Where you keep one configuration file per site (server block)./etc/nginx/sites-enabled/: Holds links to the files insites-availablefor only the sites you actually use. Removing a link turns off the site while keeping the file.
grep -n include /etc/nginx/nginx.conf6:include /etc/nginx/modules-enabled/*.conf;
27: include /etc/nginx/mime.types;
60: include /etc/nginx/conf.d/*.conf;
61: include /etc/nginx/sites-enabled/*;Right after installation, one site, default, is enabled. sites-enabled/default is a link that points to sites-available/default.
ls -l /etc/nginx/sites-available/ /etc/nginx/sites-enabled//etc/nginx/sites-available/:
total 4
-rw-r--r-- 1 root root 2412 Mar 27 2026 default
/etc/nginx/sites-enabled/:
total 0
lrwxrwxrwx 1 root root 34 Oct 27 08:18 default -> /etc/nginx/sites-available/defaultWithout comments (#) and blank lines, the default file is only a few lines long. root is the folder that holds the web page files, and index is the list of file names to look for, in order, when a folder address is requested.
grep -v -E "^\s*#|^\s*$" /etc/nginx/sites-available/defaultserver {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}The default page you see now is index.nginx-debian.html in the /var/www/html folder.
ls /var/www/html/index.nginx-debian.htmlAccess and error logs are written to the /var/log/nginx folder. When something goes wrong, check error.log first.
ls -l /var/log/nginx/total 8
-rw-r----- 1 www-data adm 426 Oct 27 08:19 access.log
-rw-r----- 1 www-data adm 76 Oct 27 08:18 error.log7. Change the page and apply configuration changes
If you create index.html in /var/www/html, this file is shown because it comes before index.nginx-debian.html in the index order. tee writes what it receives to a file and also prints it to the screen. Use it with sudo to write a file in a folder that requires administrator privileges.
echo "<h1>Hello Nginx</h1>" | sudo tee /var/www/html/index.html<h1>Hello Nginx</h1>curl http://localhost<h1>Hello Nginx</h1>Changes to page files take effect immediately, but when you edit a configuration file, Nginx has to read it again. Before that, check the configuration for syntax errors with nginx -t.
sudo nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulUse reload to make Nginx read the configuration again. It switches to the new configuration without dropping requests in progress. It prints nothing.
sudo systemctl reload nginxOther management commands are listed below. None of them print anything, and you can check the result with systemctl is-active nginx.
sudo systemctl restart nginx: Stops Nginx and starts it again. Requests in progress are dropped.sudo systemctl stop nginx: Stops Nginx. It starts again when the server reboots.sudo systemctl start nginx: Starts Nginx when it is stopped.
sudo systemctl stop nginx
systemctl is-active nginxinactivesudo systemctl start nginx
systemctl is-active nginxactive8. Troubleshooting
How to connect PHP or a Node.js app is covered in How to Connect Nginx to PHP-FPM and How to Connect a Node.js App with an Nginx Reverse Proxy. For 502 and 403 errors that appear after connecting an app, see How to Fix Nginx 502 Bad Gateway and How to Fix Nginx 403 Forbidden.
curl: (28) Connection timed out after 10005 milliseconds
If curl -I http://localhost works inside the server but requests time out only from other PCs, the firewall is blocking port 80. Check that sudo ufw status lists Nginx Full (or 80/tcp), and if it does not, run sudo ufw allow "Nginx Full". If the rule is in ufw and the result is the same, check your cloud provider's firewall (such as DigitalOcean Cloud Firewall).
nginx: [emerg] invalid number of arguments in "root" directive
This means there is a syntax error in a configuration file. The following is the result after removing the semicolon (;) at the end of the root /var/www/html; line. default:44 at the end of the message is the file and line number with the error.
sudo nginx -t2026/10/27 08:20:40 [emerg] 5647#5647: invalid number of arguments in "root" directive in /etc/nginx/sites-enabled/default:44
nginx: configuration file /etc/nginx/nginx.conf test failedA reload in this state fails, but Nginx keeps running with the previous configuration. So the site still opens, and only your edits are not applied.
sudo systemctl reload nginxJob for nginx.service failed.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.systemctl is-active nginxactiveFix the line shown, check that sudo nginx -t ends with test is successful, and run reload again.
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
This means another program is already using port 80, so Nginx could not start. The following is the result of starting Nginx while another program (a Python web server) is using port 80.
sudo systemctl start nginxJob for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.Check the cause with journalctl. -n 12 shows only the last 12 lines, and the first 3 lines are omitted. Nginx retries a few times and then stops with still could not bind().
sudo journalctl -u nginx --no-pager -n 12…
Oct 27 08:20:48 opendoc-nginx-10271716 nginx[5970]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 27 08:20:48 opendoc-nginx-10271716 nginx[5970]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 27 08:20:49 opendoc-nginx-10271716 nginx[5970]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 27 08:20:49 opendoc-nginx-10271716 nginx[5970]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 27 08:20:50 opendoc-nginx-10271716 nginx[5970]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 27 08:20:50 opendoc-nginx-10271716 nginx[5970]: nginx: [emerg] still could not bind()
Oct 27 08:20:50 opendoc-nginx-10271716 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Oct 27 08:20:50 opendoc-nginx-10271716 systemd[1]: nginx.service: Failed with result 'exit-code'.
Oct 27 08:20:50 opendoc-nginx-10271716 systemd[1]: Failed to start nginx.service - A high performance web server and a reverse proxy server.Find the program using port 80 with ss. The program name and process ID (pid) appear after users:.
sudo ss -tlnp | grep ":80 "LISTEN 0 5 0.0.0.0:80 0.0.0.0:* users:(("python3",pid=5902,fd=3))If you don't need that program, stop it and start Nginx. If it runs as a service, stop it with sudo systemctl stop service-name; otherwise, pass the pid you found above to sudo kill. If you need both programs, change the port of one of them.
sudo kill 5902
sudo systemctl start nginx
systemctl is-active nginxactivecurl: (7) Failed to connect to localhost port 80 after 0 ms: Could not connect to server
This means no program is accepting connections on port 80 on the server. Nginx is stopped or not installed. Check the status with systemctl status nginx --no-pager, and if it shows failed, look for the cause in sudo journalctl -u nginx --no-pager -n 20.
9. Uninstall
purge removes the packages along with the configuration files in /etc/nginx. The beginning of the output is omitted.
sudo apt purge -y nginx nginx-commonREMOVING:
nginx* nginx-common*
Summary:
Upgrading: 0, Installing: 0, Removing: 2, Not Upgrading: 78
Freed space: 1864 kB
Removing nginx (1.28.3-2ubuntu1.12) ...
Removing nginx-common (1.28.3-2ubuntu1.12) ...
Processing triggers for man-db (2.13.1-1build1) ...
Purging configuration files for nginx-common (1.28.3-2ubuntu1.12) ...
Processing triggers for ufw (0.36.2-9build1) ...
Rules updated for profile 'OpenSSH'
Skipped reloading firewallRemove any packages that were installed along with it and are no longer used. There was nothing to remove on this server.
sudo apt autoremove -ySummary:
Upgrading: 0, Installing: 0, Removing: 0, Not Upgrading: 78Close ports 80 and 443 that you opened in the firewall. Keep the SSH rule.
sudo ufw delete allow "Nginx Full"Rule deleted
Rule deleted (v6)sudo ufw statusStatus: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)Check for remaining packages. grep -c counts the matching lines, so 0 means all Nginx packages have been removed.
dpkg -l | grep -c nginx0/etc/nginx is gone, but the web page folder /var/www/html remains.
ls /etc/nginx /var/www/html/var/www/html:
index.html
index.nginx-debian.html
ls: cannot access '/etc/nginx': No such file or directoryIf you don't need the remaining page files, delete them. rm -rf deletes the folder and everything in it without asking, so double-check the path before you run it.
sudo rm -rf /var/www/htmlNginx has been removed, so nothing accepts connections on port 80.
curl http://localhostcurl: (7) Failed to connect to localhost port 80 after 0 ms: Could not connect to serverTry it yourself
No server yet? You can spin up an Ubuntu server in a few minutes with one of these providers. We verified this guide on DigitalOcean.
Was this guide helpful? Let us know if anything is wrong or out of date.
Request a correction