Set up multiple WordPress sites on Ubuntu using Docker Compose and Nginx.
Table of contents#
Open Table of contents
Prerequisites and usage#
- Ubuntu server 20.04
Step 1: Install docker and nginx#
-
Update the system:
sudo apt update sudo apt upgrade -y
-
Install the necessary dependencies:
sudo apt install build-essential
-
Install Nginx:
sudo apt install nginx
-
Install Docker
sudo apt install apt-transport-https ca-certificates curl software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable" apt-cache policy docker-ce sudo apt install docker-ce
-
Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
Step 2: Create a Docker Compose File and Run#
-
Create a directory for your WordPress project and navigate into it::
mkdir your-domain cd your-domain
-
Create a stack.yml file with the following content:
nano stack.yml
Paste following code to file:
version: '3.3' services: wordpress_your_domain: image: wordpress restart: always ports: - 8000:80 environment: WORDPRESS_DB_HOST: db_your_domain WORDPRESS_DB_USER: your_domain WORDPRESS_DB_PASSWORD: your_password WORDPRESS_DB_NAME: your_domain volumes: - ./data:/var/www/html db_your_domain: image: mysql:5.7 restart: always environment: MYSQL_DATABASE: your_domain MYSQL_USER: your_domain MYSQL_PASSWORD: your_password MYSQL_RANDOM_ROOT_PASSWORD: '1' volumes: - db_your_domain:/var/lib/mysql volumes: db_your_domain:
-
Run docker with docker compose compose:
docker-compose -f stack.yml up -d
Step 3: Point domain to a WordPress with Nginx#
- Set Up DNS Records: First, make sure your domain’s DNS settings are configured to point to your server’s IP address. This typically involves setting an A record for your domain (e.g., example.com) and any subdomains (e.g., www.example.com) to your server’s IP address.
- Configure Nginx for Your Domain
Create an Nginx configuration file for your domain. Replace example.com with your actual domain name.
Add the following content to the file:
sudo nano /etc/nginx/conf.d/example.com.conf
server { listen 80; server_name example.com www.example.com; client_max_body_size 64M; location / { # point domain to docker port we setting in file stack.yml proxy_pass http://localhost:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_redirect off; proxy_buffering off; } }
- Restart Nginx:
sudo systemctl restart nginx
Step 4: Install wordpress on your domain#
You will see tutorial setup wordpress when access to your domain. With this, you have completed the setup of wordpress on ubuntu with docker and nginx.