Skip to main content

Wordpress 部署

相对于本地开发环境,项目部署就更加麻烦了。看到一些教程里,照着配置本地开发环境,再来一套 nginx、mysql、php,再重新安装一遍 Wordpress,真的是头大。还有什么 cPanel、万网虚拟主机、宝塔控制面板,还要学这么多东西。照这些教程配置的服务,因为没有经过专门的优化和调校,应用难免会有些慢。而且一些高级的配置,还需要自己手动去处理,比如顶级域名转向 www 二级域名,http 连接转向 https 连接等。

笔者提供的方案,虽然有一些局限性,但是真的是非常简单,并且帮你处理了上面讲的这些麻烦的问题,是个性价比非常高的方案。

先来说这个局限性。这个方案需要一台装有 ubuntu 系统的 vps,需要能用 ssh 秘钥连上服务器。这点排除了一些使用虚拟主机的同学。但是提供了更大的自由性。对于开发者来说,vps 也是更值得入手的方案。除了部署网站,可以加点其他有趣的小玩样。

下面开始介绍方案。

依赖

首先需要安装一下基础的依赖,包括 git、ansible。 mac 系统 直接 brew install git ansible 没有 brew 的话,先执行命令安装 brew: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" centos 系统 yum install git ansible ubuntu/debian 系统 apt-get install git ansible windows 系统 scoop install git ansible 或者 choco install git ansible 没有 scoop 或者 choco 的话先安装,因为笔者没有 windows 系统,大家自己摸索。 下载项目 到上一步我们创建的本地开发环境生成的项目下,执行命令。 git clone --depth=1 git@github.com:roots/trellis.git && rm -rf trellis/.git 配置参数 然后对代码进行一些设置,主要涉及三个配置文件: 网站参数 trellis/group_vars/production/wordpress_sites.yml 配置网站信息: wordpress_sites: example.com: site_hosts: - canonical: example.com redirects: - www.example.com local_path: ../site # path targeting local Bedrock site directory (relative to Ansible root) repo: git@github.com:example/example.com.git # replace with your Git repo URL repo_subtree_path: site # relative path to your Bedrock/WP directory in your repo branch: master multisite: enabled: false ssl: enabled: false provider: letsencrypt cache: enabled: false 密码参数 trellis/group_vars/production/vault.yml 配置密码相关参数:

Documentation: https://roots.io/trellis/docs/vault/

vault_mysql_root_password: productionpw

Documentation: https://roots.io/trellis/docs/security/

vault_users:

  • name: "{{ admin_user }}" password: example_password salt: "generateme"

Variables to accompany group_vars/production/wordpress_sites.yml

Note: the site name (example.com) must match up with the site name in the above file.

vault_wordpress_sites: example.com: env: db_password: example_dbpassword # Generate your keys here: https://roots.io/salts.html auth_key: "generateme" secure_auth_key: "generateme" logged_in_key: "generateme" nonce_key: "generateme" auth_salt: "generateme" secure_auth_salt: "generateme" logged_in_salt: "generateme" nonce_salt: "generateme" 服务器参数 trellis/hosts/production 配置服务器

Add each host to the [production] group and to a "type" group such as [web] or [db].

List each machine only once per [group], even if it will host multiple sites.

[production] your_server_hostname

[web] your_server_hostname 将 your_server_hostname 换成你的 vps ip 地址。 安装服务器 准备好前面的步骤之后,开始配置服务器,执行命令: cd ./trellis && ansible-playbook server.yml -e env=production 等待命令执行完毕,服务器就配置好了。 发布项目 因为 trellis 发布项目需要项目是 bedrock 项目,前面我们是用 local 创建的项目,所以还需要做一些调整,调整部分我们独立开一篇教程。先简单介绍调整好后如何发布项目。 发布项目执行命令: cd ./trellis && ./bin/deploy.sh production example.com 回滚项目 回滚项目执行命令: cd ./trellis && ansible-playbook rollback.yml -e "site=example.com env=production"