SSD Grow

SSD_Grow

Use Docker & Nginx to Host Multiple Websites on a Single-Performance Linux VPS

Docker is a fantastic tool for running multiple services on a single-performance Linux VPS without interference. For example, you can host one website built on WordPress and another on Ghost or a flat-file CMS. However, containerizing software often raises the question: How do you host multiple websites, each in a separate Docker container, from one Performance Linux VPS? By default, Docker services listen on port 80, leading to conflicts for incoming traffic. While you can change the listening port, no one wants to type in coolwebsite.com:34567. Instead, you can use Nginx to route traffic from coolwebsite.com to a container listening on port 34567 and traffic from anothercoolwebsite.net to a container on port 45678.

Nginx-proxy solves this problem by listening on port 80 (the standard HTTP port) and forwarding incoming requests to the appropriate container. This process, known as reverse proxying, leverages Docker’s VIRTUAL_HOST variable. In this tutorial, we’ll set up nginx-proxy and demonstrate how to use Docker and Nginx to route traffic to different containers, enabling you to host multiple websites on different domains from a single Performance Linux VPS.

An Ubuntu/Debian/CentOS server with Docker installed A non-root user with sudo privileges.

Step 1. Starting up nginx-proxy to hook Docker and Nginx together

To get started, let’s start up the nginx-proxy container. This can be accomplished either by a single docker command or using docker-compose. Let’s cover both.

To get started, create a Docker network

Before we get started, either way, we need to first create a Docker network that we will use to bridge all of these containers together.

$ docker network create nginx-proxy

From now on, we need to ensure that we’re always adding new containers to the nginx-proxy Docker network.

Installing nginx-proxy with Docker

$ docker run -d -p 80:80 --name nginx-proxy --net nginx-proxy -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy

Installing nginx-proxy with docker-compose

First, create a new docker-compose.yml file in the directory of your choosing (one titled nginx-proxy is a good idea), and copy in the following text:

Version: "3"
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
ports:
- "80:80"
volumes:
-/var/run/docker.sock:/tmp/docker.sock:ro


networks:
default:
external:
name: nginx-proxy

Then run the following docker-compose command to get started.

$ docker-compose up -d

Nginx-proxy works to host multiple websites

As shown in both code options, the container listens on port 80 and exposes the same port inside the container, allowing all incoming traffic to flow through Nginx.

You might wonder about the purpose of the line /var/run/docker.sock:/tmp/docker.sock. This line grants any container access to the host’s Docker socket, which contains information about Docker events, such as container creation or shutdown.

This setup enables nginx-proxy to detect new containers via the socket, automatically generate the necessary configuration files to route traffic, and restart Nginx to apply the changes immediately. nginx-proxy identifies containers with the VIRTUAL_HOST variable enabled, which is crucial for our setup.

Additionally, the –net nginx-proxy line in the Docker command and the networks: default: external: name: nginx-proxy block in the docker-compose.yml file are important. They ensure that all containers communicate over the designated Docker network.

Step 2. Adding a container to the proxy

Now that have nginx-proxy running, can start adding new containers, which will be automatically picked up and configured for. As covered in the last Docker tutorial, and since it’s an easy implementation to try out, let’s use WordPress as an example.

Using Docker  

Starting a WordPress container with a basic configuration is quite easy.

$ docker run -d --name blog --expose 80 --net nginx-proxy -e
VIRTUAL_HOST=blog.DOMAIN.TLD wordpress

Take note of a few elements here. –expose 80 will allow traffic to flow into the container on port 80. –net nginx-proxy ensures we’re using the Docker network we created earlier. Finally, -e VIRTUAL_HOST=blog.DOMAIN.TLD flags nginx-proxy to create the proxy information and direct traffic arriving to the correct domain.

Using docker-compose

Start by creating a ‘docker-compose.yml’ file in an empty directory and copying in the following.

$ version: "3"

services:
db_node_domain:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: PASSWORD
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: PASSWORD
container_name: wordpress_db

wordpress:
depends_on:
- db_node_domain
image: wordpress:latest
expose:
- 80
restart: always
environment:
VIRTUAL_HOST: blog.DOMAIN.TLD
WORDPRESS_DB_HOST: db_node_domain:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: PASSWORD
container_name: wordpress
volumes:
db_data:

networks:
default:
external:
name: nginx-proxy

Again, take note of the expose and environment: VIRTUAL_HOST options within the file. Also, the networks option at the bottom is necessary to allow this container to “speak” with nginx-proxy.

Also, take note of the line that begins with db_node_domain. If you are going to host multiple WordPress blogs using this method, you need to create unique database names for each. You should replace db_node_domain with your preferred naming scheme. You also need to update two other fields with this chosen name. First:

$ ddepends_on:
- db_node_domain

And second:

$ WORDPRESS_DB_HOST: db_node_domain:3306

Once the configuration file is set up, you can run the docker-compose up -d command. As long as your DNS is set up to route traffic properly, things should work correctly.

From here, you can start up any number of additional WordPress sites—or any type of service, for that matter—and have them automatically added to the nginx-proxy network. This Docker and Nginx configuration is pretty infinitely extensible, limited only by the Performance Linux VPS resources available to you.

Additional resources to host multiple websites

Be sure to check out the extensive documentation for nginx-proxy to learn more about configuring more complex setups between Docker and Nginx, such as those involving SSL, multiple ports, or multiple networks. Although haven’t tested it yet, there’s a companion tool called letsencrypt-nginx-proxy-companion that automatically creates and renews Let’s Encrypt certificates directly alongside the proxy itself.

Conclusion

Using Docker and Nginx to host multiple websites on a high-performance linux vps is an efficient and scalable solution. By leveraging Docker’s containerization and Nginx’s reverse proxy capabilities, you can manage and route traffic to various services seamlessly. This setup not only maximizes the use of your Performance Linux VPS resources but also simplifies the deployment and management of multiple websites. Whether you’re running WordPress, Ghost, or other CMS platforms, Docker and Nginx provide a robust framework for handling multiple sites with ease

Related Articles