如何使用 Docker Compose 安装 WordPress

Linux命令 Edge插件网 2年前 (2023-03-09) 576次浏览 已收录 0个评论

WordPress是一种流行的开源内容管理系统(CMS),用于创建和管理网站。安装WordPress的方法之一是使用Docker Compose,这是一个用于定义和运行多容器Docker应用程序的工具。如何使用 Docker Compose 安装 WordPress

使用 Docker Compose 安装 WordPress

第 1 步。首先,通过在终端中运行以下命令,确保所有系统软件包都是最新的。apt

<span class="pln">sudo apt update
sudo apt upgrade</span>

第 2 步。安装 Docker。

默认情况下,Docker 在 Ubuntu 22.04 基础存储库上不可用。现在运行以下命令将 Docker 存储库添加到系统中:

<span class="pln">echo </span><span class="str">"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"</span> <span class="pun">|</span><span class="pln"> sudo tee </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apt</span><span class="pun">/</span><span class="pln">sources</span><span class="pun">.</span><span class="pln">list</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">.</span><span class="pln">list</span>

接下来,将 GPG 密钥导入您的系统:

<span class="pln">curl </span><span class="pun">-</span><span class="pln">fsSL https</span><span class="pun">:</span><span class="com">//download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg</span>

启用存储库后,现在使用以下命令安装最新版本的 Docker 包:

<span class="pln">sudo apt update
sudo apt install docker</span><span class="pun">-</span><span class="pln">ce docker</span><span class="pun">-</span><span class="pln">ce</span><span class="pun">-</span><span class="pln">cli containerd</span><span class="pun">.</span><span class="pln">io docker</span><span class="pun">-</span><span class="pln">compose</span><span class="pun">-</span><span class="pln">plugin</span>

您可以验证 Docker 是否已安装以及当前版本:

<span class="pln">docker </span><span class="pun">-</span><span class="pln">v</span>

成功安装后,启用 Docker(在系统启动时自动启动)、启动并使用以下命令验证状态:

<span class="pln">sudo systemctl enable docker
sudo systemctl start docker
sudo systemctl status docker</span>

默认情况下,Docker 需要根权限。如果要避免在每次运行命令时使用,请将用户名添加到组中:sudodockerdocker

<span class="pln">sudo usermod </span><span class="pun">-</span><span class="pln">aG docker $</span><span class="pun">(</span><span class="pln">whoami</span><span class="pun">)</span><span class="pln">
su </span><span class="pun">-</span><span class="pln"> $</span><span class="pun">{</span><span class="pln">USER</span><span class="pun">}</span>

确认您的用户已添加到 Docker 组:

<span class="pln">groups</span>

有关安装和管理 Docker 的其他资源,请阅读下面的帖子:

  • 如何在 Ubuntu Linux √ 上安装 Docker。

第 3 步。为 WordPress 创建 Docker Compose 文件。

首先,为 WordPress 配置创建一个目录:

<span class="pln">mkdir wordpress
cd wordpress</span>

接下来,使用您喜欢的文本编辑器创建并打开 Docker 撰写文件:

<span class="pln">nano docker</span><span class="pun">-</span><span class="pln">compose</span><span class="pun">.</span><span class="pln">yml</span>

添加以下文件:

<span class="pln">version</span><span class="pun">:</span> <span class="str">'3.9'</span><span class="pln">

services</span><span class="pun">:</span><span class="pln">
  wp</span><span class="pun">:</span><span class="pln">
    image</span><span class="pun">:</span><span class="pln"> wordpress</span><span class="pun">:</span><span class="pln">latest
    container_name</span><span class="pun">:</span><span class="pln"> wordpress</span><span class="pun">-</span><span class="pln">app
    restart</span><span class="pun">:</span> <span class="kwd">unless</span><span class="pun">-</span><span class="pln">stopped
    expose</span><span class="pun">:</span>
      <span class="pun">-</span> <span class="lit">8080</span><span class="pln">
    volumes</span><span class="pun">:</span>
      <span class="pun">-</span> <span class="pun">./</span><span class="pln">config</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">conf</span><span class="pun">.</span><span class="pln">ini</span><span class="pun">:</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">php</span><span class="pun">/</span><span class="pln">conf</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">conf</span><span class="pun">.</span><span class="pln">ini
      </span><span class="pun">-</span> <span class="pun">./</span><span class="pln">wp</span><span class="pun">-</span><span class="pln">app</span><span class="pun">:</span><span class="str">/var/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html
      </span><span class="com">#- ./plugin-name/trunk/:/var/www/html/wp-content/plugins/plugin-name # Plugin development</span>
      <span class="com">#- ./theme-name/trunk/:/var/www/html/wp-content/themes/theme-name # Theme development</span><span class="pln">
    environment</span><span class="pun">:</span><span class="pln">
      WORDPRESS_DB_HOST</span><span class="pun">:</span><span class="pln"> db
      WORDPRESS_DB_NAME</span><span class="pun">:</span> <span class="str">"${DB_NAME}"</span><span class="pln">
      WORDPRESS_DB_USER</span><span class="pun">:</span> <span class="str">"${DB_USER_NAME}"</span><span class="pln">
      WORDPRESS_DB_PASSWORD</span><span class="pun">:</span> <span class="str">"${DB_USER_PASSWORD}"</span><span class="pln">
      VIRTUAL_HOST</span><span class="pun">:</span><span class="pln"> your</span><span class="pun">-</span><span class="pln">domain</span><span class="pun">.</span><span class="pln">com
      LETSENCRYPT_HOST</span><span class="pun">:</span><span class="pln"> your</span><span class="pun">-</span><span class="pln">domain</span><span class="pun">.</span><span class="pln">com
    depends_on</span><span class="pun">:</span>
      <span class="pun">-</span><span class="pln"> db
    links</span><span class="pun">:</span>
      <span class="pun">-</span><span class="pln"> db

  wpcli</span><span class="pun">:</span><span class="pln">
    image</span><span class="pun">:</span><span class="pln"> wordpress</span><span class="pun">:</span><span class="pln">cli
    container_name</span><span class="pun">:</span><span class="pln"> wpcli_app
    volumes</span><span class="pun">:</span>
      <span class="pun">-</span> <span class="pun">./</span><span class="pln">config</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">conf</span><span class="pun">.</span><span class="pln">ini</span><span class="pun">:</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">php</span><span class="pun">/</span><span class="pln">conf</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">conf</span><span class="pun">.</span><span class="pln">ini
      </span><span class="pun">-</span> <span class="pun">./</span><span class="pln">wp</span><span class="pun">-</span><span class="pln">app</span><span class="pun">:</span><span class="str">/var/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html
    environment</span><span class="pun">:</span><span class="pln">
      WORDPRESS_DB_HOST</span><span class="pun">:</span><span class="pln"> db
      WORDPRESS_DB_NAME</span><span class="pun">:</span> <span class="str">"${DB_NAME}"</span><span class="pln">
      WORDPRESS_DB_USER</span><span class="pun">:</span> <span class="str">"${DB_USER_NAME}"</span><span class="pln">
      WORDPRESS_DB_PASSWORD</span><span class="pun">:</span> <span class="str">"${DB_USER_PASSWORD}"</span><span class="pln">
    depends_on</span><span class="pun">:</span>
      <span class="pun">-</span><span class="pln"> db
      </span><span class="pun">-</span><span class="pln"> wp

  pma</span><span class="pun">:</span><span class="pln">
    image</span><span class="pun">:</span><span class="pln"> phpmyadmin</span><span class="pun">/</span><span class="pln">phpmyadmin
    container_name</span><span class="pun">:</span><span class="pln"> pma
    restart</span><span class="pun">:</span> <span class="kwd">unless</span><span class="pun">-</span><span class="pln">stopped
    environment</span><span class="pun">:</span>
      <span class="com"># https://docs.phpmyadmin.net/en/latest/setup.html#docker-environment-variables</span><span class="pln">
      PMA_HOST</span><span class="pun">:</span><span class="pln"> db
      PMA_PORT</span><span class="pun">:</span> <span class="lit">3306</span><span class="pln">
      MYSQL_ROOT_PASSWORD</span><span class="pun">:</span> <span class="str">"${DB_ROOT_PASSWORD}"</span><span class="pln">
      UPLOAD_LIMIT</span><span class="pun">:</span> <span class="lit">50M</span><span class="pln">
      VIRTUAL_HOST</span><span class="pun">:</span><span class="pln"> phpmyadmin</span><span class="pun">.</span><span class="pln">your</span><span class="pun">-</span><span class="pln">domain</span><span class="pun">.</span><span class="pln">com
      LETSENCRYPT_HOST</span><span class="pun">:</span><span class="pln"> phpmyadmin</span><span class="pun">.</span><span class="pln">your</span><span class="pun">-</span><span class="pln">domain</span><span class="pun">.</span><span class="pln">com
    expose</span><span class="pun">:</span>
      <span class="pun">-</span> <span class="lit">8081</span><span class="pln">
    links</span><span class="pun">:</span>
      <span class="pun">-</span><span class="pln"> db</span><span class="pun">:</span><span class="pln">db

  db</span><span class="pun">:</span><span class="pln">
    image</span><span class="pun">:</span><span class="pln"> mysql</span><span class="pun">:</span><span class="pln">latest
    container_name</span><span class="pun">:</span><span class="pln"> wordpressdb
    restart</span><span class="pun">:</span> <span class="kwd">unless</span><span class="pun">-</span><span class="pln">stopped
    command</span><span class="pun">:</span> <span class="pun">[</span>
        <span class="str">'--default_authentication_plugin=mysql_native_password'</span><span class="pun">,</span>
        <span class="str">'--character-set-server=utf8mb4'</span><span class="pun">,</span>
        <span class="str">'--collation-server=utf8mb4_unicode_ci'</span>
    <span class="pun">]</span><span class="pln">
    volumes</span><span class="pun">:</span>
      <span class="pun">-</span> <span class="pun">./</span><span class="pln">wp</span><span class="pun">-</span><span class="pln">data</span><span class="pun">:/</span><span class="pln">docker</span><span class="pun">-</span><span class="pln">entrypoint</span><span class="pun">-</span><span class="pln">initdb</span><span class="pun">.</span><span class="pln">d
      </span><span class="pun">-</span><span class="pln"> db_data</span><span class="pun">:</span><span class="str">/var/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">mysql
    environment</span><span class="pun">:</span><span class="pln">
      MYSQL_DATABASE</span><span class="pun">:</span> <span class="str">"${DB_NAME}"</span><span class="pln">
      MYSQL_ROOT_PASSWORD</span><span class="pun">:</span> <span class="str">"${DB_ROOT_PASSWORD}"</span><span class="pln">
      MYSQL_USER</span><span class="pun">:</span> <span class="str">"${DB_USER_NAME}"</span><span class="pln">
      MYSQL_PASSWORD</span><span class="pun">:</span> <span class="str">"${DB_USER_PASSWORD}"</span><span class="pln">

volumes</span><span class="pun">:</span><span class="pln">
  db_data</span><span class="pun">:</span>

保存并关闭文件,然后为撰写文件中使用的变量创建环境文件:

<span class="pln">sudo nano </span><span class="pun">.</span><span class="pln">env</span>

添加以下文件:

<span class="pln">DB_NAME</span><span class="pun">=</span><span class="pln">wordpress
DB_USER_NAME</span><span class="pun">=</span><span class="pln">username
DB_USER_PASSWORD</span><span class="pun">=</span><span class="pln">userpassword
DB_ROOT_PASSWORD</span><span class="pun">=</span><span class="pln">your</span><span class="pun">-</span><span class="pln">strong</span><span class="pun">-</span><span class="pln">password</span>

保存并关闭文件,然后为 PHP 配置创建一个文件夹:

<span class="pln">mkdir config</span>

现在我们创建并打开文件进行编辑:php.conf.ini

<span class="pln">nano config</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">conf</span><span class="pun">.</span><span class="pln">ini</span>

添加以下文件:

<span class="pln">file_uploads </span><span class="pun">=</span> <span class="typ">On</span><span class="pln">
memory_limit </span><span class="pun">=</span> <span class="lit">512M</span><span class="pln">
upload_max_filesize </span><span class="pun">=</span> <span class="lit">30M</span><span class="pln">
post_max_size </span><span class="pun">=</span> <span class="lit">30M</span><span class="pln">
max_execution_time </span><span class="pun">=</span> <span class="lit">400</span>

第 4 步。为 nginx 配置 Docker Compose

首先,我们为 Nginx 配置创建目录:

<span class="pln">mkdir nginx</span>

接下来,在该目录中为虚拟主机创建一个目录:

<span class="pln">mkdir nginx</span><span class="pun">/</span><span class="pln">vhost</span>

之后,创建并打开它们进行编辑。nginx/vhost/wordpress.your-domain.com

<span class="pln">nano nginx</span><span class="pun">/</span><span class="pln">vhost</span><span class="pun">/</span><span class="pln">wordpress</span><span class="pun">.</span><span class="pln">your</span><span class="pun">-</span><span class="pln">domain</span><span class="pun">.</span><span class="pln">com</span>

粘贴以下文件:

<span class="pln">server_tokens off</span><span class="pun">;</span><span class="pln">
client_max_body_size </span><span class="lit">30m</span><span class="pun">;</span>

保存并关闭文件。

对文件执行相同的操作。nginx/vhost/phpmyadmin.example.com

<span class="pln">nano nginx</span><span class="pun">/</span><span class="pln">vhost</span><span class="pun">/</span><span class="pln">phpmyadmin</span><span class="pun">.</span><span class="pln">your</span><span class="pun">-</span><span class="pln">domain</span><span class="pun">.</span><span class="pln">com</span>

添加以下代码:

<span class="pln">server_tokens off</span><span class="pun">;</span><span class="pln">
client_max_body_size </span><span class="lit">50m</span><span class="pun">;</span>

保存并关闭文件,然后再次打开 docker 撰写文件:

<span class="pln">nano docker</span><span class="pun">-</span><span class="pln">compose</span><span class="pun">.</span><span class="pln">yml</span>

将以下代码粘贴到该行之前。volumes: db_data:

<span class="pln">nginx</span><span class="pun">:</span><span class="pln">
    container_name</span><span class="pun">:</span><span class="pln"> nginx
    image</span><span class="pun">:</span><span class="pln"> nginxproxy</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">-</span><span class="pln">proxy
    restart</span><span class="pun">:</span> <span class="kwd">unless</span><span class="pun">-</span><span class="pln">stopped
    ports</span><span class="pun">:</span>
        <span class="pun">-</span> <span class="lit">80</span><span class="pun">:</span><span class="lit">80</span>
        <span class="pun">-</span> <span class="lit">443</span><span class="pun">:</span><span class="lit">443</span><span class="pln">
    volumes</span><span class="pun">:</span>
        <span class="pun">-</span> <span class="str">/var/</span><span class="pln">run</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">.</span><span class="pln">sock</span><span class="pun">:</span><span class="str">/tmp/</span><span class="pln">docker</span><span class="pun">.</span><span class="pln">sock</span><span class="pun">:</span><span class="pln">ro
        </span><span class="pun">-</span> <span class="pun">./</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">html</span><span class="pun">:</span><span class="str">/usr/</span><span class="pln">share</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">html
        </span><span class="pun">-</span> <span class="pun">./</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">certs</span><span class="pun">:</span><span class="str">/etc/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">certs
        </span><span class="pun">-</span> <span class="pun">./</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">vhost</span><span class="pun">:</span><span class="str">/etc/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">vhost</span><span class="pun">.</span><span class="pln">d
    logging</span><span class="pun">:</span><span class="pln">
        options</span><span class="pun">:</span><span class="pln">
            max</span><span class="pun">-</span><span class="pln">size</span><span class="pun">:</span> <span class="str">"10m"</span><span class="pln">
            max</span><span class="pun">-</span><span class="pln">file</span><span class="pun">:</span> <span class="str">"3"</span>

第5步。为 SSL 配置 Docker Compose

现在我们使用以下命令打开一个 docker-compose 文件:

<span class="pln">nano docker</span><span class="pun">-</span><span class="pln">compose</span><span class="pun">.</span><span class="pln">yml</span>

在该行之前添加以下代码:volumes: db_data:

<span class="pln">acme</span><span class="pun">-</span><span class="pln">companion</span><span class="pun">:</span><span class="pln">
    container_name</span><span class="pun">:</span><span class="pln"> acme</span><span class="pun">-</span><span class="pln">companion
    image</span><span class="pun">:</span><span class="pln"> nginxproxy</span><span class="pun">/</span><span class="pln">acme</span><span class="pun">-</span><span class="pln">companion
    restart</span><span class="pun">:</span> <span class="kwd">unless</span><span class="pun">-</span><span class="pln">stopped
    volumes_from</span><span class="pun">:</span>
        <span class="pun">-</span><span class="pln"> nginx
    volumes</span><span class="pun">:</span>
        <span class="pun">-</span> <span class="str">/var/</span><span class="pln">run</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">.</span><span class="pln">sock</span><span class="pun">:</span><span class="str">/var/</span><span class="pln">run</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">.</span><span class="pln">sock
        </span><span class="pun">-</span> <span class="pun">./</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">acme</span><span class="pun">:</span><span class="str">/etc/</span><span class="pln">acme</span><span class="pun">.</span><span class="pln">sh
    environment</span><span class="pun">:</span><span class="pln">
        DEFAULT_EMAIL</span><span class="pun">:</span><span class="pln"> admin@your</span><span class="pun">-</span><span class="pln">domain</span><span class="pun">.</span><span class="pln">com</span>

步骤 7.配置防火墙。

现在我们使用WordPress设置了一个简单防火墙(UFW),以允许公共访问默认Web端口80:

<span class="pln">sudo ufw allow </span><span class="typ">OpenSSH</span><span class="pln">
sudo ufw allow </span><span class="str">'Nginx Full'</span><span class="pln">
sudo ufw enable</span>

第8步。安装WordPress。

完成所有配置文件后,就可以启动和启动容器了:

<span class="pln">docker compose up </span><span class="pun">-</span><span class="pln">d</span>

输出:

<span class="pln">$ docker ps
CONTAINER ID   IMAGE                       COMMAND                  CREATED          STATUS          PORTS                                                                      NAMES
r1e8agdt169d   nginxproxy</span><span class="pun">/</span><span class="pln">acme</span><span class="pun">-</span><span class="pln">companion   </span><span class="str">"/bin/bash /app/entr…"</span>   <span class="lit">12</span><span class="pln"> seconds ago   </span><span class="typ">Up</span> <span class="lit">8</span><span class="pln"> seconds                                                                               acme</span><span class="pun">-</span><span class="pln">companion
</span><span class="lit">9m3ilk8ff790</span><span class="pln">   wordpress</span><span class="pun">:</span><span class="pln">latest            </span><span class="str">"docker-entrypoint.s…"</span>   <span class="lit">12</span><span class="pln"> seconds ago   </span><span class="typ">Up</span> <span class="lit">8</span><span class="pln"> seconds    </span><span class="lit">80</span><span class="pun">/</span><span class="pln">tcp</span><span class="pun">,</span> <span class="lit">8080</span><span class="pun">/</span><span class="pln">tcp                                                           wordpress</span><span class="pun">-</span><span class="pln">app
</span><span class="lit">1f9c777g9dta</span><span class="pln">   phpmyadmin</span><span class="pun">/</span><span class="pln">phpmyadmin       </span><span class="str">"/docker-entrypoint.…"</span>   <span class="lit">12</span><span class="pln"> seconds ago   </span><span class="typ">Up</span> <span class="lit">8</span><span class="pln"> seconds    </span><span class="lit">80</span><span class="pun">/</span><span class="pln">tcp</span><span class="pun">,</span> <span class="lit">8081</span><span class="pun">/</span><span class="pln">tcp                                                           pma
</span><span class="lit">1bmwil07an42</span><span class="pln">   nginxproxy</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">-</span><span class="pln">proxy      </span><span class="str">"/app/docker-entrypo…"</span>   <span class="lit">12</span><span class="pln"> seconds ago   </span><span class="typ">Up</span> <span class="lit">10</span><span class="pln"> seconds   </span><span class="lit">0.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">:</span><span class="lit">80</span><span class="pun">-></span><span class="lit">80</span><span class="pun">/</span><span class="pln">tcp</span><span class="pun">,</span> <span class="pun">:::</span><span class="lit">80</span><span class="pun">-></span><span class="lit">80</span><span class="pun">/</span><span class="pln">tcp</span><span class="pun">,</span> <span class="lit">0.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">:</span><span class="lit">443</span><span class="pun">-></span><span class="lit">443</span><span class="pun">/</span><span class="pln">tcp</span><span class="pun">,</span> <span class="pun">:::</span><span class="lit">443</span><span class="pun">-></span><span class="lit">443</span><span class="pun">/</span><span class="pln">tcp   nginx
t0dh64a2f4a3   mysql</span><span class="pun">:</span><span class="pln">latest                </span><span class="str">"docker-entrypoint.s…"</span>   <span class="lit">12</span><span class="pln"> seconds ago   </span><span class="typ">Up</span> <span class="lit">10</span><span class="pln"> seconds   </span><span class="lit">3306</span><span class="pun">/</span><span class="pln">tcp</span><span class="pun">,</span> <span class="lit">33060</span><span class="pun">/</span><span class="pln">tcp                                                        wordpressdb</span>

现在打开您的网络浏览器并使用 URL 访问 WordPress Web UI。您将被重定向到以下页面:https://your-domain.com

如何使用 Docker Compose 安装 WordPress

此外,您可以通过 URL 访问 phpMyAdmin 。https://phpmyadmin.your-domain.com

如何使用 Docker Compose 安装 WordPress
phpMyAdmin login page

感谢您使用本教程在 Ubuntu 系统上安装 WordPress with Docker Compose。有关其他帮助或有用信息,我们建议您查看 Docker 官方网站


Edge插件网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何使用 Docker Compose 安装 WordPress
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址