介绍
Ghost是一个轻量级的开源博客平台,易于使用。
在本教程中,您将在CentOS 7上设置Ghost,您还将配置Nginx来向Ghost发送代理请求,并将Ghost作为系统服务在后台运行。
前提条件
要完成本教程,您将需要:
- 一个 1GB CentOS 7 服务器设置,按照 初始服务器设置与 CentOS 7 指南,包括一个 sudo 非根用户
- Node.js 安装使用教程中解释的 EPEL 存储方法: 如何在 CentOS 7 服务器上安装 Node.js。
- Nginx 安装在您的服务器上,如 如何在 CentOS 7 上安装 Nginx所示。
步骤 1 - 安装幽灵
首先,我们需要安装 Ghost. 我们将 Ghost 放置在 /var/www/ghost
目录中,这是推荐的安装位置。
从Ghost的GitHub存储库下载Ghost的最新版本,使用wget:
1wget https://ghost.org/zip/ghost-latest.zip
要解包档案,先安装unzip
程序与包管理器. 确保系统在安装新程序之前是最新的始终是一个好主意,所以更新包和安装unzip
使用以下命令:
1sudo yum update -y
2sudo yum install unzip -y
上面的命令中的y
旗自动更新和安装包,而不需要用户的确认。
安裝「unzip」後,將下載的包解密至「/var/www/ghost」目錄,先創建「/var/www」文件夾,然後解密檔案:
1sudo mkdir /var/www
2sudo unzip -d /var/www/ghost ghost-latest.zip
转到 /var/www/ghost/
目录:
1cd /var/www/ghost/
然后安装 Ghost 依赖性,但只安装用于生产所需的依赖性,从而跳过只有开发 Ghost 的用户所需的任何依赖性。
1sudo npm install --production
一旦这个过程完成,Ghost 将被安装,但我们需要在开始之前设置Ghost。
步骤 2 – 配置幽灵
Ghost 使用位于 /var/www/ghost/config.js 的配置文件. 这个文件不存在于框中,但 Ghost 安装中包含了
config.example.js 文件,我们将使用它作为起点。
将示例配置文件复制到 /var/www/ghost/config.js
. 我们将复制文件,而不是移动它,以便我们有原始配置文件的副本,如果我们需要重置您的更改。
1sudo cp config.example.js config.js
打开文件以编辑:
1sudo vi config.js
如果我们不这样做,博客上的链接会将访问者带到 my-ghost-blog.com。
1[label /var/www/ghost/config.js]
2
3...
4
5config = {
6 // ### Production
7 // When running Ghost in the wild, use the production environment
8 // Configure your URL and mail settings here
9 production: {
10 url: 'http://your_domain_or_ip_address',
11 mail: {},
12...
url
值必须以URL的形式,例如http://example.com
或http://11.11.11.11
。
Ghost 可以在没有邮件设置的情况下运行;如果您需要支持 Ghost 用户的密码恢复,则只需要这些设置。
您可以通过在 官方网站的配置细节进一步定制Ghost。
保存文件并离开编辑器。
虽然仍在 /var/www/ghost
目录中,请使用以下命令启动 Ghost:
1sudo npm start --production
输出应该与以下相似:
1[secondary_label Output]
2
3> ghost@0.11.7 start /var/www/ghost
4> node index
5
6WARNING: Ghost is attempting to use a direct method to send email.
7It is recommended that you explicitly configure an email service.
8Help and documentation can be found at http://support.ghost.org/mail.
9
10Migrations: Creating tables...
11...
12
13Ghost is running in production...
14Your blog is now available on http://your_domain_or_ip_address
15Ctrl+C to shut down
Ghost在端口2368
上聆听,而不是在公共网络界面上聆听,所以你将无法直接访问它。
步骤 3 — 将 Nginx 配置为 Ghost 的代理请求
下一步是设置 Nginx 来服务我们的 Ghost 博客,这将允许端口 80 上的连接连接到 Ghost 正在运行的端口,以便人们可以访问您的 Ghost 博客,而不会在地址的末尾添加
:2368。
如果 Ghost 仍在您的终端中运行,请按CTRL+C
关闭 Ghost 实例,然后继续。
现在让我们配置 Nginx. 首先更改到 /etc/nginx
目录:
1cd /etc/nginx/
如果您从 CentOS EPEL 存储库中安装了 Nginx,如前提教程所示,您将没有用于管理网站配置的网站可用
和网站可用
目录。
1sudo mkdir sites-available
2sudo mkdir sites-enabled
接下来,在 /etc/nginx/sites-available/
中创建一个名为 ghost
的新文件:
1sudo vi /etc/nginx/sites-available/ghost
将下列配置放入文件中,并将your_domain_or_ip_address
更改为您的域名或服务器的IP地址,如果您没有域名:
1[label /etc/nginx/sites-available/ghost]
2server {
3 listen 80;
4 server_name your_domain_or_ip_address;
5 location / {
6 proxy_set_header HOST $host;
7 proxy_set_header X-Forwarded-Proto $scheme;
8 proxy_set_header X-Real-IP $remote_addr;
9 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
10 proxy_pass http://127.0.0.1:2368;
11 }
12}
此基本配置将此服务器的所有请求发送到Ghost博客,该网站运行在端口2368
,并设置适当的HTTP标题,以便当您查看Ghost日志时,您可以看到访问者的原始IP地址。
保存文件,退出编辑器,并通过在 /etc/nginx/sites-enabled
目录中创建此文件的 symlink 来启用此配置:
1sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost
Nginx不会使用这个新配置,直到我们修改默认的 Nginx 配置文件,并告诉它将配置文件包含在网站启用
文件夹中。
1sudo vi nginx.conf
在http
块中包含下列行,以便在网站启用
文件夹中包含配置文件:
1[label /etc/nginx/nginx.conf]
2
3http {
4...
5 # Load modular configuration files from the /etc/nginx/conf.d directory.
6 # See http://nginx.org/en/docs/ngx_core_module.html#include
7 # for more information.
8 include /etc/nginx/conf.d/*.conf;
9 include /etc/nginx/sites-enabled/*;
然后完全评论服务器
块在http
块中:
1[label /etc/nginx/nginx.conf]
2...
3
4 # Load modular configuration files from the /etc/nginx/conf.d directory.
5 # See http://nginx.org/en/docs/ngx_core_module.html#include
6 # for more information.
7 include /etc/nginx/conf.d/*.conf;
8 include /etc/nginx/sites-enabled/*;
9
10# server {
11# listen 80 default_server;
12# listen [::]:80 default_server;
13# server_name _;
14# root /usr/share/nginx/html;
15#
16# # Load configuration files for the default server block.
17# include /etc/nginx/default.d/*.conf;
18#
19# location / {
20# }
21#
22# error_page 404 /404.html;
23# location = /40x.html {
24# }
25#
26# error_page 500 502 503 504 /50x.html;
27# location = /50x.html {
28# }
29...
30...
保存文件并离开编辑器. 测试配置以确保没有问题:
1sudo nginx -t
你会看到以下输出,如果一切都正确:
1[secondary_label Output]
2nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
3nginx: configuration file /etc/nginx/nginx.conf test is successful
如果您看到任何错误,请修复它们并重新测试配置。
使用工作配置文件,重新启动 Nginx 以应用更改:
1sudo systemctl restart nginx
在重新启动Ghost之前,让我们创建一个新的用户帐户来运行Ghost。
步骤 4 – 运行 Ghost 作为独立用户
为了提高安全性,我们将在单独的用户帐户下运行Ghost。这个用户只会访问‘/var/www/ghost’目录和其主文件夹。
使用以下命令创建一个新的幽灵
用户:
1sudo adduser --shell /bin/bash ghost
然后让这个新用户成为 /var/www/ghost
目录的所有者:
1sudo chown -R ghost:ghost /var/www/ghost/
现在让我们确保这个用户可以运行 Ghost. 登录为ghost
用户:
1sudo su - ghost
现在在这个用户下启动 Ghost,并确保它运行:
1cd /var/www/ghost
2npm start --production
你应该能够访问你的博客在http://your_domain_or_ip_address`. Nginx将发送请求到你的Ghost实例。
事情很好,但让我们确保Ghost在未来继续运行。
步骤 5 – 运行 Ghost 作为系统服务
目前,Ghost在我们的终端中运行,如果我们关闭登录,我们的博客将关闭,让我们让Ghost在后台运行,并确保在系统重新启动时重新启动。 要做到这一点,我们将创建一个systemd
单元文件,说明systemd
应该如何管理Ghost。 按CTRL+C
来停止Ghost,然后按CTRL+D
来退出ghost
用户帐户。
创建一个新的文件,以保持systemd
单元文件的定义:
1sudo vi /etc/systemd/system/ghost.service
将下列配置添加到文件中,该文件定义了服务的名称、服务的组和用户,以及该服务应该如何开始的信息:
1[label /etc/systemd/system/ghost.service]
2[Unit]
3Description=Ghost
4After=network.target
5
6[Service]
7Type=simple
8
9WorkingDirectory=/var/www/ghost
10User=ghost
11Group=ghost
12
13ExecStart=/usr/bin/npm start --production
14ExecStop=/usr/bin/npm stop --production
15Restart=always
16SyslogIdentifier=Ghost
17
18[Install]
19WantedBy=multi-user.target
如果您不熟悉systemd
单元文件,请查看理解系统单元和单元文件
(https://andsky.com/tech/tutorials/understanding-systemd-units-and-unit-files)的教程,该教程应该让你快速加速。
保存文件并离开编辑器,然后启用并启动服务:
1sudo systemctl enable ghost.service
2sudo sytemctl start ghost.service
再次访问http://your_domain_or_ip_address
,你会看到你的博客。
结论
在本教程中,您安装了Ghost,将Nginx配置为Ghost的代理请求,并确保Ghost作为系统服务运行。