NGINX Installation and Testing on Ubuntu Server
NGINX Installation and Testing on Ubuntu Server
NGINX is a high-performance web server and reverse proxy known for its stability, low resource consumption, and rich feature set. In this guide, we will walk through installing and testing NGINX on an Ubuntu server.
Requirements
- A running instance of Ubuntu Server (20.04 or later).
- A user account with sudo privileges.
- Access to a terminal or SSH connection.
1. Connect to the Ubuntu Server
From your computer, connect using SSH:
ssh username@SERVER_IP
Example:
ssh ubuntu@192.168.1.100
2. Update Ubuntu
Before installing any new software, update the package index:
sudo apt update
Optionally upgrade installed packages:
sudo apt upgrade -y
3. Install NGINX
Install NGINX from the default Ubuntu repository:
sudo apt install nginx -y
Check the installed version:
nginx -v
Example output:
nginx version: nginx/1.24.x
4. Check NGINX Status
Verify that NGINX is running:
sudo systemctl status nginx
You should see:
Active: active (running)
Press q to exit.
5. Start and Enable NGINX
If NGINX is not running, start it:
sudo systemctl start nginx
Enable NGINX to start automatically after reboot:
sudo systemctl enable nginx
6. Test NGINX Locally
Run:
curl http://localhost
If NGINX is working, you should see HTML containing:
Welcome to nginx!
7. Find the Server IP Address
Run:
hostname -I
Example output:
192.168.1.100
You can also use:
ip addr
8. Test NGINX from a Browser
From another computer on the same network, open a browser and visit:
http://192.168.1.100
Replace 192.168.1.100 with your Ubuntu Server’s IP address.
You should see the Welcome to nginx! default page.
9. Check the Firewall
Check UFW (Uncomplicated Firewall):
sudo ufw status
If UFW is active, allow HTTP traffic:
sudo ufw allow 'Nginx HTTP'
Check again:
sudo ufw status
Port 80 should now be allowed.
10. Create Your Own Test Website
The default NGINX website is located under /var/www/html/.
Create/edit the index page:
sudo nano /var/www/html/index.html
Add the following content:
<!DOCTYPE html>
<html>
<head>
<title>My NGINX Server</title>
</head>
<body>
<h1>Hello from Ubuntu Server!</h1>
<p>NGINX is working correctly.</p>
</body>
</html>
Save the file:
- Press
Ctrl + O - Press
Enter - Press
Ctrl + X
11. Test Your Website
On the Ubuntu Server:
curl http://localhost
You should see your custom HTML content.
From another computer, open:
http://SERVER_IP
You should now see:
Hello from Ubuntu Server! NGINX is working correctly.
12. Test NGINX Configuration
Before changing or reloading NGINX configuration, always test it:
sudo nginx -t
Successful output should look like:
syntax is ok
test is successful
If the configuration is valid, reload NGINX:
sudo systemctl reload nginx
13. Useful NGINX Commands
| Command | Purpose |
|---|---|
sudo systemctl start nginx |
Start NGINX |
sudo systemctl stop nginx |
Stop NGINX |
sudo systemctl restart nginx |
Restart NGINX |
sudo systemctl reload nginx |
Reload configuration |
sudo systemctl status nginx |
Check status |
sudo nginx -t |
Test configuration |
nginx -v |
Check NGINX version |
curl http://localhost |
Test locally |
14. Important NGINX Locations
| Path | Description |
|---|---|
/etc/nginx/ |
Main NGINX configuration directory |
/etc/nginx/nginx.conf |
Main NGINX configuration file |
/etc/nginx/sites-available/ |
Configuration files for websites |
/etc/nginx/sites-enabled/ |
Enabled website configurations |
/var/www/html/ |
Default website files |
/var/log/nginx/ |
NGINX log files |
15. Quick Installation Test (Summary)
Run these commands one by one:
sudo apt update
sudo apt install nginx -y
sudo systemctl enable --now nginx
sudo nginx -t
curl http://localhost
hostname -I
Then open http://SERVER_IP in your browser. If you see the NGINX welcome page, NGINX is successfully installed and working on your Ubuntu Server!
Conclusion
You have successfully installed and tested NGINX on your Ubuntu Server. NGINX is now ready to serve web content, act as a reverse proxy, or handle load balancing for your applications.
Table of Contents