Website construction: How to build WordPress using Docker

There are times when you want to send information on the Web in various scenes. We describe the procedure for building a website that will be a means of doing so.
In this knowledge, as the first step, we will describe how to install WordPress, which is a content management system (CMS) that makes it easy to build a website.
This procedure uses Docker to facilitate development and operation.

 

Sponsored Links

Server environment that is the premise of this procedure

The server environment for executing this procedure is as follows.

  • If you use IaaS on the cloud such as AWS or GCP or a rental server as the server environment, you can publish the website as it is. However, since it is possible to install it on your own PC, you may first install it on your own PC as a test.
    • Since this procedure is an installation procedure using Docker, it is easy to migrate to a server on the cloud after installing it as a test on a PC.
  • OS uses Linux such as Ubuntu and CentOS.

 

How to install Docker and Docker Compose

When installing WordPress, it is possible to install it on a physical server or PC as it is. However, it is not easy to build and operate.
そTherefore, in this knowledge, Docker and Docker Compose are used to virtualize the server environment and WordPress is installed in it. This procedure facilitates construction and operation. In particular, since the virtualized server environment can be migrated to another PC or server as it is, it is easy to migrate to the server on the cloud after testing on your own PC.

 

How to set up Docker for virtual server for WordPress

  • Make a directory to put the Docker Compose file for the WordPress virtual server.
    • The following directories are examples. This directory can be anywhere you like.
$ mkdir -p ~/web/top/docker_compose/
$ cd ~/web/top/docker_compose/

 

  • Make a Docker Compose file for the WordPress virtual server.
    • In the example below, vi is used as the editor, but there is no problem using anything such as emacs.
$ vi docker-compose.yml

 

  • Copy the following contents and save it in the “docker-compose.yml” file.
  • With this setting, you can download and build a WordPress virtual server and a MySQL database virtual server.
version: '3'

services:
  db:
    image: mysql:5.7
    volumes:
      - ./volumes/db_data:/var/lib/mysql
    environment:
      MYSQL_DATABASE: top_wordpress_db
      MYSQL_USER: user01
      MYSQL_PASSWORD: passWord01@
      MYSQL_RANDOM_ROOT_PASSWORD: "yes"
    restart: always

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "80:80"
    volumes:
      - ./volumes/wordpress/html:/var/www/html
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_NAME: top_wordpress_db
      WORDPRESS_DB_USER: user01
      WORDPRESS_DB_PASSWORD: passWord01@
    restart: always

Details of the above settings will be described. This setting is an example, so modify it if necessary.

  • Database virtual server settings
    • Use MySQL 5.7.
    • Set “./volumes/db_data” on the physical PC or server to “/var/lib/mysql” on the virtual server. Since there is an actual database file under “/var/lib/mysql”, it is necessary to update the file. Virtual servers using Docker should not have files to update. Therefore, this setting saves the file on the physical server in cooperation with the physical PC or the directory on the server side.
    • The DB name is “top_wordpress_db”.
    • The user name in the DB is “user01”.
    • The password for the above user is “passWord01@”.
    • Randomly generate the initial password of the root user in the DB by pwgen.
    • If it stops due to a failure, it will restart automatically.
  • WordPress virtual server settings
    • WordPress virtual server is built after the database virtual server.
    • Use the latest version of WordPress.
    • Open port 80 of the virtual server by connecting it to port 80 on the physical PC or server.
    • Set “./volumes/wordpress/html” on the physical PC or server to “/var/www/html” on the virtual server. Since there is a WordPress related actual file under “/var/www/html”, it is necessary to update the file. Virtual servers using Docker should not have files to update. Therefore, this setting saves the file on the physical server in cooperation with the physical PC or the directory on the server side.
    • The database used by WordPress is the above database. Connect with port 3306.
    • Connect to the DB name “top_wordpress_db”.
    • Log in to the DB with the user name “user01”.
    • Use “passWord01@” as the password for the above user.
    • If it stops due to a failure, it will restart automatically.

 

Make a directory on the physical server that works with the directory on the virtual server side described in the above settings.

  • Make “./volumes/db_data” on the physical server as “/var/lib/mysql” of the virtual server for database.
  • Make “./volumes/wordpress/html” on the physical server as “/var/www/html” of the virtual server for WordPress.
$ mkdir -p ./volumes/db_data
$ mkdir -p ./volumes/wordpress/html

 

How to start a WordPress virtual server & How to check the website

  • Download, install, and start the virtual server for WordPress using Docker Compose based on the created file.
$ docker-compose up -d

 

  • Access the following URL with a web browser.
http://localhost/
 Or http://127.0.0.1/ 

 

  • If the WordPress screen is displayed, this procedure is complete. The language selection screen is displayed.
  • After that, perform the initial settings according to the WordPress screen. The procedure for this setting is described in other knowledge.
Sponsored Links