如何在Ubuntu 18.04上安装Ghost

Ghost是建立在Node.js平台之上的现代源代码发布平台。它是完全可定制的并且易于使用,使您可以以几乎零的学习曲线发布内容。 在本教程中,我们将向您展示如何使用Nginx作为代理在Ubuntu 18.04服务器上部署安全的Ghost博客,免费的Let's Encrypt SSL证书,Node.js的最新LTS版本以及MySQL / MariaDB作为数据库后端。

8 min read
By myfreax
如何在Ubuntu 18.04上安装Ghost

Ghost是建立在Node.js平台之上的现代源代码发布平台。它是完全可定制的并且易于使用,使您可以以几乎零的学习曲线发布内容。

在本教程中,我们将向您展示如何使用Nginx作为代理在Ubuntu 18.04服务器上部署安全的Ghost博客,免费的Let's Encrypt SSL证书,Node.js的最新LTS版本以及MySQL / MariaDB作为数据库后端。

先决条件

作为遵循本教程的先决条件,您需要:

在继续学习本教程之前,请确保您以具有[sudo]权限的用户身份登录

安装Node.js和Yarn

在撰写本文时,建议的Ghost Node.js版本是v8 carbon LTS。我们将从NodeSource存储库中安装Node.js。

使用以下 curl命令启用Node.js v8的NodeSource存储库:

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

通过键入以下命令安装Node.js:

sudo apt install nodejs

有关其他安装方法,请查看我们的如何在Ubuntu 18.04上安装Node.js 指南。

安装yarn ,请先使用以下命令在系统上启用Yarn存储库:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

启用存储库后,请通过以下方式安装yarn:

sudo apt update
sudo apt-get -o Dpkg::Options::="--force-overwrite" install yarn

安装MySQL

Ghost支持 MySQL MariaDB 和SQLite数据库。在本教程中,我们将使用MySQL,这是在生产模式下运行Ghost时推荐的数据库。

使用以下命令安装MySQL软件包:

sudo apt install mysql-server

运行mysql_secure_installation命令以提高MySQL安装的安全性:

sudo mysql_secure_installation

将会要求您配置VALIDATE PASSWORD PLUGIN,用于测试MySQL用户密码的强度。密码验证策略分为三个级别:低,中和强。如果您不想设置验证密码插件,请按ENTER

在下一个提示符下,将要求您设置MySQL根用户的密码。

一旦您设置了root密码,脚本还将要求您删除匿名用户,限制root用户对本地计算机的访问并删除测试数据库。您应该对所有问题回答Y(是)。

接下来,我们需要将身份验证方法从auth_socket更改为mysql_native_password,以便Ghost安装程序可以访问我们的MySQL服务器。为此,请以root用户身份登录到MySQL服务器:

sudo mysql

并运行以下查询,该查询将在使用mysql_native_password方法时为MySQL根用户设置密码:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_pasword';
FLUSH PRIVILEGES;

请确保您使用强密码更改了very_strong_pasword

安装Ghost-CLI

我们将使用官方的Ghost CLI实用程序安装Ghost。利用该实用程序,您可以在单个命令中安装或更新Ghost。

Ghost CLI作为npm软件包提供。以下命令将在您的Ubuntu系统上全局安装Ghost CLI:

sudo yarn global add ghost-cli

创建Ghost安装位置

我们将在建议的安装位置/var/www/ghost目录中下载并安装Ghost。

创建目录类型:

sudo mkdir -p /var/www/ghost

将目录的所有权更改为您的用户:

sudo chown $USER:$USER /var/www/ghost

$USER是包含您的用户名的环境变量

设置正确的目录权限

sudo chmod 775 /var/www/ghost

安装Ghost

现在,您已经安装了Ghost CLI,并且所有先决条件都已完成,我们可以开始安装。

转到/var/www/ghost目录。

cd /var/www/ghost

要开始安装运行,请运行ghost install​​,它将安装和配置Ghost,将Nginx配置为反向代理,并使用免费的SSL证书进行加密来保护网站。

ghost install
✔ Checking system Node.js version
✔ Checking logged in user
✔ Checking current folder permissions
System checks failed with message: 'Linux version is not Ubuntu 16'
Some features of Ghost-CLI may not work without additional configuration.
For local installs we recommend using `ghost install local` instead.
? Continue anyway? (y/N) y

安装程序将检查您的系统并打印一些警告,如上所示。键入y继续,安装程序将下载并安装Ghost:

ℹ Checking operating system compatibility [skipped]
✔ Checking for a MySQL installation
✔ Checking memory availability
✔ Checking for latest Ghost version
✔ Setting up install directory
✔ Downloading and installing Ghost v1.24.9
✔ Finishing install process

接下来,系统将提示您设置博客URL和MySQL信息。提供我们在Installing MySQL部分中设置的根用户名和密码,并使用默认的数据库名称ghost_prod

? Enter your blog URL: https://example.com
? Enter your MySQL hostname: localhost
? Enter your MySQL username: root
? Enter your MySQL password: [hidden]
? Enter your Ghost database name: ghost_prod

安装程序将创建一个名为ghost的系统用户,并询问您是否要创建一个幽灵MySQL用户,请键入Yes

✔ Configuring Ghost
✔ Setting up instance
Running sudo command: chown -R ghost:ghost /var/www/ghost/content
✔ Setting up "ghost" system user
? Do you wish to set up "ghost" mysql user? Yes
✔ Setting up "ghost" mysql user

接下来,安装程序将询问您是否要设置Nginx。输入Yes进行确认。

? Do you wish to set up Nginx? Yes
✔ Creating nginx config file at /var/www/ghost/system/files/example.com.conf
Running sudo command: ln -sf /var/www/ghost/system/files/example.com.conf /etc/nginx/sites-available/example.com.conf
Running sudo command: ln -sf /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf
Running sudo command: nginx -s reload
✔ Setting up Nginx

一旦设置了Nginx,安装程序就会询问您是否要设置SSL。输入Yes进行确认,配置向导将要求您输入电子邮件地址,然后它将为您的域生成一个免费的“加密SSL”证书并配置Nginx。

? Do you wish to set up SSL? Yes
? Enter your email (used for Let's Encrypt notifications) admin@example.com
Running sudo command: mkdir -p /etc/letsencrypt
Running sudo command: ./acme.sh --install --home /etc/letsencrypt
Running sudo command: /etc/letsencrypt/acme.sh --issue --home /etc/letsencrypt --domain example.com --webroot /var/www/ghost/system/nginx-root --reloadcmd "nginx -s reload" --accountemail admin@example.com
Running sudo command: openssl dhparam -out /etc/nginx/snippets/dhparam.pem 2048
Running sudo command: mv /tmp/ssl-params.conf /etc/nginx/snippets/ssl-params.conf
✔ Creating ssl config file at /var/www/ghost/system/files/example.com-ssl.conf
Running sudo command: ln -sf /var/www/ghost/system/files/example.com-ssl.conf /etc/nginx/sites-available/example.com-ssl.conf
Running sudo command: ln -sf /etc/nginx/sites-available/example.com-ssl.conf /etc/nginx/sites-enabled/example.com-ssl.conf
Running sudo command: nginx -s reload
✔ Setting up SSL

接下来,安装程序将询问您是否要设置systemd服务。键入Y以接受,安装程序将创建一个名为ghost_example-com的新系统服务,并使其在启动时启动:

? Do you wish to set up Systemd? Yes
✔ Creating systemd service file at /var/www/ghost/system/files/ghost_example-com.service
Running sudo command: ln -sf /var/www/ghost/system/files/ghost_example-com.service /lib/systemd/system/ghost_example-com.service
Running sudo command: systemctl daemon-reload
✔ Setting up Systemd

最后,安装程序将设置数据库并询问您是否要启动Ghost,请键入Yes

Running sudo command: /var/www/ghost/current/node_modules/.bin/knex-migrator-migrate --init --mgpath /var/www/ghost/current
✔ Running database migrations
? Do you want to start Ghost? Yes
Running sudo command: systemctl is-active ghost_example-com
✔ Ensuring user is not logged in as ghost user
✔ Checking if logged in user is directory owner
✔ Checking current folder permissions
Running sudo command: systemctl is-active ghost_example-com
✔ Validating config
✔ Checking folder permissions
✔ Checking file permissions
✔ Checking content folder ownership
✔ Checking memory availability
Running sudo command: systemctl start ghost_example-com
✔ Starting Ghost
Running sudo command: systemctl is-enabled ghost_example-com
Running sudo command: systemctl enable ghost_example-com --quiet
✔ Starting Ghost
You can access your publication at https://example.com
Next, go to your admin interface at https://example.com/ghost/ to complete the setup of your publication

Ghost uses direct mail by default
To set up an alternative email method read our docs at https://docs.ghost.org/docs/mail-config

完成Ghost设置

打开浏览器,转到https://example.com/ghost/进入Ghost管理界面,将显示以下屏幕:

要开始使用,请单击Create your account按钮。

系统会将您重定向到“创建帐户”屏幕,您需要在其中输入博客标题以及帐户的全名,电子邮件地址和密码:

填写完详细信息后,单击Invite your team按钮。

在此屏幕上,系统将要求您输入合作者的电子邮件地址。您只需单击I'll do this later, take me to my blog!链接,即可将您重定向到Ghost仪表板:

您可以在此处创建新信息,添加用户并更改Ghosts配置。

结论

在本教程中,您学习了如何安装可用于生产的Ghost实例。

您现在应该访问 Ghost帮助页面,并了解有关如何管理Ghost安装的更多信息。您也可以访问 Ghost Marketplace ,发现Ghost主题。