Docker-compose部署WordPress

  • Docker-compose部署WordPress
    • 一. 准备部分
    • 二. 编写启动脚本
    • 三.nginx挂载
    • 四. 启动
    • 附录
      • test1 安装mysql
      • test2 编写脚本
      • test3 启动
cd <span class="token operator">/</span>home mkdir <span class="token operator">-</span>p wordpress<span class="token operator">/</span><span class="token punctuation">{</span>db<span class="token punctuation">,</span>db_data<span class="token punctuation">}</span> cd wordpress touch docker<span class="token operator">-</span>compose<span class="token punctuation">.</span>yml cd <span class="token operator">/</span>home<span class="token operator">/</span>wordpress<span class="token operator">/</span>db touch mysql<span class="token punctuation">.</span>cnf
 cd <span class="token operator">/</span>home mkdir <span class="token operator">-</span>p wordpress<span class="token operator">/</span><span class="token punctuation">{</span>db<span class="token punctuation">,</span>db_data<span class="token punctuation">}</span> cd wordpress  touch docker<span class="token operator">-</span>compose<span class="token punctuation">.</span>yml cd <span class="token operator">/</span>home<span class="token operator">/</span>wordpress<span class="token operator">/</span>db  touch mysql<span class="token punctuation">.</span>cnf 
cd /home mkdir -p wordpress/{db,db_data} cd wordpress touch docker-compose.yml cd /home/wordpress/db touch mysql.cnf

此时目录结构

mysql配置

<span class="token function">vi</span> /home/wordpress/db/mysql.cnf <span class="token punctuation">[</span>mysqld<span class="token punctuation">]</span> explicit_defaults_for_timestamp<span class="token operator">=</span>true wait_timeout<span class="token operator">=</span>86400 interactive_timeout<span class="token operator">=</span>7200
<span class="token function">vi</span> /home/wordpress/db/mysql.cnf  <span class="token punctuation">[</span>mysqld<span class="token punctuation">]</span> explicit_defaults_for_timestamp<span class="token operator">=</span>true wait_timeout<span class="token operator">=</span>86400 interactive_timeout<span class="token operator">=</span>7200 
vi /home/wordpress/db/mysql.cnf [mysqld] explicit_defaults_for_timestamp=true wait_timeout=86400 interactive_timeout=7200

Docker-compose.yml

<span class="token function">vi</span> /home/wordpress/docker-compose.yml version: <span class="token string">'2'</span> services: wordpress: image: wordpress ports: - <span class="token string">"8080:80"</span> environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_NAME: wordpress WORDPRESS_DB_USER: root WORDPRESS_DB_PASSWORD: yourpassword db: image: mysql:5.7 volumes: - ./db_data:/var/lib/mysql - ./db/mysql.cnf:/etc/my.cnf restart: always environment: MYSQL_ROOT_PASSWOD: yourpassword
<span class="token function">vi</span> /home/wordpress/docker-compose.yml version: <span class="token string">'2'</span> services: wordpress: image: wordpress ports: - <span class="token string">"8080:80"</span> environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_NAME: wordpress WORDPRESS_DB_USER: root WORDPRESS_DB_PASSWORD: yourpassword db: image: mysql:5.7 volumes: - ./db_data:/var/lib/mysql  - ./db/mysql.cnf:/etc/my.cnf restart: always environment: MYSQL_ROOT_PASSWOD: yourpassword 
vi /home/wordpress/docker-compose.yml version: '2' services: wordpress: image: wordpress ports: - "8080:80" environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_NAME: wordpress WORDPRESS_DB_USER: root WORDPRESS_DB_PASSWORD: yourpassword db: image: mysql:5.7 volumes: - ./db_data:/var/lib/mysql - ./db/mysql.cnf:/etc/my.cnf restart: always environment: MYSQL_ROOT_PASSWOD: yourpassword
<span class="token function">vi</span> /etc/nginx/site-available/default server <span class="token punctuation">{</span> listen 80<span class="token punctuation">;</span> server_name your.domain<span class="token punctuation">;</span> location / <span class="token punctuation">{</span> proxy_pass http://127.0.0.1:8080<span class="token punctuation">;</span> proxy_set_header Host <span class="token variable">$host</span><span class="token punctuation">;</span> proxy_set_header X-Real-IP <span class="token variable">$remote_addr</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> nginx -s reload
<span class="token function">vi</span> /etc/nginx/site-available/default  server <span class="token punctuation">{</span> listen 80<span class="token punctuation">;</span> server_name your.domain<span class="token punctuation">;</span> location / <span class="token punctuation">{</span> proxy_pass http://127.0.0.1:8080<span class="token punctuation">;</span> proxy_set_header Host <span class="token variable">$host</span><span class="token punctuation">;</span> proxy_set_header X-Real-IP <span class="token variable">$remote_addr</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>   nginx -s reload 
vi /etc/nginx/site-available/default server { listen 80; server_name your.domain; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } nginx -s reload
docker-compose up
docker-compose up  
docker-compose up

查看浏览器是否能打开你挂载的网站

情况1.不可以打开

……各种排查错误

情况2.可以打开(假设可以打开)

docker-compose down docker-compose up -d
  docker-compose down  docker-compose up -d 
docker-compose down docker-compose up -d

好的代码都需要测试,虽然我成功过一次,不过为了博客能好用些,我找了台相对干净的服务器来测试下

照着上面的步骤能否部署

我的docker早就装过了 2x版本的

出师不利 第一步遇到问题,新服务器没有更新 apt list

<span class="token function">apt-get</span> update
 <span class="token function">apt-get</span> update 
apt-get update

安装完以后我发现没有弹出填写密码的提示

mysql mysql<span class="token operator">></span> <span class="token keyword">set</span> password <span class="token keyword">for</span> <span class="token string">'root'</span>@<span class="token string">'localhost'</span><span class="token operator">=</span>password<span class="token punctuation">(</span><span class="token string">'yourpassword'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> mysql<span class="token operator">></span> FLUSH PRIVILEGES<span class="token punctuation">;</span> mysql<span class="token operator">></span> <span class="token keyword">exit</span><span class="token punctuation">;</span>
mysql  mysql<span class="token operator">></span> <span class="token keyword">set</span> password <span class="token keyword">for</span> <span class="token string">'root'</span>@<span class="token string">'localhost'</span><span class="token operator">=</span>password<span class="token punctuation">(</span><span class="token string">'yourpassword'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  mysql<span class="token operator">></span> FLUSH PRIVILEGES<span class="token punctuation">;</span>  mysql<span class="token operator">></span> <span class="token keyword">exit</span><span class="token punctuation">;</span> 
mysql mysql> set password for 'root'@'localhost'=password('yourpassword'); mysql> FLUSH PRIVILEGES; mysql> exit;

nginx这块配置我就略过了

首次启动所以才开始拉镜像

第一次启动失败

我缺少个随机密码参数

environment: MYSQL_ROOT_PASSWOD: yourpassword MYSQL_RANDOM_ROOT_PASSWORD: <span class="token string">"1"</span>
environment: MYSQL_ROOT_PASSWOD: yourpassword MYSQL_RANDOM_ROOT_PASSWORD: <span class="token string">"1"</span> 
environment: MYSQL_ROOT_PASSWOD: yourpassword MYSQL_RANDOM_ROOT_PASSWORD: "1"

第二次启动失败

没有连接权限

…经过了近两个半个小时的踩坑,终于部署成功

失败乃成功之母,把踩的坑也记录下来.问题主要是在连接不上数据库

这样的话wordpress就部署上去了。 本篇完

原文链接:https://jiuaidu.com/jianzhan/1066844/

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享