本教程适用于以下操作系统版本

  • Ubuntu 20.04 (Focal Fossa)
  • Ubuntu 18.04 (Bionic Beaver)

本页目录

  • 先决条件
  • 入门
  • 安装 Nginx 和 MariaDB
  • 安装 Ghost Cli
  • 安装 Ghost
  • 使用 Let's Encrypt SSL 保护 Ghost
  • 访问 Ghost 管理界面
  • 结论

Ghost 是一个基于 Node.js 构建的免费、开源且轻量级的博客平台。它简单、可定制,并允许您在互联网上创建和发布您的内容。它专为博客而设计,因此比其他博客平台快得多。它提供丰富的功能集,其中一些列在下面:

  • Markdown 编辑器
  • 定时发布
  • 提供适用于 Linux、Mac 和 Windows 的应用程序
  • 提供数百个免费和高级主题
  • 内置分析
  • 提供迁移插件以从 WordPress 迁移到 Ghost

本教程将解释如何在 Ubuntu 20.04 上使用 Nginx 和 Let's Encrypt SSL 安装 Ghost。

先决条件

  • 一台运行 Ubuntu 20.04 的服务器。
  • 一个指向您服务器 IP 的有效域名。
  • 您的服务器上配置了 root 密码。

入门

首先,建议将 APT 软件包缓存更新到最新版本。您可以使用以下命令更新它:

apt-get update -y

APT 缓存更新后,使用以下命令安装其他所需的依赖项:

apt-get install gnupg2 curl unzip git acl -y

接下来,您还需要在系统上安装 Node.js。

首先,使用以下命令添加 Node.js 存储库:

curl -sL https://deb.nodesource.com/setup_12.x | bash -

存储库安装后,运行以下命令将 Node.js 安装到您的系统。

apt-get install nodejs -y

安装完成后,使用以下命令验证 Node.js 版本:

node -v

您应该得到以下输出:

v12.22.1

接下来,使用以下命令验证 NPM 版本:

npm -v

您应该得到以下输出:

6.14.12

安装 Nginx 和 MariaDB

接下来,您需要在系统上安装 Nginx web 服务器和 MariaDB 数据库服务器。您可以使用以下命令安装这两个软件包:

apt-get install mariadb-server nginx -y

安装完成后,使用以下命令保护 MariaDB 安装并设置 MariaDB root 密码:

mysql_secure_installation

回答所有问题以保护 MariaDB 安装:

Enter current password for root (enter for none): 
Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

接下来,使用以下命令登录 MariaDB:

mysql -u root -p

登录后,使用以下命令为 Ghost 创建数据库和用户:

MariaDB [(none)]> CREATE DATABASE ghostdb;
MariaDB [(none)]> CREATE USER 'ghostuser'@'localhost' IDENTIFIED BY 'password';

接下来,使用以下命令授予 Ghost 数据库所有权限:

MariaDB [(none)]> GRANT ALL ON ghostdb.* TO 'ghostuser'@'localhost';

接下来,使用以下命令刷新权限并退出 MariaDB:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

完成后,您可以继续下一步。

安装 Ghost Cli

接下来,您需要安装 Ghost Cli 来安装和管理 Ghost。您可以使用 NPM 安装它,如下所示:

npm install -g ghost-cli@latest

安装后,使用以下命令验证 Ghost Cli 的安装版本:

ghost version

您应该看到以下输出:

Ghost-CLI version: 1.16.3

安装 Ghost

首先,使用以下命令为 Ghost 创建一个单独的用户:

adduser user1

接下来,使用以下命令将 user1 添加到 sudo 和 www-data 组:

adduser user1 sudo
adduser user1 www-data

接下来,使用 user1 登录并使用以下命令为 Ghost 创建目录:

su - user1
sudo mkdir /var/www/ghost

接下来,使用以下命令设置适当的权限和所有权:

sudo chown -R www-data:www-data /var/www/ghost
sudo chown -R www-data:www-data /var/www/
sudo chmod 775 /var/www/ghost
sudo setfacl -R -m u:user1:rwx /var/www/ghost/

接下来,将目录更改为 ghost 并使用以下命令安装 Ghost 博客平台:

cd /var/www/ghost
ghost install

系统将要求您提供博客 URL、数据库名称、用户名、密码和其他信息,如下所示:

? Enter your blog URL: https://ghost.linuxbuz.com
? Enter your MySQL hostname: localhost
? Enter your MySQL username: ghostuser
? Enter your MySQL password: [hidden]
? Enter your Ghost database name: ghostdb
? Configuring Ghost
? Setting up instance
+ sudo useradd --system --user-group ghost
+ sudo chown -R ghost:ghost /var/www/ghost/content
? Setting up "ghost" system user
? Setting up "ghost" mysql user [skipped]
? Do you wish to set up Nginx? Yes
+ sudo mv /tmp/ghost-linuxbuz-com/ghost.linuxbuz.com.conf /etc/nginx/sites-available/ghost.linuxbuz.com.conf
+ sudo ln -sf /etc/nginx/sites-available/ghost.linuxbuz.com.conf /etc/nginx/sites-enabled/ghost.linuxbuz.com.conf
+ sudo nginx -s reload
? Setting up SSL [skipped]
? Do you wish to set up Systemd? Yes
+ sudo mv /tmp/ghost-linuxbuz-com/ghost_ghost-linuxbuz-com.service /lib/systemd/system/ghost_ghost-linuxbuz-com.service
+ sudo systemctl daemon-reload
? Setting up Systemd
+ sudo systemctl is-active ghost_ghost-linuxbuz-com
? Do you want to start Ghost? (Y/n) 

此时,Ghost 已安装并启动。

使用 Let's Encrypt SSL 保护 Ghost

接下来,您需要使用 Let's Encrypt SSL 保护 Ghost。

首先,使用以下命令安装 Certbot 客户端:

sudo apt-get install certbot python3-certbot-nginx -y

安装后,运行以下命令下载 Let's Encrypt SSL 并配置 Nginx 使用这些证书:

sudo certbot --nginx -d ghost.linuxbuz.com

系统将要求您提供电子邮件地址并接受服务条款,如下所示:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 
[email protected]


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for ghost.linuxbuz.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/ghost.linuxbuz.com.conf

接下来,选择是否将 HTTP 流量重定向到 HTTPS:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

输入 2 并按 Enter 完成安装。

Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/ghost.linuxbuz.com.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://ghost.linuxbuz.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=ghost.linuxbuz.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/ghost.linuxbuz.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/ghost.linuxbuz.com/privkey.pem
   Your cert will expire on 2021-07-13. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

现在,您可以使用以下命令验证 Ghost 网站;

ghost ls

您应该看到以下输出:

+ sudo systemctl is-active ghost_ghost-linuxbuz-com
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? Name               ? Location       ? Version ? Status               ? URL                        ? Port ? Process Manager ?
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? ghost-linuxbuz-com ? /var/www/ghost ? 4.2.1   ? running (production) ? https://ghost.linuxbuz.com ? 2368 ? systemd         ?
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

您还可以使用以下命令验证 Ghost 服务:

sudo systemctl status ghost_ghost-linuxbuz-com

您应该看到以下输出:

? ghost_ghost-linuxbuz-com.service - Ghost systemd service for blog: ghost-linuxbuz-com
     Loaded: loaded (/lib/systemd/system/ghost_ghost-linuxbuz-com.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-04-14 07:05:33 UTC; 9min ago
       Docs: https://ghost.org/docs/
   Main PID: 7546 (node)
      Tasks: 22 (limit: 2353)
     Memory: 134.3M
     CGroup: /system.slice/ghost_ghost-linuxbuz-com.service
             ??7546 ghost run
             ??7590 /usr/bin/node current/index.js

Apr 14 07:13:44 ubuntu node[7590]: [2021-04-14 07:13:44] INFO "GET /ghost/assets/img/dashboard/start-membership-baafa2c6558d118ae59cb1fddab045>
Apr 14 07:13:44 ubuntu node[7590]: [2021-04-14 07:13:44] INFO "GET /ghost/assets/img/dashboard/the-browser-f5e7e199fe82d8435913e84d022fa7e1.pn>
Apr 14 07:13:44 ubuntu node[7590]: [2021-04-14 07:13:44] INFO "GET /ghost/assets/img/dashboard/join-community-bb5478f7dca6c04a2d0baf2b02f880fb>
Apr 14 07:13:44 ubuntu node[7590]: [2021-04-14 07:13:44] INFO "GET /ghost/api/canary/admin/members/?filter=status%3Apaid&order=created_at%20as>
Apr 14 07:14:11 ubuntu node[7590]: [2021-04-14 07:14:11] INFO "GET /" 200 475ms
Apr 14 07:14:11 ubuntu node[7590]: [2021-04-14 07:14:11] INFO "GET /" 200 480ms
Apr 14 07:14:15 ubuntu node[7590]: [2021-04-14 07:14:15] INFO "GET /members/api/site/" 200 12ms
Apr 14 07:14:18 ubuntu node[7590]: [2021-04-14 07:14:18] INFO "GET /" 304 142ms
Apr 14 07:14:25 ubuntu node[7590]: [2021-04-14 07:14:25] INFO "GET /members/api/site/" 304 4ms
Apr 14 07:14:26 ubuntu node[7590]: [2021-04-14 07:14:26] INFO "GET /members/api/member/" 401 4ms

访问 Ghost 管理界面

现在,打开您的 web 浏览器并使用 URL https://ghost.linuxbuz.com/ghost/ 访问 Ghost 管理界面。您应该看到以下页面:

如何在 Ubuntu 上用 Nginx 部署 Ghost 博客的完整教程

单击 Create your account 按钮以创建您的第一个账户:

如何在 Ubuntu 上用 Nginx 部署 Ghost 博客的完整教程

提供您的账户详细信息,然后单击屏幕底部的 I’ll do this later。您应该看到以下页面中的 Ghost 管理界面:

如何在 Ubuntu 上用 Nginx 部署 Ghost 博客的完整教程

您还可以使用 URL https://ghost.linuxbuz.com/ 访问您的 Ghost 网站。您应该看到以下页面:

如何在 Ubuntu 上用 Nginx 部署 Ghost 博客的完整教程

结论

恭喜!您已在 Ubuntu 20.04 上成功安装 Ghost 博客平台。现在您可以从 Ghost 界面创建和发布您自己的内容。