WordPress Docker Setup with MySQL, Nginx

WordPress Docker Setup with MySQL, Nginx

Here we can set up WordPress PHP FPM and MySQL docker container on Ubuntu/Debian or any linux machines. And also with nginx configuration for WordPress FPM setup. This article is very use for bloggers / beginners with single machine setup.

MySQL Docker setup

I’m using MySQL version 8.0 which is the latest image that available in docker hub. And here the docker run command to deploy the docker container. And you can customize the name and passwords accordingly.

docker run -d --name mysql-wp --net=host  -v ./db_data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mypassword -e MYSQL_DATABASE=wordpress -e MYSQL_USER=wordpress -e MYSQL_PASSWORD=mypassword mysql:8.0

It used default port 3306, and also you can harden the security with this link.

And this will mount the data volume in the given directory. So the data will never lose even after deleting the container.

WordPress PHP FPM Docker setup

Setting up WordPress on same machine with the following command. And I’m using the official WordPress docker image with the latest PHP version 8.0 with FPM. PHP-FPM is faster than traditional CGI-based methods, So it’s well recommended for production.

docker run -d --name wordpress -v nginx/html:/var/www/html --net=host -e WORDPRESS_DB_USER=wordpress -e WORDPRESS_DB_PASSWORD=mypassword -e WORDPRESS_DB_NAME=wordpress -e WORDPRESS_DB_HOST= wordpress:php8.0-fpm

nginx/html – This path should serve by the nginx. So set the path accordingly. And set password that you have mentioned for MySQL setup. And this uses port 9000.

Note: –net=host is uses main network interface for faster communication.

Nginx Configuration for WordPress

This is the special configuration for WordPress FPM setup in Nginx. So make sure that, this configuration is applied on nginx. For nginx setup, click here

location / {
        try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
    include fastcgi_params;
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;

After added these configurations in nginx, then restart the nginx container. And in the configuration is mentioned which communicate to same machine, so make sure you have WordPress on same machine else mention correct IP of WordPress.

Then change the owner of the file under nginx/html directory, which is also used by the WordPress container. www-data is the username which is used by the WordPress container.

cd nginx/html && sudo chown -R www-data:www-data wp-* index.php xmlrpc.php 

Wrap up

By this way you can get optimized WordPress setup with PHP FPM and also these containers is mounted under the filesystem So it will not get deleted even after killed/deleting the container. You can now see the webpage under 80 port. And after this setup, make sure you have blocked port 9000 and 3306 to the public for security.

Leave a Reply

Your email address will not be published.