Install NGINX on Ubuntu
In this tutorial, we will install NGINX on Ubuntu PC,
NGINX can be installed on Ubuntu from the default Ubuntu package repository with apt. For most beginners and server administrators, this is the simplest method because Ubuntu manages package updates and the NGINX service is integrated with systemd.

Steps to Install NGINX on Ubuntu
Following is a step by step guide to install NGINX on Ubuntu.
The recommended basic installation flow is:
- Update the Ubuntu package index.
- Install the
nginxpackage. - Start and enable the NGINX service if it is not already running.
- Allow HTTP or HTTPS traffic through the firewall when UFW is enabled.
- Verify the NGINX version, service status, and default web page.
1. Get the Public Key
We have to get the public Key to verify signatures while installing.
Open a terminal and run the following command.
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $key
arjun@arjun-VPCEH26EN:~$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $key
[sudo] password for arjun:
Executing: /tmp/tmp.oIPmrXaOiq/gpg.1.sh --keyserver
keyserver.ubuntu.com
--recv-keys
Keep the terminal open, we have to run some more commands.
Important: if you install NGINX from Ubuntu’s default repositories, you normally do not need to add a separate public key manually. The command above is an older repository-signing style and also uses $key as a placeholder. For a standard Ubuntu install, use the apt update and apt install nginx steps below.
2. Update All Packages
Run the following command in terminal to update ubuntu package.
$ sudo apt-get update
arjun@arjun-VPCEH26EN:~$ sudo apt-get update
Get:1 file:/var/cuda-repo-8-0-local-ga2 InRelease
Ign:1 file:/var/cuda-repo-8-0-local-ga2 InRelease
Get:2 file:/var/cuda-repo-8-0-local-ga2 Release [574 B]
Get:2 file:/var/cuda-repo-8-0-local-ga2 Release [574 B]
Hit:4 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Ign:5 http://ppa.launchpad.net/natecarlson/maven3/ubuntu xenial InRelease
Get:6 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:7 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Hit:8 http://archive.canonical.com/ubuntu xenial InRelease
On current Ubuntu systems, the shorter apt command is commonly used for interactive terminal work:
sudo apt update
This command refreshes Ubuntu’s package index so that the system knows the available NGINX package version and dependencies.
3. Install NGINX
Run the following command in terminal to install NGINX in your Ubuntu PC.
$ sudo apt-get install nginx
arjun@arjun-VPCEH26EN:~$ sudo apt-get install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
nginx-common nginx-core
Suggested packages:
fcgiwrap nginx-doc
The following NEW packages will be installed:
nginx nginx-common nginx-core
0 upgraded, 3 newly installed, 0 to remove and 28 not upgraded.
Need to get 458 kB of archives.
After this operation, 1,482 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx-common all 1.10.3-0ubuntu0.16.04.2 [26.6 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx-core amd64 1.10.3-0ubuntu0.16.04.2 [428 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx all 1.10.3-0ubuntu0.16.04.2 [3,490 B]
Fetched 458 kB in 1s (351 kB/s)
Preconfiguring packages ...
Selecting previously unselected package nginx-common.
(Reading database ... 1243835 files and directories currently installed.)
Preparing to unpack .../nginx-common_1.10.3-0ubuntu0.16.04.2_all.deb ...
Unpacking nginx-common (1.10.3-0ubuntu0.16.04.2) ...
Selecting previously unselected package nginx-core.
Preparing to unpack .../nginx-core_1.10.3-0ubuntu0.16.04.2_amd64.deb ...
Unpacking nginx-core (1.10.3-0ubuntu0.16.04.2) ...
Selecting previously unselected package nginx.
Preparing to unpack .../nginx_1.10.3-0ubuntu0.16.04.2_all.deb ...
Unpacking nginx (1.10.3-0ubuntu0.16.04.2) ...
Processing triggers for ureadahead (0.100.0-19) ...
ureadahead will be reprofiled on next reboot
Processing triggers for ufw (0.35-0ubuntu2) ...
Processing triggers for systemd (229-4ubuntu19) ...
Setting up nginx-common (1.10.3-0ubuntu0.16.04.2) ...
Setting up nginx-core (1.10.3-0ubuntu0.16.04.2) ...
Setting up nginx (1.10.3-0ubuntu0.16.04.2) ...
Processing triggers for systemd (229-4ubuntu19) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for ufw (0.35-0ubuntu2) ...
NGINX is installed successfully.
The same installation command can be written with apt as shown below:
sudo apt install nginx
During installation, Ubuntu may ask for confirmation. Type Y and press Enter to continue. After the package is installed, Ubuntu usually creates a systemd service named nginx.
4. Verify the installation of NGINX
Run the following command in terminal to display the version of NGINX.
$ sudo nginx -v
arjun@arjun-VPCEH26EN:~$ sudo nginx -v
nginx version: nginx/1.10.3 (Ubuntu)
The version number in your terminal may be different because it depends on the Ubuntu release and repository used. You can also verify the service status with systemctl.
sudo systemctl status nginx
If NGINX is running, the status output should show active (running).
Active: active (running)
Start, Stop, Restart, and Reload NGINX on Ubuntu Terminal
After installing NGINX on Ubuntu, you can manage it from the terminal using systemctl. These commands are useful when you change configuration files or troubleshoot the web server.
| Task | Ubuntu terminal command | When to use it |
|---|---|---|
| Start NGINX | sudo systemctl start nginx | Use when the service is installed but not running. |
| Stop NGINX | sudo systemctl stop nginx | Use when you want to stop serving web traffic temporarily. |
| Restart NGINX | sudo systemctl restart nginx | Use after major changes or when a full restart is acceptable. |
| Reload NGINX | sudo systemctl reload nginx | Use after configuration changes when you want to avoid a full stop/start. |
| Enable NGINX on boot | sudo systemctl enable nginx | Use when NGINX should start automatically after reboot. |
For a quick service check, run:
sudo systemctl is-active nginx
active
Allow NGINX Through UFW Firewall on Ubuntu
If the Ubuntu firewall is enabled with UFW, allow web traffic to reach NGINX. First, check the available NGINX application profiles:
sudo ufw app list
You may see profiles such as Nginx HTTP, Nginx HTTPS, and Nginx Full. For a plain HTTP test server, allow HTTP:
sudo ufw allow 'Nginx HTTP'
If you are configuring HTTPS also, use the HTTPS profile or the full profile based on your server requirement.
sudo ufw status
Check NGINX Default Page in Browser on Ubuntu
Once NGINX is installed and running, open a browser and visit your server IP address or local host address. On the same Ubuntu machine, use:
http://localhost/
On a remote server, replace localhost with the public IP address or domain name. If NGINX is working and port 80 is reachable, the default NGINX welcome page should load.
You can also test from the terminal with curl:
curl -I http://localhost/
HTTP/1.1 200 OK
Server: nginx
Where NGINX Is Installed on Ubuntu
After installing NGINX on Ubuntu using apt, the main files are placed in standard Linux locations. These paths help you find configuration files, logs, and the default web root.
| NGINX path on Ubuntu | Purpose |
|---|---|
/etc/nginx/nginx.conf | Main NGINX configuration file. |
/etc/nginx/sites-available/ | Stores available server block configuration files. |
/etc/nginx/sites-enabled/ | Stores enabled server block links used by NGINX. |
/var/www/html/ | Default document root for the default site on many Ubuntu installations. |
/var/log/nginx/access.log | Access log for HTTP requests. |
/var/log/nginx/error.log | Error log used for troubleshooting configuration and runtime issues. |
Create a Simple NGINX Server Block on Ubuntu
The default page is enough to verify installation. For a real website, create a separate document root and server block. Replace example.com with your domain name.
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
printf 'Hello from NGINX on Ubuntu\n' > /var/www/example.com/html/index.html
Create a server block file:
sudo nano /etc/nginx/sites-available/example.com
Add this basic configuration:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Enable the server block, test the NGINX configuration, and reload NGINX:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
The nginx -t command checks the configuration syntax before reload. Always run it after editing NGINX configuration files.
Troubleshoot NGINX Installation Problems on Ubuntu
If NGINX does not start or the browser does not show the default page, check the service status, configuration syntax, firewall, and logs.
sudo nginx -t
sudo systemctl status nginx
sudo journalctl -u nginx --no-pager
sudo tail -n 50 /var/log/nginx/error.log
| Problem after installing NGINX | Likely check |
|---|---|
nginx -t fails | Fix the configuration line shown in the error message. |
| Browser cannot connect | Check whether NGINX is running and whether UFW allows port 80. |
| Another service uses port 80 | Stop the conflicting service or change its port before starting NGINX. |
| Default page works locally but not remotely | Check cloud firewall/security group rules and public IP routing. |
| Changes do not appear | Confirm the correct document root and reload NGINX after configuration changes. |
Uninstall NGINX from Ubuntu
If you installed NGINX only for testing and want to remove it, use remove to uninstall the package while keeping some configuration files, or purge to remove package configuration files as well.
sudo apt remove nginx
To remove NGINX package configuration files too, run:
sudo apt purge nginx nginx-common
Be careful before purging on a real server. Back up any site configuration and files you still need.
Install NGINX on Ubuntu FAQ
How do I know if NGINX is installed on Ubuntu?
Run nginx -v to check whether the NGINX binary is available. You can also run systemctl status nginx to check whether the NGINX service exists and is running.
How do I run NGINX in Ubuntu terminal?
Use sudo systemctl start nginx to start NGINX, sudo systemctl stop nginx to stop it, and sudo systemctl reload nginx to reload configuration changes after running sudo nginx -t.
Where is NGINX installed on Ubuntu?
The main NGINX configuration is usually in /etc/nginx/nginx.conf. Site configurations are commonly kept in /etc/nginx/sites-available/ and enabled through /etc/nginx/sites-enabled/. The default web root is commonly /var/www/html/.
Do I need to add an NGINX public key before installing on Ubuntu?
Not for the normal Ubuntu repository installation. Running sudo apt update and sudo apt install nginx is enough for the standard package. A separate signing key is only relevant when you intentionally add an external NGINX repository.
What command checks NGINX configuration on Ubuntu?
Run sudo nginx -t. This checks the NGINX configuration syntax and reports whether the test is successful. Use it before reloading or restarting NGINX after configuration changes.
Editorial QA Checklist for This NGINX Ubuntu Installation Tutorial
- Confirm the standard Ubuntu installation path uses
sudo apt updateandsudo apt install nginx. - Keep the older existing terminal output unchanged, but make clear that manual public-key setup is not required for the default Ubuntu repository install.
- Verify that every newly added terminal command block uses the
language-bashPrismJS class and output-only blocks use theoutputclass. - Check that service management commands use
systemctland include start, stop, restart, reload, status, and enable use cases. - Ensure firewall guidance mentions UFW profiles such as
Nginx HTTPonly when UFW is enabled. - Confirm file paths such as
/etc/nginx/,/var/www/html/, and/var/log/nginx/are described as Ubuntu package installation paths. - Before publishing, test the sample server block with
sudo nginx -ton a clean Ubuntu environment or clearly mark it as an example usingexample.com.
Conclusion
In this NGINX Tutorial, we have successfully learnt to install NGINX on Ubuntu.
For a basic Ubuntu setup, the essential commands are sudo apt update, sudo apt install nginx, sudo systemctl status nginx, and sudo nginx -t. After installation, verify the default page in the browser and configure firewall, server blocks, and HTTPS according to your server requirement.
TutorialKart.com