You are currently viewing WordPress Deployment with Nginx, PHP-FPM, and MariaDB

WordPress Deployment with Nginx, PHP-FPM, and MariaDB

WordPress is the most popular website builder and content management system in the world. It has a market share of 41% of websites on the internet. With WordPress, you can make a website, start a blog, or create an online store. It comes with thousands of free and premium plugins that work as addons to extend the features and functionality on your site.

There are several ways to get WordPress. The easiest is through a hosting, provide, but today we choose to download and install it by ourselves. So, in this article, we’ll learn how to deploy WordPress using Nginx, PHP-FPM, and MariaDB by using Docker Compose.

Add MariaDB configuration

Create a docker-compose.yml file and add the below configuration to the file. The configuration for MariaDB database is listed under the service name mysql.

services:
  mysql:
    image: mariadb:latest
    volumes:
      - /data/mysql: /var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: mysql_root_password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress_user_password
    restart: always
    port: 3306:3306

Add WordPress configuration

Add WordPress configuration to the docker-compose.yml file as a service named  wordpress.

version: '3'
services:
  mysql:
    image: mariadb:latest
    volumes:
      - /data/mysql: /var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: mysql_root_password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress_user_password
    restart: always
    port: 3306:3306
  wordpress:
    image: wordpress:php7.4-fpm-alpine
    volumes:
      - /data/html: /var/www/html
    depends_on:
      - mysql
    environment:
      WORDPRESS_DB_HOST: mysql
      MYSQL_ROOT_PASSWORD: mysql_root_password
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress_user_password
      WORDPRESS_TABLE_PREFIX: wp_
    port: 9000:9000
    links:
      - mysql
    restart: always

Make sure the WordPress environment variables match exactly with the MariaDB environment variables respectively. In case of any mismatch, the WordPress application would not be able to connect to the database.

Add Nginx configuration

Add Nginx configuration to the docker-compose.yml file as a service named  nginx.

version: '3'
services:
  mysql:
    image: mariadb:latest
    volumes:
      - /data/mysql: /var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: mysql_root_password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress_user_password
    restart: always
    port: 3306:3306
  wordpress:
    image: wordpress:php7.4-fpm-alpine
    volumes:
      - /data/html: /var/www/html
    depends_on:
      - mysql
    environment:
      WORDPRESS_DB_HOST: mysql
      MYSQL_ROOT_PASSWORD: mysql_root_password
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress_user_password
      WORDPRESS_TABLE_PREFIX: wp_
    port: 9000:9000
    links:
      - mysql
    restart: always
  nginx:
    image: nginx:latest
    volumes:
      - nginx: /etc/nginx/conf.d
      - /data/html: /var/www/html
    ports:
      - 80:80
    links:
      - wordpress

We also need to provide a configuration file for the Nginx server. Create a file named wordpress.conf and put the below contents.

server {
  listen 80;
  access_log off;
  root /var/www/html;
  index index.php index.html index.htm;
  server_name example.com;
  server_tokens off;
  location / {
    try_files $uri $uri/ /index.php?$args;
  }
  # pass the PHP scripts to FastCGI server listening on wordpress:9000
  location ~ .php$ {
    fastcgi_split_path_info ^(.+.php)(/.+)$;
    fastcgi_pass wordpress_ip:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  }
}

Now create the containers and run the services with the below command.

$ sudo docker-compose up -d

Once the installation is over, let’s install the Certbot which is a free, open-source software tool for automatically using Let’s Encrypt certificates on manually-administrated websites to enable HTTPS. With Certbot’s help, we can easily convert an HTTP site to a secure HTTPS site using a nonprofit certificate authority-provided SSL/TLS certificate. We can run the following two commands to get Certbot installed. 

apt install certbot
apt install python-certbot-nginx

The last step is to run Certbot to apply a SSL/TLS certificate for our Nginx website. It will automatically configure the necessary configuration on our Nginx configuration. Here is the command to apply certificate and make changes on configuration file:

certbot --nginx
service nginx restart

After restart nginx service, open up the URL https://your_ip in your browser. This should show the WordPress initial setup page.

發佈留言