In this post, I'm going to explain how to set up multiple WordPress sites on a Raspberry Pi using Docker and docker-compose. This article will only discuss the setup and basic concepts, we assume that you already have a domain name that you want to use, and that your Raspberry Pi has settings to connect to and receive external connections, including firewall settings, sharer settings, etc. will not be discussed.
Table of Contents
Installing docker and docker-compose on a Raspberry pi
For the sake of simplicity, we will utilize Docker and docker-compose to deploy our site and the support we need. Therefore, we will start by installing Docker and docker-compose.
What is Docker?
Docker is an open platform for developing and running applications. The original main purpose was to make it easier to solve the problem of inconsistencies between development and deployment environments. Using Docker allows us to package an application and all its dependencies into a unit called a "container". These containers can run anywhere, which means you can develop and test on your local machine, and then deploy on a variety of cloud environments, virtual machines, or bare metal, without making any changes.
Because Docker is so easy to use and containers are so lightweight compared to traditional virtual machines, many non-developers familiar with the environment now often use it in tutorials or deployment tooling situations, similar to the use of virtual machines.
What is docker-compose?
docker-compose is a Docker tool for defining and running multiple Docker containers. It allows users to define and run multiple Docker containers using the
docker-compose.yml file to configure all the services of the application, and then start all the services with just one command.
For example, in a common deployment site scenario, the file might consist of a web server, a database, and a cache, so using docker-compose, you can deploy the file in a
docker-compose.yml Define all these services in a file and make sure they run on the same network, share storage and so on. Not only is it easy to manage, it's also easy to reuse.
Installation steps for Docker and docker-compose
Below are the installation steps, which were tested and confirmed on a Raspberry Pi 4 with Ubuntu server 20.04. First we install Docker.
curl -sSL https://get.docker.com | sh sudo usermod -aG docker ubuntu
Next, reboot your computer and run the following command to install docker-compose.
sudo apt-get install -y libffi-dev libssl-dev sudo apt-get install -y python3 python3-pip sudo apt-get remove python-configparser sudo pip3 -v install docker-compose
Setting up nginx-proxy and LetsEncrypt
In order to support multiple WordPress sites, we need to setup two other containers, nginx-proxy and LetsEncrypt, before starting the Wordpress container.
What is nginx-proxy?
nginx-proxy is a reverse proxy server. There are many functions of a reverse proxy, and since we're not talking about web technology, we'll just provide an intuitive explanation here. nginx-proxy in our environment today acts like a switchboard lady, and when a phone call (a web request) comes in, it helps to route the call to the right place. With this, we can use multiple Wordpress containers on the same Raspberry Pi machine at the same time. The nginx-proxy container will be responsible for routing the web request to the correct Wordpress container for us.
What is LetsEncrypt?
LetsEncrypt's main function here is to allow our website to support https.
Anyone who has been surfing the web has probably seen the https in front of a web address, right? This means that it is a secure web protocol. Imagine you are having a private conversation. You might want to make sure that only you and one other person know what's going on in this conversation, and that no one else can hear or understand it. This is what the security of communication between a website and its users is all about. https is this technology and is implemented using a technology called "SSL/TLS" to ensure this security.
To make this secure communication possible, you need something called an "SSL certificate". This is like an official ID that confirms who you are and what you say you are. Let's Encrypt is one of the organizations that provides these certificates. Let's Encrypt offers SSL certificates completely free of charge, which is perfect for our cost-saving servers. The LetsEncrypt container we're about to set up will handle the process of obtaining the SSL certificates for us.
Setting up a Docker Network
First we need to create a Docker network that all containers use to communicate with each other. This only needs to be done once. Later on, our Wordpress web containers will also communicate with the reverse proxy through this network.
docker network create --driver bridge nginx-proxy
Setting up the nginx-proxy and letsencrypt-nginx-proxy-companion containers
We are going to use nginx-proxy and letsencrypt-nginx-proxy-companion for the two containers mentioned above. First create a folder for the nginx-proxy reverse proxy service:
mkdir nginx-proxy cd nginx-proxy
Next, under this folder, create a docker-compose.yml:
version: "3" services: nginx-proxy: nginx-proxy: nginx-proxy nginx-proxy. image: alexanderkrause/rpi-nginx-proxy container_name: nginx-proxy restart: always container_name: nginx-proxy restart: always - "80:80" - "443:443" volumes. - . /conf:/etc/nginx/conf.d - . /vhost:/etc/nginx/vhost.d - . /html:/usr/share/nginx/html - . /certs:/etc/nginx/certs:ro - /var/run/docker.sock:/tmp/docker.sock:ro - . /nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro labels. - "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy" letsencrypt. image: alexanderkrause/rpi-letsencrypt-nginx-proxy-companion container_name: nginx-proxy-le restart: always depends_on: nginx-proxy-le - nginx-proxy depends_on: nginx-proxy-le - . /conf:/etc/nginx/conf.d - . /vhost:/etc/nginx/vhost.d - . /html:/usr/share/nginx/html - . /certs:/etc/nginx/certs:rw - /var/run/docker.sock:/var/run/docker.sock:ro networks: /etc/nginx/certs:rw /var/run/docker.sock:ro external. name: nginx-proxy
The advantage of Docker is that we can start containers easily. However, for different raspberry pi systems, we sometimes need to find the correct image for the ARM processor architecture. The docker-compose.yml above was tested to run on both Raspbian and Ubuntu installations. The alexanderkrause version of the image used here is tuned to jwilder/nginx-proxy. Jason Wilder's detailed post can be found atHere.Find it. Before we can use it, we also need the nginx.tmpl created by jwilder.
curl -o nginx.tmpl https://raw.githubusercontent.com/jwilder/docker-gen/master/templates/nginx.tmpl
In short, rpi-nginx-proxy is a reverse proxy, and rpi-letsencrypt-nginx-proxy-companion detects and automatically helps us to get certificates from letsencrypt, so that all the URLs that we later set up for each wordpress site will support https.
Next, start the reverse proxy with docker-compose.
docker-compose up -d
Setting up multiple WordPresses with docker-compose Tribune!
Now it's finally time to set up our WordPress site. We're going to keep using docker to make this easier. First we'll create a directory for each site. You can also create a folder if you only want to set up one site.
mkdir wordpress- cd wordpress-
Each site will have its own folder, and in each folder, create a file called docker-compose.yml for the site.
version: '3.3' services. db. container_name: db- image: hypriot/rpi-mysql volumes. - . /db_data:/var/lib/mysql - . /home/db:/home/db - . /sqldump:/sqldump restart: always environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress. container_name: wordpress- depends_on. - wordpress: container_name: wordpress- depends_on: db image: wordpress:latest expose. - 80 restart: always volumes: . - . /data_volume:/var/www/html - . /home/wp:/home/wp environment. VIRTUAL_HOST: <YOUR_SITE_URL LETSENCRYPT_HOST: <YOUR_SITE_URL LETSENCRYPT_EMAIL: WORDPRESS_DB_HOST: db_:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress wordpress WORDPRESS_DB_NAME: wordpress - db:db_ networks: wordpress_db_names: wordpress default: db:db_ external. name: nginx-proxy
Please note that the , , and above need to be defined. Each site's associated container should have its own conatiner_name to ensure there are no conflicts. This will ensure that the wordpress container will always find the correct db to request data from. Otherwise, when you run more than one of these containers, the connection may get messed up because of the duplicate service name.
Now our configuration is complete. For each site, nginx-proxy will automatically detect that they are launched through the nginx-proxy network we set up. The letsencrypt companion will then look at the hostname and email we configured for each site and pull the certificate for us.
Finally, we launch the instance for each site. In each directory of the site, run:
docker-compose up -d
This is great! The letsencrypt companion takes some time to finish pulling certificates when running, so it may take a few minutes after launch, but after a few minutes you should be able to access all sites using the https:// address.
- Low-cost home servers Article Series