Install WordPress with Docker Compose using Nginx on Google Cloud Platform. In this article you are going to learn how to install and configure WordPress with Nginx, PHP-FPM on Google Cloud with Docker, Docker Compose and connect to Cloud SQL. We will also install PhpMyAdmin and connect it with Cloud SQL.
This setup is tested on Google Cloud Platform, it will also work on other cloud services, any VPS or any Dedicated servers with Docker.
Prerequisites
- Your Compute Engine Instance running.
- For setting up Compute Engine, see the Setting up Compute Engine Instance.
- Install Docker on Google Cloud Platform.
- Install Docker Compose on Google Cloud.
- Set up Cloud DNS, see the Setting up Google Cloud DNS for your domain.
- Google Cloud SQL Setup, see Setup Cloud SQL and connect with Compute Engine.
This setup can also be installed on any Linux based servers.
Step 1: Setup Directories
Once you have Docker, Docker Compose installed and the Cloud SQL is setup you can proceed to setup directories for the WordPress installation.
SSH to your instance and create the directories as follows.
sudo mkdir wordpress-docker cd wordpress-docker sudo mkdir nginx sudo mkdir wordpress sudo mkdir -p logs/nginx
We will use these directories as volumes for our installation.
Step 2: Configure Nginx
Create a new Nginx configuration inside the nginx folder.
sudo nano nginx/wordpress.conf
Copy the below configurations.
server { listen 80; listen [::]:80; server_name yourdomainname.com; root /var/www/html; index index.php; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ .php$ { try_files $uri =404; fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass wordpress:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } location ~ /.ht { deny all; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { log_not_found off; access_log off; allow all; } location ~* .(css|gif|ico|jpeg|jpg|js|png)$ { expires max; log_not_found off; } }
Step 3: Setup Docker Compose File
Create a docker-compose.yml
file and include the configurations for WordPress.
We will create a container for Nginx and another container for WordPress and connect it to Cloud SQL.
We will also setup PhpMyAdmin to manage the database.
sudo nano docker-compose.yml
Copy and paste the below configurations in your docker compose file.
version: '3' services: wordpress: image: wordpress:5.2.2-php7.2-fpm container_name: wordpress restart: unless-stopped ports: - '9000:9000' environment: WORDPRESS_DB_HOST: Cloud_SQL_IP WORDPRESS_DB_NAME: Database_name WORDPRESS_DB_USER: Database_user_name WORDPRESS_DB_PASSWORD: Database_user_password volumes: - ./wordpress:/var/www/html nginx: image: nginx:latest container_name: nginx restart: unless-stopped ports: - "80:80" - "443:443" volumes: - ./nginx:/etc/nginx/conf.d - ./logs/nginx:/var/log/nginx - ./wordpress:/var/www/html links: - wordpress phpmyadmin: image: phpmyadmin/phpmyadmin restart: unless-stopped ports: - 3333:80 environment: PMA_HOST: Cloud_SQL_IP_Address MYSQL_ROOT_PASSWORD: Cloud_SQL_Root_Password
Now we have our docker-compose.yml
file setup which a wordpress
container wit the latest WordPress image with PHP 7.2 FPM. It will also creates an nginx
container and configure it with the configuration we created above.
This will also downloads the phpmyadmin
image and configure it on a different port and connects it with Cloud SQL.
Step 4: Run Docker-Compose
Now we have the Nginx configuration and Docker Compose configuration ready to be deployed.
Move to the folder that has your docker-compose.yml
file and execute the following command to setup WordPress with Docker containers.
cd ~/wordpress-docker
docker-compose up -d
Now the containers are created using the configured images and will be accessible with the volumes we have created.
Once the setup is completed you can run this command to check the containers.
Step 5: Setup Firewall rules
As we have configured PhpMyAdmin on custom port 3333
, we need to open the port in Firewall.
Go to VPC Network >> Firewall rules and click Create Firewall rules.
In Name enter phpmyadmin
In Targets select All instances in the network
In Source filter select IP ranges
In Source IP ranges enter 0.0.0.0/0
In Protocols and ports check TCP and enter 3333
.
Click Create.
Step 6: Verify WordPress Installation with Docker
Once the setup is complete go to your browser and check your domain name.
http://yourdomainname.com
You will see the default WordPress installation screen. Go ahead and install WordPress.
To check the PhpMyAdmin you can use the port 3333
http://yourdomainname.com:3333
This is connected with Cloud SQL, you can manage your Cloud SQL database from here.
Conclusion
Now you have learned how to install WordPress with Docker and Docker Compose and install PhpMyAdmin and connect it to Cloud SQL.
Why is /var/www/html visible in the nginx container?
( – ./wordpress:/var/www/html )
This does not feel secure, because nginx is a reverse proxy that hides the servers behind it, and yet you make the /var/www/html visibl ein the nginx container.