使用docker 部署wordpress 博客网站
环境:centos7、docker、vmwareworkstation虚拟机
1 拉取mysql:5.7 镜像,使用mysql镜像运行一容器
[root@server ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
c499e6d256d6: Already exists
22c4cdf4ea75: Pull complete
6ff5091a5a30: Pull complete
2fd3d1af9403: Pull complete
0d9d26127d1d: Pull complete
54a67d4e7579: Pull complete
fe989230d866: Pull complete
466a91a95e2f: Pull complete
3e4554c238f1: Pull complete
603b48ead88c: Pull complete
1e86a9aa7171: Pull complete
Digest: sha256:fbaeced79cfdae5d3c8d4a8c41e883f254f72ed7428c6b93a498824b76d97121
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
2 运行容器 查看状态
[root@localhost ~]# docker run -d –name=mysql-wpx -p 3306:3306 -v /data/mysql:/var/lib/mysql/ -e MYSQL_ROOT_PASSWORD=000000 mysql:5.7
5c0368eaca68bf1df6cebdeaa969b468ca46f8cc16a855098d7aa8189b9fb401
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c0368eaca68 mysql:5.7 “docker-entrypoint…” 38 seconds ago Up 37 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-wpx
3 拉取wordpress镜像
[root@server ~]# docker pull wordpress
Using default tag: latest
latest: Pulling from library/wordpress
c499e6d256d6: Already exists
3a635b94b3b9: Pull complete
cf28be682a33: Pull complete
b7118ab6e551: Pull complete
925f628a16b8: Pull complete
a77cff9973b5: Pull complete
9b4f44173a15: Pull complete
c9bd6c436ae3: Pull complete
399316af3112: Pull complete
3cea11f54c61: Pull complete
1ebb66998d8f: Pull complete
ac288a1a6e08: Pull complete
776d750cfd59: Pull complete
a997f5c2f033: Pull complete
a41091590f04: Pull complete
6b1397000eb2: Pull complete
7b387d8d3957: Pull complete
04673b988ee3: Pull complete
0e2da6305da6: Pull complete
f0224352bc00: Pull complete
d5e8b4e26a84: Pull complete
Digest: sha256:191d5caf4ef5b8c57721ade777820f3267654325f7902b2ccd377ceeba1c3fe2
Status: Downloaded newer image for wordpress:latest
docker.io/library/wordpress:latest
4 运行wordpress 容器,查看状态
[root@localhost ~]# docker run -d –name wp-wpx -v /data/wordpress:/var/www/html -e WORDPRESS_DB_HOST=10.1.1.1:3306 -e WORDPRESS_DB_PASSWORD=000000 –link mysql-wpx:mysql -p 8080:80 wordpress
d73e590c48d55963fc3ed9ef82f60f67152c58c7cb15e62d3e6110a8be866e5c
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d73e590c48d5 wordpress “docker-entrypoint…” About a minute ago Up About a minute 0.0.0.0:8080->80/tcp wp-wpx
5c0368eaca68 mysql:5.7 “docker-entrypoint…” 7 minutes ago Up 7 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-wpx
5 进入wordpress 容器,输入env命令查看环境变量信息
[root@localhost ~]# docker exec -it d7 /bin/bash
root@d73e590c48d5:/var/www/html# env
MYSQL_PORT_33060_TCP_ADDR=172.17.0.2
MYSQL_PORT=tcp://172.17.0.2:3306
MYSQL_PORT_3306_TCP_ADDR=172.17.0.2
MYSQL_NAME=/wp-wpx/mysql
MYSQL_ENV_MYSQL_ROOT_PASSWORD=000000
MYSQL_PORT_3306_TCP_PORT=3306
HOSTNAME=d73e590c48d5
PHP_VERSION=7.3.16
APACHE_CONFDIR=/etc/apache2
PHP_MD5=
PHP_INI_DIR=/usr/local/etc/php
WORDPRESS_VERSION=5.4
GPG_KEYS=CBAF69F173A0FEA4B537F470D66C9593118BCCB6 F38252826ACD957EF380D39F2F7956BC5DA04B5D
MYSQL_ENV_MYSQL_MAJOR=5.7
PHP_LDFLAGS=-Wl,-O1 -Wl,–hash-style=both -pie
MYSQL_PORT_3306_TCP=tcp://172.17.0.2:3306
PWD=/var/www/html
HOME=/root
MYSQL_ENV_GOSU_VERSION=1.7
MYSQL_PORT_33060_TCP_PROTO=tcp
WORDPRESS_SHA1=d5f1e6d7cadd72c11d086a2e1ede0a72f23d993e
PHP_SHA256=91aaee3dbdc71b69b4f3292f9d99211172a2fa926c3f3bbdb0e85dab03dd2bcb
WORDPRESS_DB_HOST=10.1.1.1:3306
PHPIZE_DEPS=autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c
TERM=xterm
PHP_URL=https://www.php.net/get/php-7.3.16.tar.xz/from/this/mirror
MYSQL_PORT_33060_TCP_PORT=33060
PHP_EXTRA_CONFIGURE_ARGS=–with-apxs2 –disable-cgi
MYSQL_PORT_3306_TCP_PROTO=tcp
SHLVL=1
PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
MYSQL_PORT_33060_TCP=tcp://172.17.0.2:33060
WORDPRESS_DB_PASSWORD=000000
APACHE_ENVVARS=/etc/apache2/envvars
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PHP_EXTRA_BUILD_DEPS=apache2-dev
PHP_ASC_URL=https://www.php.net/get/php-7.3.16.tar.xz.asc/from/this/mirror
PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
MYSQL_ENV_MYSQL_VERSION=5.7.29-1debian10
_=/usr/bin/env
使用cat命令查看/etc/hosts文件信息
root@d73e590c48d5:/var/www/html# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 mysql 5c0368eaca68 mysql-wpx
172.17.0.3 d73e590c48d5
使用cat命令查看/var/html/wp-config.php 信息
root@d73e590c48d5:/var/www/html# 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://wordpress.org/support/article/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’);
/** MySQL database username */
define( ‘DB_USER’, ‘root’);
/** MySQL database password */
define( ‘DB_PASSWORD’, ‘000000’);
/** MySQL hostname */
define( ‘DB_HOST’, ‘10.1.1.1: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’, ‘930c99590c95b2b0a055e020b36df03d32579f6e’);
define( ‘SECURE_AUTH_KEY’, ‘567bdf94825a641b2e31250720910fb6dc063021’);
define( ‘LOGGED_IN_KEY’, ‘b0eac229ec3644d339820ee0be4f9623acc7bfb8’);
define( ‘NONCE_KEY’, ‘c8e3147ce9a763bbce44b97e2cbc787df3c35873’);
define( ‘AUTH_SALT’, ‘f2f982acd0cbddf91eab764789b66f6e79f27778’);
define( ‘SECURE_AUTH_SALT’, ‘326022ad7956610343462091fbecdc460e8bab4b’);
define( ‘LOGGED_IN_SALT’, ‘d64cbc642f7dc88b29d6f5f3271500ba6623bb4d’);
define( ‘NONCE_SALT’, ‘2a7aabcdf0838d2d9bc15047bdd4a57b2ae84a81’);
/**#@-*/
/**
* 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 documentation.
*
* @link https://wordpress.org/support/article/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’, __DIR__ . ‘/’ );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . ‘wp-settings.php’;
6 进入mysql 容器,使用root用户和密码登录mysql数据库,查看是否有wordpress数据库?
[root@localhost ~]# docker exec -it 5c /bin/bash
root@5c0368eaca68:/# mysql -uroot -p000000
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.29 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>
有wordpress数据库
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+——————–+
5 rows in set (0.01 sec)
没有表
mysql> use wordpress;
Database changed
mysql> show tables;
Empty set (0.00 sec)
7 在外网(物理机)的浏览器 使用http:// 容器宿主机的ip:HostPort 访问wordpress站点。如下图所示:
再次进入mysql 容器,使用root用户和密码登录mysql数据库,进入wordpress数据库,再查看是否有表?
有表了
mysql> show tables;
+———————–+
| Tables_in_wordpress |
+———————–+
| 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)
最后值得注意的是:wordpress启动后一定要先查看日志看是否启动正常,如果有apache服务启动有问题可能跟docker
版本、iptables服务有关,mysql至少要用5.6版本以上不要用太新的版本没那么稳定,这里我用的是拉的是mysql5.7。
如果遇到问题可以在评论区大家一起讨论一起学习
原文链接:https://www.cnblogs.com/PGxiaochaoren/p/12682017.html