前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker runs WordPress through Nginx reverse proxy to open HTTPS binding domain name

Docker runs WordPress through Nginx reverse proxy to open HTTPS binding domain name

作者头像
码农笔录
发布2021-12-06 19:09:38
3350
发布2021-12-06 19:09:38
举报
文章被收录于专栏:码农笔录码农笔录
src=http___blog.orx.me_wpcontent_uploads_2013_12_wordpresslogo.jpgrefer=http___blog.orx.jpg
src=http___blog.orx.me_wpcontent_uploads_2013_12_wordpresslogo.jpgrefer=http___blog.orx.jpg

原文链接 https://www.aiprose.com/blog/148

This article uses docker-compose to run wordpress, uses the existing mysql database, and the external nginx opens https reverse proxy to wordpress. If your domain name has not been filed, please file it first.

startup files

Write wordpress.yml,Then execute docker-compose -f wordpress.yml up -d to start the container

172.18.0.1:3308 is my own mysql address

代码语言:javascript
复制
version: "3"
services:
  wordpress:
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     volumes:
      - /opt/wordpress:/var/www/html
     environment:
       - WORDPRESS_DB_HOST=172.18.0.1:3308
       - WORDPRESS_DB_USER=wordpress
       - WORDPRESS_DB_PASSWORD=wordpress

If you do not have a ready-made mysql database before, you can write the configuration file according to the tutorial on the official website

代码语言:javascript
复制
version: '3.1'
services:
  wordpress:
    image: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - /opt/wordpress:/var/www/html

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - /opt/mysql:/var/lib/mysql

Here you need to go to the wordpress backend to configure the address and modify it to our last address with the domain name.

Access ip:8000/wp-admin

image.png
image.png

Configure Nginx

Here, use nginx to open https and reverse proxy to worpress. If nginx is not installed, please install nginx first. HTTPS certificate can go to Alibaba Cloud to apply for a free SSL certificate. There are many pits here. Please pay attention to the configuration file.

Our case domain name is aispider.cc, we redirect all domain names to https://www.aispider.cc

vi /etc/nginx/nginx.conf

代码语言:javascript
复制
   server {
        listen  80;
        server_name     aispider.cc;
        location / {
           if ($host !~* ^www) {
                set $name_www www.$host;
                rewrite ^(.*) https://$name_www$1 permanent;
           }
           rewrite ^(.*) https://$host$1 permanent;
        }
    }
    server {
        listen  80;
        server_name  www.aispider.cc;
        rewrite ^(.*) https://$host$1 permanent;
    }
    server {
        listen       443 ssl http2 default_server;
        server_name  www.aispider.cc;
        ssl_certificate "/opt/cer/aispider.cc.pem";
        ssl_certificate_key "/opt/cer/aispider.cc.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers PROFILE=SYSTEM;
        ssl_prefer_server_ciphers on;

        location / {
           if ($host !~* ^www) {
              set $name_www www.$host;
              rewrite ^(.*) https://$name_www$1 permanent;
           }
           proxy_pass  http://localhost:8000;
           proxy_redirect     off;
           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-Host $server_name;
           proxy_set_header   X-Forwarded-Proto https;
           proxy_set_header   Upgrade $http_upgrade;
           proxy_set_header   Connection "upgrade";
           proxy_read_timeout 86400;
        }
    }

Modify wordpress configuration file

If the proxy passes directly, there will be many problems, such as circular redirection and forwarding, resources such as js, css, etc. go through http requests, and the address is redirected to localhost. I also stepped on a lot of pits to configure it.

Modify /opt/wordpress/wp-config.php, this is the volume mounted by the docker container, add the following content

vi /opt/wordpress/wp-config.php

代码语言:javascript
复制
define('FORCE_SSL_ADMIN', true);

if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false){
    $_SERVER['HTTPS'] = 'on';
    $_SERVER['SERVER_PORT'] = 443;
}
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
    $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}

define('WP_HOME','https://www.aispider.cc/');
define('WP_SITEURL','https://www.aispider.cc/');

It is best to restart the container after modification,docker-compose -f wordpress.yml restart

Finally visit the configured address https://www.aispider.cc/

The theme I use here is RK blogger. If you like, download and install it by Google. Remember to modify the display of the record information at the bottom of the theme configuration.

image.png
image.png
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/10/19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • startup files
  • Configure Nginx
  • Modify wordpress configuration file
相关产品与服务
网站建设
网站建设(Website Design Service,WDS),是帮助您快速搭建企业网站的服务。通过自助模板建站工具及专业设计服务,无需了解代码技术,即可自由拖拽模块,可视化完成网站管理。全功能管理后台操作方便,一次更新,数据多端同步,省时省心。使用网站建设服务,您无需维持技术和设计师团队,即可快速实现网站上线,达到企业数字化转型的目的。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档