Nginx is a powerful, high-performance web server used for serving static files, load balancing, and reverse proxying. It is known for its stability, rich feature set, simple configuration, and low resource consumption. In this guide, we’ll walk you through the steps to install Nginx on Ubuntu 24.04 Noble Numbat.
Step 1: Update Your Package List
Before installing Nginx, it’s important to update your package list to ensure you have the latest information on the available packages. Open your terminal and run:
$ sudo apt update
This command will update your local package index.
Step 2: Install Nginx
Once your package list is updated, you can install Nginx by running:
$ sudo apt install nginx
You may be prompted to confirm the installation. Type Y
and press Enter to proceed.
Step 3: Start and Enable Nginx
After the installation is complete, you need to start the Nginx service and enable it to start on boot:
$ sudo systemctl start nginx
$ sudo systemctl enable nginx
To verify that Nginx is running, you can check its status:
$ sudo systemctl status nginx
You should see output indicating that the Nginx service is active and running.
Step 4: Adjust Firewall Settings
If you have a firewall running on your Ubuntu system, you need to allow traffic on HTTP (port 80) and HTTPS (port 443). Use the following commands to allow Nginx traffic:
$ sudo ufw allow 'Nginx HTTP'
$ sudo ufw allow 'Nginx HTTPS'
To check the status of your firewall and confirm that the rules have been added, run:
$ sudo ufw status
Step 5: Verify Nginx Installation
To verify that Nginx is installed and running correctly, open your web browser and enter your server’s IP address:
http://your_server_ip
You should see the default Nginx welcome page, which confirms that Nginx is working correctly.
Step 6: Basic Nginx Configuration
The main Nginx configuration file is located at /etc/nginx/nginx.conf
, and site-specific configurations are stored in the /etc/nginx/sites-available/
directory. To enable a site, create a symbolic link to it in the /etc/nginx/sites-enabled/
directory.
Creating a New Server Block
To set up a new website, you need to create a new server block file. For example, to create a new server block for example.com
:
- Create the configuration file:
$ sudo nano /etc/nginx/sites-available/example.com
- Add the following configuration to the file:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
}
- Save and close the file.
- Create the document root directory:
$ sudo mkdir -p /var/www/example.com/html
- Set the correct permissions:
$ sudo chown -R $USER:$USER /var/www/example.com/html
$ sudo chmod -R 755 /var/www/example.com
- Create a sample index.html file:
$ nano /var/www/example.com/html/index.html
Add the following content to the file:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to example.com!</title>
</head>
<body>
<h1>Success! The example.com server block is working!</h1>
</body>
</html>
Save and close the file.
- Enable the server block by creating a symbolic link:
$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
- Test the Nginx configuration for syntax errors:
$ sudo nginx -t
- Reload Nginx to apply the changes:
$ sudo systemctl reload nginx
Conclusion
You have successfully installed and configured the Nginx web server on your Ubuntu 24.04 Noble Numbat system. You can now deploy your websites and applications using Nginx.
Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.