关注【索引目录】服务号,更多精彩内容等你来探索!
手动启动 Ghost 博客很酷,直到你必须连续执行三次。
无论您是要扩展、重建,还是只是喜欢基础设施的可重复性,Ansible + Ghost都是自动化天堂的完美搭配。
在这篇文章中,我将引导您使用 Ansible 在 Ubuntu 22.04 上设置 Ghost CMS 5.x:
-
MariaDB 10.6 作为数据库 -
完全基于系统的流程设置 -
Ghost CLI 安装 -
电子邮件 SMTP 配置 -
合理的默认用户权限 -
是的——从备份恢复 Ghost
项目布局
我们使用 Galaxy 搭建 Ansible 角色:
ansible-galaxy init roles/ghost --offline
最终结构如下:
hex-ansible
├─ ghost.yml
├─ hosts.ini
├─ roles
│ └─ ghost/
│ ├─ tasks/
│ ├─ templates/
│ ├─ vars/
│ ├─ files/
│ └─ ...
变量:vars/main.yml
所有可配置值都集中在一个地方:
ghost_user: ghost
node_major: 18
ghost_version: "5.89.3"
cli_version: 1.27.0
ghost_dir: /var/www/ghost
ghost_url: https://journal.hexmos.com
db_user: ghostuser
db_pass: ghostpass123
db_name: ghostdb
db_host: 127.0.0.1
restore_url: https://backup-bucket.example.com/ghost.zip
mail_transport: "SMTP"
mail_from: "'Ghost Journal' ghost@example.com"
mail_host: "smtp.example.com"
mail_port: 2465
mail_secure: true
mail_user: "SMTP_USER"
mail_pass: "SMTP_PASS"
任务分解
1.安装 MariaDB(install_mariadb.yml)
这处理:
-
仓库 + GPG 密钥 -
安装 MariaDB 10.6 -
创建数据库和用户
- name: Install MariaDB 10.6
apt:
name: mariadb-server
state: present
- name: Create DB and user
shell: |
mysql -u root <<EOF
CREATE DATABASE IF NOT EXISTS {{ db_name }};
CREATE USER IF NOT EXISTS '{{ db_user }}'@'{{ db_host }}' IDENTIFIED BY '{{ db_pass }}';
GRANT ALL PRIVILEGES ON {{ db_name }}.* TO '{{ db_user }}'@'{{ db_host }}';
FLUSH PRIVILEGES;
EOF
2. 安装 Ghost 和 Node.js(install_ghost.yml)
关键步骤:
-
添加 NodeSource 仓库 -
全局安装 Ghost CLI -
允许 systemd 操作员使用无密码 sudo -
使用 CLI 以 ghost用户 身份安装 Ghost
- name: Install Ghost as ghost user
become_user: "{{ ghost_user }}"
shell: |
cd {{ ghost_dir }} && \
ghost install {{ ghost_version }} \
--url {{ ghost_url }} \
--db mysql \
--dbhost {{ db_host }} \
--dbuser {{ db_user }} \
--dbpass {{ db_pass }} \
--dbname {{ db_name }} \
--no-setup-nginx \
--no-setup-ssl \
--process systemd \
--start \
--no-prompt
args:
chdir: "{{ ghost_dir }}"
creates: "{{ ghost_dir }}/current"
另外,为了避免sudo提示ghost:
- name: Allow ghost user passwordless sudo
copy:
dest: /etc/sudoers.d/ghost
content: |
ghost ALL=(ALL) NOPASSWD: /bin/systemctl
ghost ALL=(ALL) NOPASSWD: /bin/systemctl, /bin/mv, /usr/bin/mkdir, /usr/bin/chown
mode: '0440'
validate: 'visudo -cf %s'
3. 恢复 Ghost 备份(restore_ghost.yml)
下载+提取您的备份内容:
- name: Download and unarchive ghost backup
unarchive:
src: "{{ restore_url }}"
dest: "{{ ghost_dir }}/content"
remote_src: true
确保权限设置正确:
- name: Fix permissions
file:
path: "{{ ghost_dir }}/content"
owner: "{{ ghost_user }}"
group: "{{ ghost_user }}"
recurse: yes
然后放入配置:
- name: Template config.production.json
template:
src: config.production.json.j2
dest: "{{ ghost_dir }}/config.production.json"
owner: "{{ ghost_user }}"
group: "{{ ghost_user }}"
mode: "0640"
notify: restart ghost
幽灵配置模板 ( config.production.json.j2)
{
"url": "{{ ghost_url }}",
"database": {
"client": "mysql",
"connection": {
"host": "{{ db_host }}",
"user": "{{ db_user }}",
"password": "{{ db_pass }}",
"database": "{{ db_name }}"
}
},
"mail": {
"transport": "{{ mail_transport }}",
"options": {
"from": {{ mail_from | to_json }},
"host": "{{ mail_host }}",
"port": {{ mail_port }},
"secure": {{ mail_secure | to_json }},
"auth": {
"user": "{{ mail_user }}",
"pass": "{{ mail_pass }}"
}
}
},
"process": "systemd",
"paths": {
"contentPath": "{{ ghost_dir }}/content"
}
}
处理程序 ( handlers/main.yml)
- name: restart ghost
command: ghost restart
args:
chdir: "{{ ghost_dir }}"
become: true
become_user: "{{ ghost_user }}"
运行剧本
创建一个简单的hosts.ini:
[ghost]
your.server.ip ansible_user=ubuntu
然后触发它:
ansible-playbook -i hosts.ini ghost.yml
概括
此设置可为您提供:
-
任何 Ubuntu 系统上干净的 MariaDB + Ghost CMS 堆栈 -
使用 CLI 和 systemd 自动安装 Ghost -
SMTP 就绪配置 -
备份恢复支持 -
systemctl 不再提示 sudo
想要使用 nginx、SSL(Certbot)或 Cloudflare 扩展此功能?请将它们作为单独的角色插入。
专业提示
-
使用Ansible Vault保存 SMTP 凭证等机密信息。 ghost backup通过 cron + Ansible 管理脚本进行安排。 -
幽灵升级?只需 ghost update执行任务并重启服务即可。
关注【索引目录】服务号,更多精彩内容等你来探索!

