In this post I will demonstrate how you can easily set up your VPS to host multiple websites using docker compose and Nginx as a reverse proxy.
Features
- Can host any number of different websites on a single VPS
- Each website can have it’s own domain name
- HTTPS with Automatic creation and renewal of SSL certificates
- Websites can be added and removed without the need to manually change the configuration
Prerequisites
- Hosted VPS accessible from the internet
- Basic knowledge of shell/bash commands
- Installed Docker with Docker Compose
- Basic knowledge of Docker and Docker Compose
Setup
1. Create a Directory for Nginx
Create a directory for your Nginx setup. For example:
mkdir /home/docker/nginx
cd /home/docker/nginx
2. Define Reverse Proxy Services
In the new directory, create a file named docker-compose.yml
with the following content:
version: "3"
services:
nginx-proxy:
image: nginxproxy/nginx-proxy
restart: always
container_name: nginx-proxy
ports:
- "80:80"
- "443:443"
volumes:
- ${VOLUME_DIR}/certs:/etc/nginx/certs
- ${VOLUME_DIR}/vhost:/etc/nginx/vhost.d
- ${VOLUME_DIR}/html:/usr/share/nginx/html
- /var/run/docker.sock:/tmp/docker.sock:ro
nginx-proxy-acme:
image: nginxproxy/acme-companion
restart: always
container_name: nginx-proxy-acme
environment:
- DEFAULT_EMAIL
volumes_from:
- nginx-proxy
volumes:
- /opt/docker-volumes/nginx/acme:/etc/acme.sh
- /var/run/docker.sock:/var/run/docker.sock:ro
networks:
default:
name: nginx-proxy
external: true
The file defines 2 services:
nginx-proxy
will be forwarding incoming requests to the specific websites hosted on this VPSnginx-proxy-acme
will be responsible for automatic creation and renewal of SSL certificates for HTTPS
3. Add Service Configuration
In the same directory, create a file named .env
(including the dot) with the following content:
VOLUME_DIR=/home/docker/volumes/nginx
[email protected]
This file specifies some properties for the previously declared services:
- VOLUME_DIR specifies the directory which will be used by Docker to store data for the declared services. You can chose any appropriate location.
- DEFAULT_EMAIL is the email address for registering the SSL certificates with Letsencrypt. It should be a valid email that you have access to.
4. Create a Shared Network
The docker-compose.yml
file is referring to an external network called nginx-proxy
. This will be the network that connects the reverse proxy services with the websites behind it. In order for this setup to work, we have to create the network by running the following command:
docker network create nginx-proxy
5. Start The Services
Now all that is left to do is to start the reverse proxy services. You can do that by executing:
docker compose up -d
The -d
parameter tells docker compose to run in detached mode, which will enable you to use the same shell for further operations.
To stop the services, you can execute:
docker compose down
Next Steps
You now have a fully functioning reverse-proxy setup on your server, which is able to host multiple websites on multiple domains with HTTPS.
Now you are ready to host your first website. For example, you can use my guide on how to host a WordPress website.
Leave a Reply