📌 Introduction
Nginx is an open-source web server that can serve static and dynamic web applications. It can run as a web server, load balancer, reverse proxy, or HTTP cache, and integrates with existing applications to build complete systems or distribute web apps via IP or domain.
This guide shows how to install Nginx on Ubuntu 24.04 and set up a sample web app on your server.
✅ Prerequisites
Before you begin, make sure you:
- 🚀 Provision an Ubuntu 24.04 server.
- 🌍 Create an A record pointing your domain or subdomain to the server IP (e.g.,
app.example.com). - 🔐 Access the server via SSH and create a non-root sudo user.
- 🔄 Update the system packages.
⚙️ Install NGINX on Ubuntu 24.04
The latest NGINX package is available in Ubuntu 24.04’s default APT repository. Follow these steps to update the system and install NGINX.
🔄 Update package lists
| |
📦 Install NGINX
| |
🔍 Check installed NGINX version
| |
Sample output:
| |
⚙️ Manage the NGINX service
NGINX uses the nginx systemd service to manage runtime and processes. Use the commands below to enable and manage the service.
🚀 Enable NGINX to start on boot
| |
Result:
| |
▶️ Start NGINX
| |
⏹️ Stop NGINX
| |
🔄 Restart NGINX
| |
🔍 Check NGINX status
| |
Sample output:
| |
- If
Active: active (running)appears, NGINX is running. If you seeActive: active (failed), stop any process using HTTP port80, then restart NGINX.
🌐 Create an Nginx virtual host
An Nginx virtual host serves web app files from a specific directory using a domain name. Follow these steps to set up a sample virtual host securely.
📂 Create a new virtual host config file
- In
/etc/nginx/sites-available, create a new config file, for example:app.example.com.conf.
| |
- Add the following configuration:
| |
- Save and close the file.
✅ Test Nginx configuration
- Check the configuration for errors:
| |
- Result:
| |
🔗 Enable the virtual host
- Link the config file into
/etc/nginx/sites-enabled:
| |
📁 Create the web root directory
| |
📝 Create a sample HTML file
- Create
index.htmlin the web root:
| |
- Add the following content:
| |
- Save and close the file.
🔄 Restart Nginx
| |
🌍 Verify the virtual host
- Use
curlto test:
| |
- Result:
| |
🔒 Secure the Nginx web server
SSL certificates encrypt communication between the browser and your server over HTTPS. By default, Nginx listens on insecure HTTP port 80. Follow these steps to obtain a trusted SSL certificate from Let’s Encrypt and enable HTTPS.
Install Certbot – Let’s Encrypt client 🔧
- Install Certbot using Snap:
| |
- Verify the Certbot version:
| |
📌 Sample output:
| |
🛡️ Generate an SSL certificate for Nginx
- Create a new SSL certificate for your domain. Replace
app.example.comwith your real domain:
| |
This will:
- ✅ Generate a valid SSL certificate
- ✅ Automatically configure Nginx to use SSL
- ✅ Enable HTTPS on your web server
Test automatic renewal 🔄
- Let’s Encrypt certificates last 90 days. Check auto-renewal with:
| |
If no errors appear, the certificate will renew automatically.
🌐 Verify the site with HTTPS
- Open the browser and visit:
| |
If you see the lock icon 🔒, SSL is installed successfully! 🚀
🔥 Configure UFW firewall rules
Uncomplicated Firewall (UFW) is installed and enabled by default on Ubuntu 24.04. Follow these steps to allow HTTP and HTTPS traffic.
Allow HTTP (Port 80) 🔌
- Run:
| |
Allow HTTPS (Port 443) 🔐
- Run:
| |
Check firewall status 🔢
- Run:
| |
👉 Sample output:
| |
🛡️ After setup, the server only allows HTTP and HTTPS connections.
🎯 Conclusion
Congratulations on installing Nginx on Ubuntu 24.04 and configuring a web server for your applications. Nginx supports multiple virtual hosts to deploy applications securely. You can also integrate Nginx with MySQL and PHP to build dynamic web apps. For more configuration options, visit the official Nginx documentation. 🚀😊
