How to Install Nginx on a VPS

Written by rubi | Last updated on
How to Install Nginx on a VPS

Introduction: Why Your VPS Needs Nginx

 

So, you've rented a Virtual Private Server (VPS). Congratulations! You now have a powerful slice of the internet all to yourself. But right now, it's likely just a bare operating system—like a house with a foundation but no doors or windows. To get your website online, you need a web server. The best in the business? Nginx (pronounced "engine-x").

 

Nginx is a high-performance web server that handles thousands of connections simultaneously without breaking a sweat. It's lightweight, incredibly efficient at serving static files (like images, CSS, and JavaScript), and can also act as a reverse proxy and load balancer [citation:3][citation:4]. Companies like Netflix and Dropbox use it, which means it's more than capable of handling your blog or business site [citation:4].

 

In this guide, I'll walk you through installing and configuring Nginx on a Linux VPS (specifically Ubuntu, the most beginner-friendly distribution). We'll go from a fresh server to a live website with your own custom HTML page. Let's get started.

 

Prerequisites: What You'll Need

 

  • A VPS: Any provider will work (e.g., DigitalOcean, Vultr, Linode, AWS). A plan with at least 1GB of RAM and a simple Linux distribution is perfect [citation:4].
  • Ubuntu Installed: This guide uses Ubuntu commands. If you have another distribution like CentOS, the commands will differ slightly (using yum instead of apt) [citation:3].
  • Root or Sudo Access: You need administrative privileges to install software.
  • SSH Client: A terminal to connect to your server (like Terminal on Mac/Linux or PuTTY on Windows).
  • Your Server's IP Address: You'll need this to connect and view your site.

 

Step 1: Connect to Your VPS via SSH

 

First, open your terminal and connect to your server. Replace your_server_ip with your actual VPS IP address.

 

ssh root@your_server_ip

 

If you set up a different user (which is a good security practice), use that username instead of 'root'. Once logged in, you'll be at the command line, ready to work [citation:3].

 

Step 2: Install Nginx

 

Installing Nginx on Ubuntu is surprisingly simple. We'll use the apt package manager.

 

First, update your package list to make sure you're downloading the latest version:

 

sudo apt update

 

Now, install Nginx:

 

sudo apt install nginx -y

 

That's it! Nginx is now installed. The -y flag automatically confirms the installation prompt [citation:1][citation:10].

 

Step 3: Adjust the Firewall (UFW)

 

Before we can see our server, we need to tell the firewall to allow web traffic. If you're using Ubuntu's Uncomplicated Firewall (UFW), Nginx comes with a few pre-configured profiles.

 

Check which applications are registered with UFW:

 

sudo ufw app list

 

You should see outputs like 'Nginx HTTP', 'Nginx HTTPS', and 'Nginx Full'. We'll enable both HTTP (port 80) and HTTPS (port 443) by allowing the 'Nginx Full' profile [citation:1].

 

sudo ufw allow 'Nginx Full'

 

Now, verify the change:

 

sudo ufw status

 

You should see 'Nginx Full' listed as ALLOWED.

 

Step 4: Check Your Server

 

Nginx should have started automatically after installation. Let's verify that it's running correctly.

 

sudo systemctl status nginx

 

You should see an output that says active (running) in green [citation:4][citation:6].

 

The ultimate test is to see it in a browser. Open your favorite web browser and type in your server's public IP address (e.g., http://100.200.300.400). You should be greeted with the default "Welcome to nginx!" page [citation:1].

 

Can't see the page? Double-check your firewall settings (Step 3) and make sure your VPS provider's firewall/security group also allows traffic on ports 80 and 443 [citation:3][citation:7].

 

Step 5: Create Your Own Website Files

 

The default page is nice, but we want our own site. Nginx serves files from the /var/www/html/ directory by default [citation:6][citation:10]. However, it's better practice to create a separate directory for your domain. Let's create a simple folder and a test page.

 

Create a new directory for your site (replace 'example.com' with your domain or a name you prefer):

 

sudo mkdir -p /var/www/example.com/html

 

Assign ownership to your user (replace 'your_username' with your sudo username, or use the $USER variable if you're logged in as a non-root user):

 

sudo chown -R $USER:$USER /var/www/example.com/html

 

Now, let's create a simple index.html file to prove our setup works [citation:10].

 

nano /var/www/example.com/html/index.html

 

Paste in some simple HTML:

 

<html>
  <head>
    <title>My Awesome VPS Site</title>
  </head>
  <body>
    <h1>Success! Nginx is working.</h1>
    <p>My VPS is now serving my own custom page.</p>
  </body>
</html>

 

Save the file (Ctrl+X, then Y, then Enter).

 

Step 6: Configure a Server Block (Virtual Host)

 

To tell Nginx to serve our new site, we need to create a server block (similar to Apache's virtual hosts).

 

Nginx keeps its configuration files in /etc/nginx/. The main places to look are [citation:4][citation:6]:

  • /etc/nginx/sites-available/: Where you store configuration files for each site.
  • /etc/nginx/sites-enabled/: Where you place symbolic links to the configs you want to actually run.

 

Create a new configuration file for your site in the 'sites-available' directory:

 

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

 

Add the following configuration. This tells Nginx to listen on port 80 for your domain and to serve files from the directory we created [citation:3][citation:10].

 

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/example.com/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

 

Save and exit the file.

 

Now, activate this configuration by creating a symbolic link to the 'sites-enabled' directory:

 

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

 

Step 7: Test and Reload Nginx

 

Always test your configuration for syntax errors before reloading. A simple mistake can break your site [citation:4].

 

sudo nginx -t

 

If you see "test is successful," you're good to go. Now, reload Nginx to apply the changes:

 

sudo systemctl reload nginx

 

Visit your server's IP address (or your domain, if it's pointed there) in your browser. You should now see your custom "Success! Nginx is working" page!

 

Conclusion: You're Live!

 

Congratulations! You've successfully installed Nginx on your VPS and configured it to serve a custom website. You've moved from a blank server to hosting your own content on the web. This is a huge step.

 

Next Steps & Final Checklist [citation:4]:

 

  • Enable Gzip Compression: Add gzip on; to your Nginx config to make your site load faster.
  • Set Up a Domain: Point your domain name to your VPS's IP address.
  • Install an SSL Certificate: Use Let's Encrypt to get a free certificate and enable HTTPS. It's essential for security and SEO.
  • Hide Nginx Version: Add server_tokens off; to your config for a small security boost.

 

Nginx is a powerful tool, and you've just unlocked its potential. Happy hosting!

Ready to Get Started?

Join thousands of satisfied customers who have chosen BuyVPS for their hosting needs.

View Plans & Pricing

Use promo code 5050 for 50% off your first month!