本文最后更新于 63 天前,其中的信息可能已经有所发展或是发生改变。
拉取Wordpress镜像
docker pull wordpress
成功结果
[root@iZ2vcd0kjbs3o9w48wk5uzZ ~]# docker pull wordpress
Using default tag: latest
latest: Pulling from library/wordpress
a2abf6c4d29d: Pull complete
c5608244554d: Pull complete
2d07066487a0: Pull complete
1b6dfaf1958c: Pull complete
32c5e6a60073: Pull complete
90cf855b27cc: Pull complete
8b0f1068c586: Pull complete
5355461305e8: Pull complete
ad1eec592342: Pull complete
e03fbc76cb78: Pull complete
1f5796e48b39: Pull complete
72fbe8e1d4e7: Pull complete
96edece66175: Pull complete
5f46f0743de2: Pull complete
c9f9671a5e1f: Pull complete
3f543dcd35b1: Pull complete
c88e21a0c2a0: Pull complete
964b4457a910: Pull complete
0d55fb9a64ef: Pull complete
fb009ff7c567: Pull complete
4f058a67a50d: Pull complete
Digest: sha256:fc33b796b04162a0db2e9ea9b4c361a07058b21597b1317ad9ab3ea4593de241
Status: Downloaded newer image for wordpress:latest
docker.io/library/wordpress:latest
启动WordPress容器
docker run -it --name wordpress -p 9999:80 -v /usr/local/share/wordpress:/var/www/html -d wordpress
#命令解释
--name wordpress => 为容器指定一个名称,此处命名为 wordpress
-p 9999:80 => 将容器内部的端口映射到宿主机的端口,此处容器将80端口映射到宿主机的9999端口
-v /usr/local/share/wordpress:/var/www/html => 使用挂载卷将宿主机的目录挂载到容器内部,此处是将宿主机的 /usr/local/share/wordpress 挂载到容器内部的 /var/www/html 目录。宿主机目录是什么意思呢,就是打开 ftp 你所看到的目录就是宿主机上实际的目录,操作(增删改)宿主机上 /usr/local/share/wordpress 这个目录下的文件,等同于操作容器内部的 /var/www/html 这个目录,可以看成自动同步吧,我是这么理解的
-d => 这个参数代表以 “detached” 模式运行容器,就是在后台运行
-d 后面的wordpress => 就是刚才拉取的 wordpress 镜像名称
这样使用数据卷挂载容器目录的方法,就可以将容器运行时产生的数据持久化存储起来,即使容器被删除或者重新创建,数据也不会丢失,仍然存在于宿主机的目录下。
启动成功
[root@iZ2vcd0kjbs3o9w48wk5uzZ share]# docker run -it --name wordpress -p 9999:80 -v /usr/local/share/wordpress:/var/www/html -d wordpress
b424653b20383304922bcb74a4ed63bbe4685d0c6cf8afa8d9a839e7b1914177
安装配置Mysql
拉取mysql镜像并挂载到宿主机目录
docker run -d --name mysql -v /usr/local/share/mysql/data:/var/lib/mysql -v /usr/local/share/mysql/conf:/etc/mysql/conf.d -v /usr/local/share/mysql/logs:/var/log/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=你的密码 mysql:8
#不要直接复制粘贴!按照上述命令,根据自己的实际情况修改,运行之后 docker 会自动拉取mysql:8 的镜像的;其中,/usr/local/share/mysql/data、/usr/local/share/mysql/conf、/usr/local/share/mysql/logs 是我宿主机上的目录,换成你服务器上的目录,可以统一找个地方放
拉取启动成功
[root@iZ2vcd0kjbs3o9w48wk5uzZ share]# docker run -d --name mysql -v /usr/local/share/mysql/data:/var/lib/mysql -v /usr/local/share/mysql/conf:/etc/mysql/conf.d -v /usr/local/share/mysql/logs:/var/log/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=你的密码 mysql:8
Unable to find image 'mysql:8' locally
8: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:8
5996dc92a0dda5f4d23e821cc4734e8b4c0ddc0088696fc2f7893d4690b3bf99
进入mysql容器修改密码
查看容器id
[root@iZ2vcd0kjbs3o9w48wk5uzZ share]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5996dc92a0dd mysql:8 "docker-entrypoint.s…" 33 seconds ago Up 32 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
b424653b2038 wordpress "docker-entrypoint.s…" 19 minutes ago Up 19 minutes 0.0.0.0:9999->80/tcp, :::9999->80/tcp wordpress
进入容器
docker exec -it 5996dc92a0dd /bin/bash
修改配置文件
前置准备:在容器内安装vim,然后编辑文件
#更新apt命令
apt-get update
#安装vim命令
apt-get install vim
- 解释: docker容器实际上就是运行的一个进程,只不过由于docker帮助我们包装了这个进程,给这个进程加以一个可运行的微linux环境而已,让我们感觉看起来”像”虚拟机而已,所以说一个新的环境是没有安装vim的需要自己安装才能编辑文件
ALTER USER 'root'@'%' IDENTIFIED BY '你的密码' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'localhost' IDENTIFIED BY '你的密码';
设置外部访问
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
创建数据库:
create database wordpress;
退出容器
退出mysql
exit
退出容器
exit
重启mysql容器
docker restart mysql
查看mysql容器的ip
docker inspect mysql | grep IPAddress
结果
[root@iZ2vcd0kjbs3o9w48wk5uzZ share]# docker inspect mysql | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.18.0.3",
"IPAddress": "172.18.0.3",
修改信息
原配置文件内容
/** MySQL database username */
define( 'DB_USER', 'root' );
/** MySQL database password */
define( 'DB_PASSWORD', '你的密码' );
/** MySQL hostname */
define( 'DB_HOST', '172.18.0.3' );
/** 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', '' );
站点登录
修改wordpass上传文件限制
[root@iZ2vcd0kjbs3o9w48wk5uzZ share]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5996dc92a0dd mysql:8 "docker-entrypoint.s…" 3 hours ago Up 41 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
b424653b2038 wordpress "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:9999->80/tcp, :::9999->80/tcp wordpress
[root@iZ2vcd0kjbs3o9w48wk5uzZ share]# docker exec -it wordpress /bin/bash
root@b424653b2038:/var/www/html# cd usr/local/php/conf.d
bash: cd: usr/local/php/conf.d: No such file or directory
root@b424653b2038:/var/www/html# cd usr/local/php/
bash: cd: usr/local/php/: No such file or directory
root@b424653b2038:/var/www/html# ls
index.php wp-activate.php wp-comments-post.php wp-config.php wp-includes wp-login.php wp-signup.php
license.txt wp-admin wp-config-docker.php wp-content wp-links-opml.php wp-mail.php wp-trackback.php
readme.html wp-blog-header.php wp-config-sample.php wp-cron.php wp-load.php wp-settings.php xmlrpc.php
root@b424653b2038:/var/www/html# cd /usr/local/etc/php/conf.d
root@b424653b2038:/usr/local/etc/php/conf.d# ls
docker-php-ext-bcmath.ini docker-php-ext-imagick.ini docker-php-ext-sodium.ini opcache-recommended.ini
docker-php-ext-exif.ini docker-php-ext-mysqli.ini docker-php-ext-zip.ini
docker-php-ext-gd.ini docker-php-ext-opcache.ini error-logging.ini
root@b424653b2038:/usr/local/etc/php/conf.d# touch uploads.ini
退出容器后,查找创建的“uploads.ini”文件路径
find / -name "uploads.ini"
/var/lib/docker/overlay2/5e451d9a0701c722e339931214e843ddc89232eec4ff725b38c2ce7d36a06a69/merged/usr/local/etc/php/conf.d/uploads.ini
/var/lib/docker/overlay2/5e451d9a0701c722e339931214e843ddc89232eec4ff725b38c2ce7d36a06a69/diff/usr/local/etc/php/conf.d/uploads.ini
使用wcp等连接软件编辑第一个目录下的该文件
file_uploads = On
memory_limit = 500M
upload_max_filesize = 100M
post_max_size = 100M
max_execution_time = 600
重启wordpress即可
[root@iZ2vcd0kjbs3o9w48wk5uzZ share]# docker restart wordpress
安装Nginx绑定域名
安装nginx
docker pull nginx
结果
[root@iZ2vcd0kjbs3o9w48wk5uzZ ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Already exists
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
以终端的方式打开镜像容器
docker run -i -t nginx /bin/bash
拷贝镜像中 nginx.conf 配置文件的内容
cat /etc/nginx/nginx.conf
结果
[root@iZ2vcd0kjbs3o9w48wk5uzZ ~]# docker run -i -t nginx /bin/bash
root@e5b766c7d337:/# cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
拷贝镜像中 default.conf 配置文件的内容
cat /etc/nginx/conf.d/default.conf
结果:
root@e5b766c7d337:/# cat /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
退出并删除旧的容器,执行新的容器启动命令
启动nginx容器
docker run --name nginx -d -p 80:80 -p 443:443 -v /usr/local/nginx/https:/usr/local/nginx/https -v /usr/local/nginx/html:/usr/share/nginx/html -v /usr/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/nginx/conf.d:/etc/nginx/conf.d -v /usr/local/nginx/logs:/var/log/nginx nginx
会执行失败,这是正常的。
将上述拷贝的文件,替换到宿主机对应的位置后,即可启动成功
isdiudiu.top.conf
#HTTPS server
#wordpress
server {
#SSL 访问端口号为 443
listen 443 ssl;
#填写绑定证书的域名
server_name isdiudiu.top;
#证书文件名称 即.crt文件路径
ssl_certificate /usr/local/nginx/https/isdiudiu.top.pem;
#私钥文件名称 即.key文件路径
ssl_certificate_key /usr/local/nginx/https/isdiudiu.top.key;
ssl_session_timeout 5m;
#请按照以下协议配置
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
#网站主页路径。此路径仅供参考,具体请您按照实际目录操作
location / {
root html;
index index.html index.htm;
#配置代理 因为wordpress默认端口为9999,故使其指向
proxy_pass http://8.137.154.222:9999;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
}
}
#wordpress
server {
listen 80;
#填写绑定证书的域名
server_name isdiudiu.top;
#把http的域名请求转成https
return 301 https://$host$request_uri;
location / {
#配置代理 因为wordpress默认端口为9999,故使其指向
proxy_set_header Host $host;
proxy_pass http://8.137.154.222:9999;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
}
}
强制开启ssl
找到 wordpress 所在目录,如果 wordpress 容器的启动命令是照搬我的话,目录就是这里 /usr/local/share/wordpress
,修改 wp-config.php,加入以下代码
$_SERVER['HTTPS'] = 'on';
define('FORCE_SSL_ADMIN', true);
define('FORCE_SSL_LOGIN', true);
define('WP_HOME', 'https://isdiudiu.top');
define('WP_SITEURL', 'https://isdiudiu.top');
- 摘要:
记录搭建个人博客的心路历程