How to host several sites on one VPS

сервер

Imagine you run multiple websites. You have a VPS and you want to host them in one place. You will save on hosting and make sure that all projects are at your fingertips. In this article we will tell you how to run multiple sites without conflicts on ports and with independent operation. We will use Nginx as a reverse proxy and Docker for containerization.

Please note: the code is given as an example. It is up to you to adapt it to your needs. Or ask for help from your VPS service tech support. The purpose of this article is to show the sequence.

 

Why you need reverse-proxy and Docker

An inexperienced user may be confused when faced with terms like reverse proxy and Docker. For a seasoned professional, these are basic things.

Why do you need a reverse proxy?

Without it, the server that accepts requests on ports 80 and 443 will not understand which site should process the request. Users will get unnecessary responses instead of the site's landing page. Nginx reverse proxy analyzes DNS records and directs requests to the correct site based on the domain name.

And let's not forget about Docker.

Docker is the perfect tool for application isolation. Without isolation, server resources get confused and create conflicts between applications. Docker solves the problem and gives each site its own space.

Configuring DNS

Let's not underestimate the importance of proper DNS configuration. Running multiple sites on one VPS is impossible without setting up DNS records. This is the step that many people miss.

To do this, you need to create A records for each domain and specify the IP address of the VPS. These records will make proper routing of requests to the server. For example, for the domain site1.example.com, create a record:

dns

Repeat this for each site you want to run on your VPS. This step is mandatory if you don't want users to see an error page instead of your site.

Solving the first problem: ports and addressing requests

When you have multiple sites running on the same server, the question becomes: how will Nginx figure out where to direct each request? All sites try to use ports 80 and 443 for HTTP and HTTPS connections. To solve the problem, we'll use a reverse proxy. It intercepts DNS-based requests and redirects them to the right containers.

Installing Nginx and Docker

If you already have a VPS set up, the first thing you should do is install Nginx and Docker. These are tools that allow you to deploy sites in containers and manage requests via reverse proxy.

Updating APT and installing Nginx

nginx

Configure the firewall. Allow HTTP access:

http

Installing Docker

First, let's add a Docker repository and install it:

docker

Launching sites in Docker

Now let's create containers for the sites. In the example we will use CMS Ghost - a lightweight and easy to deploy blogging engine.

Creating a Docker network

network docker

Running containers for two sites

sites

Get the IP addresses of the containers

Get the IP for each site:

ip of sites

Restricting access to Docker containers

Why leave containers open when you can protect them? Use a firewall to restrict access so that only Nginx can communicate with the containers.

Monitoring and management

After getting sites up and running on a VPS, there is still work to be done. Monitoring the performance of containers and Nginx is what separates the pro from the amateur. Docker provides access to container logs with the command:

logs

For complete control, it is better to use monitoring tools such as Netdata or Prometheus. They will give you a complete picture: from the state of servers to resource utilization.

Performance Optimization

Now that the sites are up and running, it's worth thinking about how to improve performance. Nginx offers a lot of optimization options, from caching to load balancing. Enable request caching to reduce the load on the server:

cash

This will allow the server to breathe freer and give users faster access to content.

Configuring the reverse proxy

Now let's configure Nginx to manage requests to each site. We add two configuration files to /etc/nginx/conf.d - one for each site.

Configuration for the first site

configuration

Configuration for the second site

configuration sites

Now the sites are running on the same VPS. In the next step, take care of security and connect SSL certificates. Use Let's Encrypt for free certificates and configure them for Nginx.

Testing and debugging

The last step before going into production is testing and debugging. Never trust the settings blindly. Check the correctness of reverse proxy operation with the help of:

test

This command will show the HTTP headers and make sure the site is returning the correct responses. Be sure to test the sites through your browser to make sure they display and work correctly.

Configuring SSL Certificates

Without a secure HTTPS connection, you risk not only losing user trust, but also falling out of favor with search engines. That's why setting up SSL certificates is not an option, it's a necessity. To do this, you can use Let's Encrypt, which provides free certificates. All you need to do is install Certbot and run the command to get a certificate:

ssl

Now sites are encrypted and traffic between the user and the server is protected.

Running multiple sites on a single VPS may seem like a daunting task, but with proper reverse-proxy setup, Docker and DNS record management, it's easy.

Follow this step-by-step guide. This way, you'll keep your sites stable and secure, and you can scale your infrastructure as your projects grow. Don't forget performance monitoring and optimization - it's key to long-term success.

If you're looking for a stable, fast and reliable VPS for your projects, LikeVPS is a great choice. With their servers and flexible pricing plans, you can scale resources as needed.