使用Docker快速建立WordPress环境

我整理了一种在不破坏本地环境的情况下,随意准备 WordPress 环境的方法。我尽可能以适合初次使用 WordPress 的人理解的方式进行了描述。


做好准备

请按照以下的步骤安装Docker。

Windows安装步骤,请参考以下链接:http://docs.docker.jp/docker-for-windows/step_one.html

Windows10 Professonal 64bit下载页面
https://hub.docker.com/editions/community/docker-ce-desktop-windows/

除以上之外的Windows下载页面
https://github.com/docker/toolbox/releases

以下是Mac的安装步骤:
http://docs.docker.jp/docker-for-mac/step_one.html

写这篇文章时的环境是如下所示。

    • MacOS Catalina 10.15.3

 

    • Docker 19.03.5

 

    docker-compose 1.25.4

创建工作目录

可以在任何地方创建。由于可以在文件夹中管理环境,所以如果想要创建多个环境,只需创建多个文件夹即可。

mkdir wordpress1

准备DB服务器和AP服务器的定义。

使用文本编辑器创建一个包含以下内容的文件,并以 docker-compose.yml 的名称保存在创建的文件夹中。(字符编码为 UTF-8 无 BOM)

version: "3.7" # docker-composeの書式のバージョン
services: # 以下にサーバ情報を記載
  db: # データベースサーバ(MySql)名(任意の名前を指定可)
    image: mysql:5.6 # 使用するデータベースのイメージ名とバージョン(https://hub.docker.com/_/mysql)
    restart: always # PC再起動時などDocker自体が起動したときにこのサーバも自動で起動するよう設定
    environment: # 環境変数
      MYSQL_ROOT_PASSWORD: root # rootユーザのパスワード(任意に指定可)
      MYSQL_DATABASE: wordpress_db # WordPress用データベース名(任意に指定可)
      MYSQL_USER: wordpress_user # WordPress用接続ユーザ名(任意に指定可)
      MYSQL_PASSWORD: wordpress_pass # WordPress用パスワード(任意に指定可)
  ap: # アプリケーションサーバ(PHP,WordPress)名(任意の名前を指定可)
    image: wordpress:latest # 使用するWordPressのイメージ名とバージョン(latestは最新を表す https://hub.docker.com/_/wordpress)
    restart: always # PC再起動時などDocker自体が起動したときにこのサーバも自動で起動するよう設定
    depends_on: # 先に起動させるサーバを指定
     - db
    ports:
     - "10080:80" # アプリケーションサーバの80番ポートをローカルの10080番ポートにつなげる(http://localhost:10080 でアクセスできるようになる)
    environment:
      WORDPRESS_DB_HOST: db:3306 # データベースサーバ名:ポート番号
      WORDPRESS_DB_USER: wordpress_user # WordPress用接続ユーザ名(dbの内容に合わせる)
      WORDPRESS_DB_PASSWORD: wordpress_pass # WordPress用パスワード(dbの内容に合わせる)
      WORDPRESS_DB_NAME: wordpress_db # WordPress用データベース名(dbの内容に合わせる)

用这个设置,将创建两个服务器,一个是db,另一个是ap。
※当准备多个环境时,请确保端口号ports: 10080不重复,可以设置为10081、10082等。其他设置保持不变,没有问题。


服务器启动

在wordpress1目录中执行以下命令。

docker-compose up -d

我会等待一段时间直到执行完成。


确认启动

您可以使用以下命令来检查每个服务器的状态。

docker-compose ps

如果显示如下,则表示状态已设置为启动状态。

     Name                   Command               State           Ports        
-------------------------------------------------------------------------------
wordpress_ap_1   docker-entrypoint.sh apach ...   Up      0.0.0.0:10080->80/tcp
wordpress_db_1   docker-entrypoint.sh mysqld      Up      3306/tcp             

在开始使用WordPress之前需要进行的基本设置。

在浏览器中访问 http://localhost:10080。

image.png

当出现语言选择界面时,请选择日本语并点击“继续”按钮。


必要的信息输入

在网站上输入标题、用户名、密码、电子邮件地址,然后点击“安装WordPress”按钮。

image.png

登录

点击「登录」按钮。

image.png

输入预设的用户名和密码,点击“登录”按钮。

image.png
image.png

进入DB服务器,查看表信息。

在WordPress1目录中执行以下命令。

docker-compose exec db mysql -uwordpress_user -p wordpress_db
Enter password: 

当系统要求输入密码时,请输入”wordpress_pass”。

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 34
Server version: 5.6.47 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

MySQL客户端已启动。


数据库列表

您可以使用以下命令查看数据库的列表。

show databases;

已经创建了docker-compose.yml中指定的wordpress_db。

+--------------------+
| Database           |
+--------------------+
| information_schema |
| wordpress_db       |
+--------------------+
2 rows in set (0.00 sec)

桌子列表

您可以使用以下命令来查看表的列表。

show tables;

已经创建了用于WordPress的表格。

+------------------------+
| Tables_in_wordpress_db |
+------------------------+
| wp_commentmeta         |
| wp_comments            |
| wp_links               |
| wp_options             |
| wp_postmeta            |
| wp_posts               |
| wp_term_relationships  |
| wp_term_taxonomy       |
| wp_termmeta            |
| wp_terms               |
| wp_usermeta            |
| wp_users               |
+------------------------+
12 rows in set (0.00 sec)

桌子上的內容 de

您可以使用以下命令来查看表的内容。

select * from wp_users;
+----+------------+------------------------------------+---------------+------------------+----------+---------------------+---------------------+-------------+--------------+
| ID | user_login | user_pass                          | user_nicename | user_email       | user_url | user_registered     | user_activation_key | user_status | display_name |
+----+------------+------------------------------------+---------------+------------------+----------+---------------------+---------------------+-------------+--------------+
|  1 | user       | $P$B6hIB70MCnEgXDjdLvlU12ORlbDyL4. | user          | info@example.com |          | 2020-02-21 16:16:10 |                     |           0 | user         |
+----+------------+------------------------------------+---------------+------------------+----------+---------------------+---------------------+-------------+--------------+
1 row in set (0.00 sec)

从数据库服务器退出

执行 exit。

exit

登录AP服务器

刚才就像进入了DB服务器一样,可以使用 docker-compose exec 服务器名 执行命令进入。

用下面的指令进入AP服务器。

docker-compose exec ap sh

当您进入后,应该会显示以下内容。

#

查看当前目录

您可以使用pwd命令来确认当前所在的目录。

pwd
/var/www/html

查看文件列表

你可以使用ls命令来查看文件/目录的列表。

ls
index.php    readme.html      wp-admin            wp-comments-post.php  wp-config.php  wp-cron.php  wp-links-opml.php  wp-login.php  wp-settings.php  wp-trackback.php
license.txt  wp-activate.php  wp-blog-header.php  wp-config-sample.php  wp-content     wp-includes  wp-load.php        wp-mail.php   wp-signup.php    xmlrpc.php

查看文件的内容

您可以使用”cat”命令来查看文件的内容。

cat wp-config.php

您可以查看数据库的连接设置等。

<?php
/**
 * The base configuration for WordPress
 *
 * The wp-config.php creation script uses this file during the
 * installation. You don't have to use the web site, you can
 * copy this file to "wp-config.php" and fill in the values.
 *
 * This file contains the following configurations:
 *
 * * MySQL settings
 * * Secret keys
 * * Database table prefix
 * * ABSPATH
 *
 * @link https://codex.wordpress.org/Editing_wp-config.php
 *
 * @package WordPress
 */

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress_db');

/** MySQL database username */
define( 'DB_USER', 'wordpress_user');

/** MySQL database password */
define( 'DB_PASSWORD', 'wordpress_pass');

/** MySQL hostname */
define( 'DB_HOST', 'db:3306');

/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '');

/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define( 'AUTH_KEY',         'cbaae291c44c536f6b94cbb071ac6c66cf068300');
define( 'SECURE_AUTH_KEY',  '759e4f7c08b219ef6276aab82a342091c6cf0618');
define( 'LOGGED_IN_KEY',    'a50c09c57d2aed6d2ecbd3560dc239e42c9e06ed');
define( 'NONCE_KEY',        '580347a26116c13cbb5883d91ec8a7d56587a82a');
define( 'AUTH_SALT',        '03e501a4860d9993968f79eb029f4646ff865bcf');
define( 'SECURE_AUTH_SALT', '28108878c043111feacf6cfb174743b9d353bde4');
define( 'LOGGED_IN_SALT',   '5ed8b34550fb494f125531bfd209ea8e66468401');
define( 'NONCE_SALT',       '41c843fd2b0183f7ee4c70215d6d5f62f29dfabd');

/**#@-*/

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix = 'wp_';

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the Codex.
 *
 * @link https://codex.wordpress.org/Debugging_in_WordPress
 */
define( 'WP_DEBUG', false );

// If we're behind a proxy server and using HTTPS, we need to alert WordPress of that fact
// see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
        $_SERVER['HTTPS'] = 'on';
}

/* That's all, stop editing! Happy publishing. */

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
        define( 'ABSPATH', dirname( __FILE__ ) . '/' );
}

/** Sets up WordPress vars and included files. */
require_once( ABSPATH . 'wp-settings.php' );

从AP服务器中退出

如果使用”exit”命令来结束shell,你就可以退出。

exit

将文件从服务器复制到本地

在未进入服务器的情况下,执行 docker cp (容器名称):(复制源路径) (复制目标路径) 命令。
※ 容器名称是由 docker-compose ps 显示的 name 来确定的(没有 docker-compose cp)。
※ 路径需要使用完整路径进行指定。

     Name                   Command               State           Ports        
-------------------------------------------------------------------------------
wordpress_ap_1   docker-entrypoint.sh apach ...   Up      0.0.0.0:10080->80/tcp
wordpress_db_1   docker-entrypoint.sh mysqld      Up      3306/tcp             

您可以使用以下命令将AP服务器的wp-config.php文件复制到本地的/tmp目录中。

docker cp wordpress_ap_1:/var/www/html/wp-config.php /tmp

将文件从本地复制到服务器上

可以使用 docker cp (复制源路径) (容器名称):(复制目标路径) 以完成刚才相反的操作。

当想要在本地修改配置文件并应用到服务器时,可以按照以下方式进行操作。

docker cp /tmp wordpress_ap_1:/var/www/html/wp-config.php

如果你想直接在服务器上使用编辑器修改文件,可以使用VSCode的远程开发插件来实现。

image.png

安装插件后,选择左侧屏幕图标。

image.png
image.png
image.png

打开一个可以编辑服务器内文件的窗口。

image.png

点击「打开文件夹」按钮,输入/var/www/html,并点击「确定」按钮。

image.png

在AP服务器中,文件将被列出并且可以自由编辑。

image.png

重设为初始状态的方法 shè de

当你在进行各种编辑时导致环境出现问题,希望恢复到最初的状态时,你可以使用”docker-compose down”来清除缓存,然后使用”docker-compose up”重新启动,从语言选择界面重新开始。请注意,数据库也会回到初始状态,请谨慎操作。
※ 如果你不再需要该环境,请在执行”docker-compose down”后,删除wordpress1文件夹即可。

docker-compose down
Stopping wordpress_ap_1 ... done
Stopping wordpress_db_1 ... done
Removing wordpress_ap_1 ... done
Removing wordpress_db_1 ... done
Removing network wordpress_default

如果了解这个地方,您应该可以根据自己的喜好来创建或清除环境。


如果在Windows环境下浏览器出现错误的情况下

21.PNG

因为在Windows环境下进行了类似的环境搭建后,服务器启动后在浏览器中无法正常显示,因此追加了解决方法。

环境是什么。

    • Windows10Home 64bit

 

    • DockerToolbox-19.03.1.exe をインストール

 

    docker-compose version 1.24.1

Docker版本号为19.03.1。

是的。

根据调查结果显示,DockerToolbox需要进行设置,因为它在后台运行一个名为VirtualBox的虚拟机。

需要进行VirtualBox的端口转发设置。设置步骤如下:


VirtualBox的端口转发设置

15.PNG

我要启动桌面上的Oracle VM VirtualBox。

16.PNG

按下设定按钮,然后在打开的「网络」选项卡中按下「高级」中的「端口转发」按钮。

17.PNG
image.png
18.PNG

如果出现以下画面,请点击“允许访问”按钮。

19.PNG

这应该可以运行的,但不知为什么,在我的环境中,如果主机端口不是80,它就无法正常工作。如果主机端口设置为10080时无法正常工作,请将其设置为80,并将docker-compose.yml中的ports:替换为80:80,
然后执行docker-compose down
docker-compose up -d
如果打开http://localhost,应该能正常运行。

203.PNG

这样就会显示下面的画面。

image.png
广告
将在 10 秒后关闭
bannerAds