Skip to main content



How to Set Up Nginx on a VPS Hosting Server

How to Set Up Nginx on a VPS Hosting Server

If you are looking to host websites or web applications on a virtual private server, getting your Nginx VPS setup right from the very beginning is essential. Nginx (pronounced “engine-x”) is one of the most popular and powerful web servers available today. It is known for its high performance, low memory usage, and excellent ability to handle large numbers of concurrent connections. Whether you are running a personal blog, an e-commerce platform, or a complex web application, Nginx is a reliable choice that scales beautifully with your needs.

In this guide, we will walk you through the complete process of setting up Nginx on a VPS hosting server, from initial installation through to basic configuration and testing. By the end, you will have a fully functional web server ready to serve your content to the world.

What Is Nginx and Why Use It on a VPS?

Nginx is an open-source web server that also functions as a reverse proxy, load balancer, and HTTP cache. It was originally created by Igor Sysoev and released in 2004, primarily to solve the C10k problem — the challenge of handling ten thousand concurrent connections efficiently.

When compared to Apache, Nginx uses an asynchronous, event-driven architecture rather than a thread-based one. This means it consumes far less memory when handling many simultaneous requests, making it an ideal choice for VPS environments where resources are often limited. A proper Nginx VPS setup can serve thousands of visitors without breaking a sweat, even on a modest server configuration.

Prerequisites Before You Begin

Before diving into the installation process, make sure you have the following in place:

  • A VPS running Ubuntu 20.04, Ubuntu 22.04, or Debian (this guide focuses on these distributions)
  • Root or sudo access to the server
  • A basic understanding of the Linux command line
  • SSH access to your VPS
  • A domain name pointed to your server’s IP address (optional but recommended)

If you are just getting started with VPS hosting and need guidance on choosing the right plan or managing your server environment, you can find a wealth of helpful resources at da-manager.com/blog.

Step 1: Update Your Server’s Package Index

Before installing any new software, it is always good practice to update your package lists to ensure you are pulling the latest versions from the repository. Connect to your VPS via SSH and run the following commands:

sudo apt update

sudo apt upgrade -y

This ensures your system is fully up to date and reduces the risk of compatibility issues during installation.

Step 2: Install Nginx

Once your system is updated, installing Nginx is straightforward. Run the following command:

sudo apt install nginx -y

The package manager will download and install Nginx along with any required dependencies. Once the installation is complete, Nginx should start automatically.

Verify That Nginx Is Running

To confirm that Nginx has started successfully, use the following command:

sudo systemctl status nginx

You should see output indicating that the service is active and running. If it is not running, you can start it manually with:

sudo systemctl start nginx

You can also enable Nginx to start automatically on boot with:

sudo systemctl enable nginx

Step 3: Configure the Firewall

If you have UFW (Uncomplicated Firewall) enabled on your VPS, you will need to allow HTTP and HTTPS traffic through the firewall. Nginx registers itself as an application profile with UFW during installation, making this easy to configure.

To allow both HTTP (port 80) and HTTPS (port 443) traffic, run:

sudo ufw allow 'Nginx Full'

If you only want to allow HTTP traffic for now, you can use:

sudo ufw allow 'Nginx HTTP'

Verify the firewall rules are in place with:

sudo ufw status

Step 4: Test Your Nginx Installation

Open a web browser and navigate to your server’s IP address. For example:

http://your_server_ip

If everything is working correctly, you should see the default Nginx welcome page displaying the message “Welcome to nginx!” This confirms that your Nginx VPS setup is functioning as expected and that the server is serving web content correctly.

Step 5: Configure a Server Block (Virtual Host)

Nginx uses server blocks to manage multiple websites on a single server, similar to Apache’s virtual hosts. Setting up a server block for your domain is a crucial part of any Nginx VPS setup.

Create a Directory for Your Website

First, create a directory to hold your website’s files:

sudo mkdir -p /var/www/yourdomain.co.uk/html

Assign the appropriate permissions:

sudo chown -R $USER:$USER /var/www/yourdomain.co.uk/html

sudo chmod -R 755 /var/www/yourdomain.co.uk

Create a Sample HTML Page

Create a simple index file to test your configuration:

nano /var/www/yourdomain.co.uk/html/index.html

Add a basic HTML structure and save the file. This will be the page served when someone visits your domain.

Create the Server Block Configuration File

Navigate to the sites-available directory and create a new configuration file:

sudo nano /etc/nginx/sites-available/yourdomain.co.uk

Add the following configuration:

server {
    listen 80;
    listen [::]:80;

    root /var/www/yourdomain.co.uk/html;
    index index.html index.htm;

    server_name yourdomain.co.uk www.yourdomain.co.uk;

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

Save and close the file, then enable the server block by creating a symbolic link to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/yourdomain.co.uk /etc/nginx/sites-enabled/

Step 6: Test and Reload Nginx Configuration

Before reloading Nginx, always test your configuration for syntax errors:

sudo nginx -t

If the output shows that the configuration test is successful, reload Nginx to apply the changes:

sudo systemctl reload nginx

Your website should now be accessible via your domain name.

Step 7: Secure Your Nginx Server With SSL

No Nginx VPS setup is complete without SSL/TLS encryption. The easiest way to add HTTPS to your server is by using Let’s Encrypt, a free and widely trusted certificate authority. Install Certbot and the Nginx plugin with:

sudo apt install certbot python3-certbot-nginx -y

Then run Certbot to obtain and install a certificate for your domain:

sudo certbot --nginx -d yourdomain.co.uk -d www.yourdomain.co.uk

Follow the on-screen prompts. Certbot will automatically modify your Nginx configuration to enable HTTPS and set up automatic certificate renewal.

Common Nginx Optimisation Tips

Once your basic Nginx VPS setup is complete, consider implementing the following optimisations to improve performance and security:

  • Enable Gzip compression: Reduces the size of files sent from your server, speeding up page load times.
  • Configure caching headers: Instruct browsers to cache static assets such as images, CSS, and JavaScript files.
  • Limit request rates: Use Nginx’s built-in rate limiting to protect against brute-force attacks and excessive traffic.
  • Disable server tokens: Hide the Nginx version number from response headers to reduce information exposure to potential attackers.
  • Use HTTP/2: Enable HTTP/2 in your server block for improved performance on modern browsers.

Final Thoughts

Completing a proper Nginx VPS setup does not have to be a daunting task. By following the steps outlined in this guide, you now have a fully functional, secure, and optimised web server running on your virtual private server. From installing Nginx and configuring the firewall to creating server blocks and enabling SSL, each step builds towards a robust hosting environment capable of serving your website reliably.

As your needs grow, Nginx’s flexibility means it can evolve with you — whether that involves acting as a reverse proxy for a Node.js application, a load balancer for multiple backend servers, or a high-performance static file server. With the