Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

NodeBB

  1. Home
  2. VPS Tutorial
  3. Install Nginx on Ubuntu Tutorial

Install Nginx on Ubuntu Tutorial

Scheduled Pinned Locked Moved VPS Tutorial
1 Posts 1 Posters 34 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cuong
    wrote last edited by
    #1

    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 apt package management system to install it.

    First, update your local package index to ensure you have the latest metadata:

    sudo apt update
    

    Next, install the Nginx package:

    sudo apt install nginx
    

    After the installation is complete, Nginx will start automatically. You can verify the installed version by typing:

    nginx -v
    

    Step 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 list
    

    You 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 status
    

    Step 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 nginx
    

    To 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.com
    

    Type the IP address you receive into your web browser:
    http://your_server_ip

    You 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/html directory intact and create a new directory for your domain:

    sudo mkdir -p /var/www/your_domain/html
    

    2. Assign ownership of the directory:

    sudo chown -R $USER:$USER /var/www/your_domain/html
    

    3. Ensure correct permissions:

    sudo chmod -R 755 /var/www/your_domain
    

    4. Create a sample index.html page:
    Open a new file using your preferred text editor (like nano) :

    nano /var/www/your_domain/html/index.html
    

    Paste 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_domain
    

    Paste the following configuration, ensuring you update the server_name to 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-enabled directory:

    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.conf
    

    Find the server_names_hash_bucket_size directive and remove the # symbol to uncomment it. Save and exit.

    8. Test and Restart Nginx:

    Test your configuration files for syntax errors:

    sudo nginx -t
    

    If the test is successful, restart Nginx to apply your changes:

    sudo systemctl restart nginx
    

    Nginx 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.

    1 Reply Last reply
    0

    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
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    Powered by NodeBB Contributors
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups