2021-05-29-使用docker-compose搭建wordpress博客避坑指南

使用docker-compose搭建wordpress博客

  1. 创建文件
mkdir -p /data/mysql_data
mkdir -p /data/wp_data
  1. 编写文档uploads.ini
    • 去除upload的上传限制
file_uploads = On
memory_limit = 64M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 600
  1. docker-compose文件内容
version: '3.3'
services:
   db:
     image: mysql:5.7
     volumes:
       - /data/mysql_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: root
       MYSQL_PASSWORD: 123456
   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     volumes:
       - /data/wp_data/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: root
       WORDPRESS_DB_PASSWORD: 123456
       WORDPRESS_DB_NAME: wordpress
  1. 执行:docker-compose up -d

修饰博客

配置Nginx

  1. 需要使用Nginx映射到wp的服务端口上
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  127.0.0.1:8000;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
	    proxy_pass http://127.0.0.1:8000;
        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#        location = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }

关闭主题自动更新

  1. 在functions.php文件中添加如下代码
define( 'AUTOMATIC_UPDATER_DISABLED', true );

Nginx配置SSL

步骤

  1. 获取免费SSL证书,我这边在阿里云获取的,直接在阿里云搜ssl
  2. 配置Nginx
  3. 配置wordpress

获取SSL证书(略)

配置Nginx

  1. 配置443端口
    • vim /etc/nginx/nignx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    
#    server {
#        listen       80;
#        #listen       [::]:80;
#        server_name  www.xxx.net;
#        root         /usr/share/nginx/html;

#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;

#        location / {
#	    proxy_pass http://127.0.0.1:port;
           # proxy_redirect   off;
#            proxy_set_header Host $http_host;
#            proxy_set_header X-Real-IP $remote_addr;
#            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#       }

#        error_page 404 /404.html;
#        location = /404.html {
#        }

#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }

# Settings for a TLS enabled server.

    server {
        listen       443 ssl http2;
        listen       [::]:443 ssl http2;
        # 域名
        server_name  www.xxxxxx.net;   
        root         /usr/share/nginx/html;
#        ssl on;
        ssl_certificate "/project/wordpress/ssl/5711339_www.toposphere.net.pem";
        ssl_certificate_key "/project/wordpress/ssl/5711339_www.toposphere.net.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
          # 配置路由代理
          proxy_pass http://127.0.0.1:port;
           # proxy_redirect   off;
          proxy_http_version    1.1;
          proxy_cache_bypass    $http_upgrade;

          proxy_set_header Upgrade              $http_upgrade;
          proxy_set_header Connection           "upgrade";
          proxy_set_header Host                 $host;
          proxy_set_header X-Real-IP            $remote_addr;
          proxy_set_header X-Forwarded-For      $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto    $scheme;
          proxy_set_header X-Forwarded-Host     $host;
          proxy_set_header X-Forwarded-Port     $server_port;
        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

}

  1. 配置http转https
    • vim /etc/nginx/conf.d/wordpress.conf
server {
        listen       80;
        listen       [::]:80;
        server_name  xxx.net  www.xxxx.net;
        return 301 https://www.xxxx.net$request_uri;

    }

nginx配置http转https

  1. 从起nginx
systemctl stop nginx
systemctl start nginx
# 或者
nginx -s reload

配置wordpress

  1. 在function.php中填写代码
# 开启ssl
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);
# 防止插件自动更新 与ssl无关
define( 'AUTOMATIC_UPDATER_DISABLED', true );
define('THEME_VERSION', wp_get_theme()->get('Version'));


# 将所有的请求修改为https
/* 替换图片链接为 https */
function my_content_manipulator($content){
    if( is_ssl() ){
        $content = str_replace('http://www.toposphere.net/wp-content/uploads', 'https://www.toposphere.net/wp-content/uploads', $content);
    }
    return $content;
}
add_filter('the_content', 'my_content_manipulator');
  1. 在后台中所有的带有http的访问域名修改为https的访问

目前添加的插件有

  1. 菜单插件:设置主题菜单
    • Max Mega Menu
  2. html5插件:设置html小工具
    • HTML5 Cumulus
  3. 文章目录插件:设置文章目录,目前未生效
    • Table of Contents Plus
  4. 头像插件:用于获取头像的插件
    • Simple Local Avatars
  5. 主题:Kratos

实用mysql操作语句

  1. 更新字段部分内容
update wp_posts set post_content = replace(post_content, 'https://example.com','https://www.demo.net');

遇到的坑

  1. nginx配置location,访问不了,跳转到了127.0.0.1,需要配置下面内容,具体原因没有查
proxy_set_header Host    $host;

小知识

  1. ps -ef与ps -aux的区别:推荐使用ps -ef

一直以为ps aux就可以列出所有的在运行进程,最近发现还是有些缺陷,用ps aux和-ef得到的结果居然不一样,以后尽量用-ef参数吧。
情况是这样的,我用/bmrt/blaph/blaph/bmgctl来启动进程,由于ps aux是用BSD格式来显示结果,所以可能只会显示到/bmrt/blaph/blap,后面的都被截掉了。
这样,如果用ps aux | grep bmgctl 来过滤该进程,可能就会误伤,获取不到bmgctl进程。
而ps -ef是用全格式的System V格式,显示出来就是带全路径的进程名,会显示出bmgctl,在ps -ef | grep bmgctl命令下就可以完整显示该进程了。

参考文章

  1. nginx配置:https://zhuanlan.zhihu.com/p/115731015
  2. 关于nginx中proxy_set_header配置:https://blog.csdn.net/felix_yujing/article/details/51682655
  3. nginx中proxy_set_header配置:https://blog.csdn.net/weixin_41590779/article/details/107593379
  4. 使用docker-compose搭建wordpress以及https教程:https://blog.csdn.net/shangyexin/article/details/106311376
  5. ps -aux与ps -ef的区别:https://www.2cto.com/os/201303/197697.html
  6. ps -aux与ps -ef的区别:https://blog.csdn.net/weixin_38756990/article/details/72638084
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值