How to Install Nginx Web Server on Ubuntu 24.04?
Nginx is a robust open-source web server that provides distribution for both static and dynamic web applications in an efficient way. Nginx is efficient, fast, scalable, and reliable, which draws many users. Compared to a web server that is server-based, Nginx uses an event-driven, asynchronous architecture to handle hundreds of connections without the expense of resources and high performance of a web server.
Due to its efficiency and flexibility, Nginx is a popular choice for those seeking a versatile web server, reverse proxy, load balancer, and HTTP cache. If you are building a simple site, you are hosting multiple application instances, or you are concerned with major traffic requirements, Nginx offers a flexible and efficient way to accommodate different hosting requirements. This guide will give you a step-by-step manner to configuring and installing Nginx on Ubuntu 24.04 for your web services.
Prerequisites
Before proceeding, ensure the following:
- Deploy an Ubuntu 24.04 instance on Vultr.
- Create an A record for your domain/subdomain pointing to your server IP (e.g., app.example.com).
- Access your server via SSH and create a non-root user with sudo privileges.
- Update your server.
Step 1: Update System Packages
Before installing Nginx, update your system package index:
sudo apt update && sudo apt upgrade –y
Step 2: Install Nginx
Nginx is available in the default APT repositories. Install it using:
sudo apt install nginx –y
Verify the installation:
nginx –v
Expected output:
nginx version: nginx/1.24.0 (Ubuntu)
Step 3: Manage Nginx Service
Use systemctl to control Nginx:
Enable Nginx to start on boot:
sudo systemctl enable nginx
Start Nginx service:
sudo systemctl start nginx
Stop Nginx service:
sudo systemctl stop nginx
Restart Nginx service:
sudo systemctl restart nginx
Check Nginx status:
sudo systemctl status nginx
If Nginx is running successfully, you will see:
Active: active (running)
Step 4: Configure an Nginx Virtual Host
To host a website, configure a virtual host.
Create a new configuration file:
sudo nano /etc/nginx/sites-available/app.example.com.conf
Add the following content:
server { listen 80; listen [::]:80; server_name app.example.com; root /var/www/app.example.com; index index.html; location / { try_files $uri $uri/ =404; } }
Save and exit (CTRL+X, Y, Enter).
Test the configuration:
sudo nginx –t
Expected output:
nginx: configuration file /etc/nginx/nginx.conf test is successful
Enable the Virtual Host:
sudo ln -s /etc/nginx/sites-available/app.example.com.conf /etc/nginx/sites-enabled/
Create the web root directory:
sudo mkdir -p /var/www/app.example.com
Add a sample HTML file:
sudo nano /var/www/app.example.com/index.html
Insert the following content:
<html> <head></head> <body> <h1>Welcome to Your Nginx Server!</h1> </body> </html>
Save and exit.
Restart Nginx:
sudo systemctl restart nginx
Test the setup:
curl http://app.example.com
Expected output:
Welcome to Your Nginx Server!
Step 5: Secure Nginx with SSL
To enable HTTPS, install a Let’s Encrypt SSL certificate.
Install Certbot:
sudo snap install --classic certbot
Verify installation:
certbot –version
Expected output:
certbot 2.11.0
Generate an SSL Certificate:
Replace app.example.com with your actual domain.
sudo certbot –nginx -d app.example.com –agree-tos –redirect –email [email protected]
If successful, Certbot configures SSL automatically, and your website will be accessible via HTTPS.
Step 6: Configure Firewall Rules
Ubuntu 24.04 comes with Uncomplicated Firewall (UFW). Allow HTTP and HTTPS traffic:
Allow HTTP:
sudo ufw allow 80/tcp
Allow HTTPS:
sudo ufw allow 443/tcp
Check firewall status:
sudo ufw status
Expected output:
Status: active To Action From ------- ------- ------- 80/tcp ALLOW Anywhere 443/tcp ALLOW Anywhere
Conclusion
Therefore, you have successfully installed and configured Nginx on Ubuntu 24.04. It now points our web server securely using SSL certificates and there are now multiple virtual hosts. Then you can proceed with hosting dynamic applications by combining MySQL and PHP.
Moreover, you can improve performance of Nginx by using caching, implementing load balancing features as well as preforming security enhancements like rate limiting, DDoS security etc. The updates will keep your web server stable while you maintain it.