Install Nginx on Ubuntu Tutorial
-
This tutorial provides a step-by-step guide to installing and configuring the Nginx web server on Ubuntu. Nginx is a high-performance web server known for its stability, rich feature set, and low resource consumption.
Prerequisites
To follow this guide, you will need:- An Ubuntu server (version 22.04 or later is recommended).
- A non-root user with sudo privileges configured on your server.
- A basic understanding of the Linux command line.
Step 1: Installing Nginx
Nginx is available in Ubuntu’s default software repositories. This means you can use the
aptpackage management system to install it.First, update your local package index to ensure you have the latest metadata:
sudo apt updateNext, install the Nginx package:
sudo apt install nginxAfter the installation is complete, Nginx will start automatically. You can verify the installed version by typing:
nginx -vStep 2: Adjusting the Firewall
Before you can access your web server, you must adjust your firewall settings to allow external access to the default web ports. Nginx registers itself with ufw (Uncomplicated Firewall) upon installation.
List the available application profiles:
sudo ufw app listYou will see several Nginx profiles:
- Nginx Full : Opens port 80 (HTTP) and port 443 (HTTPS).
- Nginx HTTP : Opens only port 80.
- Nginx HTTPS : Opens only port 443.
For a fresh installation without an SSL certificate, enable the HTTP profile:
sudo ufw allow 'Nginx HTTP'Verify the status of the firewall:
sudo ufw statusStep 3: Checking your Web Server
At the end of the installation process, Ubuntu starts Nginx automatically. You can verify that the service is active and running using
systemd:systemctl status nginxTo confirm the server is accessible over the network, visit your server’s IP address in your web browser. If you don't know your server's public IP, you can find it via the command line:
curl -4 icanhazip.comType the IP address you receive into your web browser:
http://your_server_ipYou should see the default "Welcome to nginx!" landing page, which confirms the software is running correctly.
Step 4: Managing the Nginx Process
Now that the web server is up and running, here are some basic management commands:
- Stop Nginx :
sudo systemctl stop nginx - Start Nginx :
sudo systemctl start nginx - Restart Nginx :
sudo systemctl restart nginx - Reload Nginx (apply config changes without dropping connections) :
sudo systemctl reload nginx - Disable Nginx from starting at server boot :
sudo systemctl disable nginx - Enable Nginx to start at server boot (default behavior) :
sudo systemctl enable nginx
Step 5: Setting Up Server Blocks (Recommended)
Server blocks allow you to host more than one domain from a single Nginx server. While the default configuration serves content from
/var/www/html, it is better practice to create a separate directory structure for each site.1. Create the directory for your domain:
Leave the default/var/www/htmldirectory intact and create a new directory for your domain:sudo mkdir -p /var/www/your_domain/html2. Assign ownership of the directory:
sudo chown -R $USER:$USER /var/www/your_domain/html3. Ensure correct permissions:
sudo chmod -R 755 /var/www/your_domain4. Create a sample
index.htmlpage:
Open a new file using your preferred text editor (likenano) :nano /var/www/your_domain/html/index.htmlPaste the following HTML into the file, then save and exit:
<html> <head> <title>Welcome to your_domain!</title> </head> <body> <h1>Success! The your_domain server block is working!</h1> </body> </html>5. Create a new server block configuration file:
sudo nano /etc/nginx/sites-available/your_domainPaste the following configuration, ensuring you update the
server_nameto match your domain:server { listen 80; listen [::]:80; root /var/www/your_domain/html; index index.html index.htm; server_name your_domain www.your_domain; location / { try_files $uri $uri/ =404; } }Save and exit the file.
6. Enable the new server block:
Create a symbolic link from your file to the
sites-enableddirectory:sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/7. Resolve potential hash bucket memory issues:
Open the main Nginx configuration file:
sudo nano /etc/nginx/nginx.confFind the
server_names_hash_bucket_sizedirective and remove the#symbol to uncomment it. Save and exit.8. Test and Restart Nginx:
Test your configuration files for syntax errors:
sudo nginx -tIf the test is successful, restart Nginx to apply your changes:
sudo systemctl restart nginxNginx should now be serving your new domain name. You can test this by navigating to
http://your_domain, where you should see the success page you created in step 4.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login