How to Install Nginx Web Server on Ubuntu 24.04
![How to Install Nginx Web Server on Ubuntu 24.04 header image](https://docs.vultr.com/public/doc-assets/1862/fab0ae3354e6053b.png)
Introduction
Nginx is an open-source web server application that enables the delivery of static and dynamic web applications or services on your server. Nginx works as a web server, load balancer, reverse proxy or HTTP cache to integrate with existing applications to form a stack or deliver web applications using your server IP address or domain.
This article explains how to install the Nginx web server on Ubuntu 24.04 and set up sample web applications to run on your server.
Prerequisites
Before you begin:
- Deploy an Ubuntu 24.04 instance on Vultr.
- Create a new domain or subdomain A record that points to your server IP address. For example,
app.example.com
. - Access the server using SSH and create a non-root user with sudo privileges.
- Update the server.
Install Nginx
The latest Nginx package is available in the default APT repositories on Ubuntu 24.04. Follow the steps below to update the server packages and install the Nginx web server application.
Update the server package index.
console$ sudo apt update
Install Nginx.
console$ sudo apt install nginx -y
View the installed Nginx version on your server.
console$ sudo nginx -version
Your output should be similar to the one below:
nginx version: nginx/1.24.0 (Ubuntu)
Manage the Nginx System Service
Nginx uses the nginx
systemd service profile to control the web server run time, and processes on your server. Follow the steps below to enable the Nginx system service and manage the web server processes on your server.
Enable the Nginx web server to start automatically at boot time.
console$ sudo systemctl enable nginx
Output:
Synchronizing state of nginx.service with SysV service script with /usr/lib/systemd/systemd-sysv-install. Executing: /usr/lib/systemd/systemd-sysv-install enable nginx
Start the Nginx service.
console$ sudo systemctl start nginx
Stop the Nginx service.
console$ sudo systemctl stop nginx
Restart the Nginx service.
console$ sudo systemctl restart nginx
View the Nginx service status and verify that it's running.
console$ sudo systemctl status nginx
Output:
● 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 Wed 2024-06-26 10:55:50 UTC; 1min 0s ago Docs: man:nginx(8) Process: 2397 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 2399 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 2400 (nginx) Tasks: 2 (limit: 1068) Memory: 1.7M (peak: 2.4M) CPU: 13ms CGroup: /system.slice/nginx.service ├─2400 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;" └─2401 "nginx: worker process"
Based on the above output value
Active: active (running)
, Nginx is active and running on your server. If the service activity status includesActive: active (failed)
, stop any processes running on the HTTP port80
and restart thenginx
service.
Create a new Nginx Virtual Host
Nginx virtual hosts include special configurations that enable the web server to deliver web application files from a specific directory using a specific domain on your server. In the following steps, create a new sample Nginx virtual host configuration to securely deliver web application files on your server.
Create a new Nginx virtual host configuration in the
/etc/nginx/sites-available
directory. For example,app.example.com.conf
.console$ sudo nano /etc/nginx/sites-available/app.example.com.conf
Add the following configurations to the file.
nginxserver { 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 close the file.
The above Nginx virtual host configuration listens for incoming connections using your
app.example.com
domain and serves your web application files from the/var/www/app.example.com
web root directory on your server.Test the Nginx configuration for errors.
console$ sudo nginx -t
Output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Link the configuration to the
/etc/nginx/sites-enabled
directory to activate the virtual host on your server.console$ sudo ln -s /etc/nginx/sites-available/app.example.com.conf /etc/nginx/sites-enabled/
Create a new web root directory
/var/www/app.example.com
to store your web application files.console$ sudo mkdir -p /var/www/app.example.com
Create a new HTML application file within the directory. For example,
index.html
.console$ sudo nano /var/www/app.example.com/index.html
Add the following HTML contents to the file.
html<html> <head></head> <body> <h1>Greetings from Vultr</h1> </body> </html>
Save and close the file.
The above HTML application displays a
Greetings from Vultr
heading when accessed in a web browser.Restart Nginx to apply the new virtual host configurations on your server.
console$ sudo systemctl restart nginx
Access your domain to verify that Nginx correctly delivers the virtual host web application files on your server. For example, use the Curl utility to test access to your domain.
console$ curl http://app.example.com
Output:
Greetings from Vultr
Secure the Nginx Web Server
SSL certificates enable encrypted communications between a user's browser and the Nginx web server using HTTPS. By default, Nginx listens for incoming connections on the insecure HTTP port 80
. Follow the steps below to generate trusted Let's Encrypt SSL certificates and secure the Nginx web server to enable encrypted HTTPS connection requests.
Install the Certbot Let's Encrypt client package using Snap.
console$ sudo snap install --classic certbot
View the installed Certbot version on your server.
console$ sudo certbot --version
Output:
certbot 2.11.0
Generate a new SSL certificate for your domain. Replace
app.example.com
with the actual domain in your Nginx virtual host configurations.console$ sudo certbot --nginx -d app.example.com --agree-tos
Set Up Firewall Rules
Uncomplicated Firewall (UFW) is available and active on Ubuntu 24.04 by default. In the following steps, configure the firewall with new connection rules to enable the Nginx web server listen for incoming HTTP and HTTPS connections on your server.
Allow network connections on the HTTP port
80
.console$ sudo ufw allow 80/tcp
Allow connections on the HTTPS port
443
.console$ sudo ufw allow 443/tcp
View the UFW table and verify that the new connection rules are active.
console$ sudo ufw status
Output:
Status: active To Action From -- ------ ---- 22/tcp ALLOW Anywhere 80/tcp ALLOW Anywhere 443/tcp ALLOW Anywhere 22/tcp (v6) ALLOW Anywhere (v6) 80/tcp (v6) ALLOW Anywhere (v6) 443/tcp (v6) ALLOW Anywhere (v6)
Conclusion
You have installed Nginx on your Ubuntu 24.04 server and configured the web server to deliver web applications on your server. Nginx supports multiple virtual host configurations you can use to securely deliver web applications on your server. In addition, you can integrate the Nginx web server with other applications such as MySQL and PHP to deliver dynamic web applications on your server. For more information and configuration options, visit the official Nginx documentation.