docker搭建wordpress

发布于 2021-08-11  2508 次阅读


本教程使用了docker+nginx的方式来搭建,系统为Ubuntu16.04

docker安装

方法一:

(1)先删除已有docker 存在版本

sudo apt-get remove docker docker-engine docker.io

(2)安装linux-image-extra-*包,以便docker 使用aufs 存储驱动

sudo apt-get update
sudo apt-get install \
linux-image-extra-$(uname -r) \
linux-image-extra-virtual

(3) 更新apt 包索引

sudo apt-get update

(4) 安装允许apt 通过HTTPS 来使用repository 的包

sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common

(5) 添加Docker 的官方GPG key,并验证该key 是否安装成功

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo apt-get update
sudo apt-get install docker-ce

(6) amd64 位机器上使用如下命令来建立稳定的库

sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

(7)安装Docker CE

sudo apt-get update
sudo apt-get install docker-ce

(8)设置开机自启动docker

sudo systemctl enable docker
sudo systemctl start docker

方法二:

直接使用脚本安装

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

验证docker是否安装好

docker --version

设置开机自启动docker

sudo systemctl enable docker
sudo systemctl start docker

docker-compose安装

方法一:使用curl从github中安装

1. 进入https://github.com/docker/compose/releases 查看最新版本,当前版本为1.29.2
sudo curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

2. 设置权限
sudo chmod +x /usr/local/bin/docker-compose

3. 查看是否安装成功
docker-compose --version
出现docker-compose version 1.29.2即安装成功

这种方法的速度非常非常慢,根本忍受不了,或者直接就连不上去,所以不是很推荐。

方法二:pip安装

sudo pip install docker-compose

验证是否安装好

docker-compose --version

不知道为什么我的机子用pip装不了,这时候就要采取第三种方法了。

方法三:离线安装

自己从github上把docker-compose下载下来然后上传到服务器上。

下载地址:

https://github.com/docker/compose/releases/download/1.29.2/docker-compose-Linux-x86_64

如果下载速度还是慢的话可以去下我下载好的,下载链接在文末会给出。

下载好了之后把文件放到/usr/local/bin/这个目录下,接着设置权限

sudo chmod +x /usr/local/bin/docker-compose

查看是否安装成功

docker-compose --version

出现docker-compose version 1.29.2即安装成功。

nginx安装

nginx安装

sudo apt -y install nginx

启动nginx

sudo systemctl start nginx

设置nginx开机自启动

systemctl enable nginx.service

设置防火墙

  • 允许http通信
sudo firewall-cmd --permanent --zone=public --add-service=http
  • 允许https通信
sudo firewall-cmd --permanent --zone=public --add-service=https
  • 重启防火墙
sudo firewall-cmd --reload

wordpress安装

前置工作准备好了

接下来找一个地方新建一个docker-compose.yml的文件

version: '3.1'

services:

  wordpress:
    image: wordpress
    container_name: wordpress
    restart: always
    ports:
      - 17080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpresswing
      WORDPRESS_DB_PASSWORD: KDM2RwB5fcQ2X2
      WORDPRESS_DB_NAME: wordpresswing
    volumes:
      - ./wordpress:/var/www/html

  db:
    image: mysql:5.7
    container_name: wordpress_db
    restart: always
    environment:
      MYSQL_DATABASE: wordpresswing
      MYSQL_USER: wordpresswing
      MYSQL_PASSWORD: KDM2RwB5fcQ2X2
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - ./db:/var/lib/mysql

17080是访问的端口号,这个可以自己改,后面利用nginx反向代理的时候需要用到这个端口号

接着在这个文件的路径下

docker-compose up -d

然后ip:端口号就可以访问的了

不过我还想要不用加端口号直接用域名就访问到,这就需要nginx反向代理了

nginx配置

/etc/nginx/conf.d里创建wordpress.conf

server {
    listen 80;
    listen [::]:80;

    server_name 域名.com www.域名.com;

    # redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
    return 301 https://www.域名.com$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name 域名.com;

    location / {
          proxy_pass http://127.0.0.1:17080;//这里注意一下

          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;
    }

    # certs sent to the client in SERVER HELLO are concatenated in ssl_certificate
    ssl_certificate xxxxx这里填ssl证书里的pem文件路径.pem;
    ssl_certificate_key xxxxxx这里填ssl证书里的key文件路径.key;
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;  # about 40000 sessions
    ssl_session_tickets off;

    # intermediate configuration
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;

    # HSTS (ngx_http_headers_module is required) (63072000 seconds)
    add_header Strict-Transport-Security "max-age=63072000" always;

    # OCSP stapling
    ssl_stapling on;
    ssl_stapling_verify on;
}

然后重启nginx

systemctl restart nginx

在这里设置完之后不要忘记在wordpress的后台里面的地址也改成你的域名

 


一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。