How to Install and Configure Nginx on AnonVM

How to Install and Configure Nginx on AnonVM

Nginx is a fast, efficient, and flexible web server used by millions of websites worldwide. Whether you're hosting a personal website, a business application, or a complex web service, Nginx can handle high traffic loads with ease. In this tutorial, we will walk through the steps to install and configure Nginx on your AnonVM server running Ubuntu or CentOS.

Prerequisites

Before starting, ensure you have:

  • AnonVM VPS or Dedicated Server: A server with root or sudo access.
  • Operating System: This tutorial assumes you are using Ubuntu (20.04/22.04) or CentOS (7/8).
  • A Domain Name (optional): If you plan to set up Nginx for your website, having a domain name configured to point to your server’s IP is ideal.

Step 1: Update Your System

Ensure your system is up to date with the latest patches and security updates. This helps avoid potential issues during installation.

For Ubuntu/Debian-based systems:

sudo apt update sudo apt upgrade -y

For CentOS-based systems:

sudo yum update -y

Step 2: Install Nginx

For Ubuntu/Debian-based systems:

  1. Install Nginx from the official repository:

     
    sudo apt install nginx -y
  2. Check if Nginx is running:

     
    sudo systemctl status nginx

    The status should show that Nginx is active (running).

For CentOS-based systems:

  1. Install EPEL repository (Extra Packages for Enterprise Linux) to get Nginx:

     
    sudo yum install epel-release -y
  2. Install Nginx:

     
    sudo yum install nginx -y
  3. Start Nginx:

     
    sudo systemctl start nginx
  4. Enable Nginx to start on boot:

     
    sudo systemctl enable nginx

Step 3: Configure Firewall for Nginx

If you're running a firewall on your AnonVM server, you need to allow HTTP (port 80) and HTTPS (port 443) traffic.

For Ubuntu/Debian-based systems using UFW:

sudo ufw allow 'Nginx Full' sudo ufw reload

For CentOS-based systems using firewall-cmd:

sudo firewall-cmd --zone=public --add-service=http --permanent sudo firewall-cmd --zone=public --add-service=https --permanent sudo firewall-cmd --reload

Step 4: Verify Nginx Installation

To check that Nginx is working correctly, open your server’s IP address or domain name in a web browser. You should see the Nginx default welcome page, confirming the installation was successful.

Example:

http://your_server_ip/

You should see a page that says "Welcome to nginx!"

Step 5: Configure Nginx for Your Website

Now that Nginx is installed and running, it's time to configure it to serve your website.

  1. Create a directory for your website:

     
    sudo mkdir -p /var/www/yourdomain.com/html
  2. Set proper permissions:

     
    sudo chown -R $USER:$USER /var/www/yourdomain.com/html sudo chmod -R 755 /var/www/yourdomain.com
  3. Create a sample HTML page:

     
    echo "<html> <head><title>Welcome to Your Domain</title></head> <body> <h1>Your domain is working!</h1> </body> </html>" | sudo tee /var/www/yourdomain.com/html/index.html
  4. Create a new server block (virtual host) for your website:

     
    sudo nano /etc/nginx/sites-available/yourdomain.com

    Add the following configuration:

     
    server { listen 80; server_name yourdomain.com www.yourdomain.com; root /var/www/yourdomain.com/html; index index.html; location / { try_files $uri $uri/ =404; } }
  5. Enable the site by creating a symbolic link:

     
    sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
  6. Test the Nginx configuration:

     
    sudo nginx -t

    If the output says “syntax is okay” and “test is successful,” then the configuration is correct.

  7. Restart Nginx to apply the changes:

     
    sudo systemctl restart nginx

Step 6: Setting Up SSL with Let's Encrypt (Optional)

For better security, it's recommended to use HTTPS for your website. Let's Encrypt provides a free SSL certificate.

  1. Install Certbot (Let’s Encrypt tool) on Ubuntu/Debian-based systems:

     
    sudo apt install certbot python3-certbot-nginx -y
  2. Install Certbot on CentOS-based systems:

     
    sudo yum install certbot python3-certbot-nginx -y
  3. Obtain and install an SSL certificate:

     
    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
  4. Verify SSL installation: After the process completes, your website should be accessible via HTTPS. You can check by visiting https://yourdomain.com.

  5. Set up auto-renewal for SSL certificates:

     
    sudo systemctl enable certbot.timer

Step 7: Enable Nginx Server Block for Multiple Websites (Optional)

If you want to host multiple websites on a single server, you can add additional server blocks (virtual hosts).

  1. Create a directory for the second website:

     
    sudo mkdir -p /var/www/secondwebsite.com/html
  2. Create a new server block for the second website:

     
    sudo nano /etc/nginx/sites-available/secondwebsite.com

    Add the configuration for the second site:

     
    server { listen 80; server_name secondwebsite.com www.secondwebsite.com; root /var/www/secondwebsite.com/html; index index.html; location / { try_files $uri $uri/ =404; } }
  3. Enable the new site:

     
    sudo ln -s /etc/nginx/sites-available/secondwebsite.com /etc/nginx/sites-enabled/
  4. Test the Nginx configuration:

     
    sudo nginx -t
  5. Restart Nginx:

     
    sudo systemctl restart nginx

Step 8: Monitor and Manage Nginx

To ensure your Nginx server is running smoothly, you can check its status and view logs.

  1. Check the status of Nginx:

     
    sudo systemctl status nginx
  2. View Nginx logs:

    • Access logs:
       
      sudo tail -f /var/log/nginx/access.log
    • Error logs:
       
      sudo tail -f /var/log/nginx/error.log
  3. Stop, start, or restart Nginx:

    • Stop Nginx:
       
      sudo systemctl stop nginx
    • Start Nginx:
       
      sudo systemctl start nginx
    • Restart Nginx:
       
      sudo systemctl restart nginx

Conclusion

You’ve successfully installed and configured Nginx on your AnonVM server. Whether you’re serving a single website or hosting multiple sites, Nginx provides a fast, secure, and highly configurable web server solution. By using SSL certificates from Let’s Encrypt, you can ensure your websites are secure and ready for production.

Was this answer helpful? 0 Users Found This Useful (0 Votes)

Powered by WHMCompleteSolution