如何在 CentOS 7 上安装和配置 Ghost

凯文·艾萨克DigitalOcean 的开发人员和作者。如何在 CentOS 7 上安装和配置 Ghost

介绍

Ghost是一个易于使用的轻量级开源博客平台。Ghost 是完全可定制的,有许多可用的主题。

在本教程中,您将在 CentOS 7 上设置 Ghost。您还将配置 Nginx 以代理对 Ghost 的请求,并让 Ghost 作为系统服务在后台运行。

先决条件

要完成本教程,您需要:

第 1 步 — 安装 Ghost

首先,我们需要安装 Ghost。我们将把 Ghost 放在/var/www/ghost目录中,这是推荐的安装位置。

使用以下命令从 Ghost 的 GitHub 存储库下载最新版本的 Ghost wget

wget https://ghost.org/zip/ghost-latest.zip

复制

要解压存档,首先unzip使用包管理器安装程序。在安装新程序之前确保系统是最新的总是一个好主意,因此更新软件包并unzip使用以下命令进行安装:

sudo yum update -y
sudo yum install unzip -y

复制

上述命令中的-y标志会自动更新和安装软件包,而不需要用户确认。

安装完成后,将unzip下载的包解压到/var/www/ghost目录。首先,创建/var/www文件夹,然后解压缩文件:

sudo mkdir /var/www
sudo unzip -d /var/www/ghost ghost-latest.zip

复制

切换到 /var/www/ghost/目录:

cd /var/www/ghost/

复制

然后安装 Ghost 依赖项,但只安装生产所需的那些。这会跳过只有开发 Ghost 的人才需要的任何依赖项。

sudo npm install --production

复制

一旦此过程完成,就会安装 Ghost,但我们需要先设置 Ghost,然后才能启动它。

第 2 步 — 配置 Ghost

Ghost 使用位于/var/www/ghost/config.js. 该文件并非开箱即用,但 Ghost 安装包含该文件config.example.js,我们将使用该文件作为起点。

将示例配置文件复制到/var/www/ghost/config.js. 我们将复制文件而不是移动它,以便我们拥有原始配置文件的副本,以防我们需要恢复您的更改。

sudo cp config.example.js config.js

复制

打开文件进行编辑:

sudo vi config.js

复制

我们必须更改 Ghost 使用的 URL。如果我们不这样做,博客上的链接会将访问者带到my-ghost-blog.comurl如果您现在不想使用域,请将字段的值更改为您的域名或服务器的 IP 地址。/var/www/ghost/config.js


...

config = {
    // ### Production
    // When running Ghost in the wild, use the production environment
    // Configure your URL and mail settings here
    production: {
        url: 'http://your_domain_or_ip_address',
        mail: {},
...

url值必须采用 URL 的形式,例如或。如果该值的格式不正确,Ghost 将不会启动。http://example.comhttp://11.11.11.11

Ghost 可以在没有邮件设置的情况下运行;仅当您需要支持 Ghost 用户的密码恢复时才需要它们。我们将在本教程中跳过配置此设置。

您可以按照官方网站上的配置详细信息进一步自定义 Ghost 。

保存文件并退出编辑器。

仍在/var/www/ghost目录中时,使用以下命令启动 Ghost:

sudo npm start --production

复制

输出应类似于以下内容:

Output
> ghost@0.11.7 start /var/www/ghost
> node index

WARNING: Ghost is attempting to use a direct method to send email.
It is recommended that you explicitly configure an email service.
Help and documentation can be found at http://support.ghost.org/mail.

Migrations: Creating tables...
...

Ghost is running in production...
Your blog is now available on http://your_domain_or_ip_address
Ctrl+C to shut down

Ghost 正在监听 port 2368,并且它没有监听公共网络接口,所以你将无法直接访问它。让我们在 Ghost 前面设置 Nginx。

第 3 步 — 配置 Nginx 以将请求代理到 Ghost

下一步是设置 Nginx 来为我们的 Ghost 博客服务。这将允许端口80上的连接连接到运行 Ghost 的端口,因此人们可以访问您的 Ghost 博客,而无需:2368在地址末尾添加 。它还增加了一层间接性,并让您在博客增长时扩展您的博客。

如果 Ghost 仍在您的终端中运行,请CTRL+C在继续之前按关闭 Ghost 实例。

现在让我们配置 Nginx。先切换到/etc/nginx目录:

cd /etc/nginx/

复制

如果您按照先决条件教程所示从 CentOS EPEL 存储库安装 Nginx,您将没有用于管理网站配置的sites-available和目录。sites-enabled让我们创建它们:

sudo mkdir sites-available
sudo mkdir sites-enabled

复制

/etc/nginx/sites-available/接下来,在调用中创建一个新文件ghost

sudo vi /etc/nginx/sites-available/ghost

复制

将以下配置放入文件中并更改your_domain_or_ip_address为您的域名,如果您没有域,则更改为您的服务器 IP 地址:/etc/nginx/sites-available/ghost

server {
    listen 80;
    server_name your_domain_or_ip_address;
    location / {
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass         http://127.0.0.1:2368;
    }
}

此基本配置会将对该服务器的所有请求发送到运行在 port 上的 Ghost 博客2368,并设置适当的 HTTP 标头,以便当您查看 Ghost 日志时,您将看到访问者的原始 IP 地址。您可以在了解 Nginx HTTP 代理、负载平衡、缓冲和缓存中了解有关此配置的更多信息。

保存文件,退出编辑器,并通过在目录中为此文件创建符号链接来启用此配置/etc/nginx/sites-enabled

sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost

复制

sites-enabled在我们修改默认的 Nginx 配置文件并告诉它在文件夹中包含配置文件之前,Nginx 不会使用这个新配置。此外,我们必须禁用默认站点。nginx.conf在编辑器中打开文件:

sudo vi nginx.conf

复制

在块中包含以下行http以将配置文件包含在文件sites-enabled夹中:/etc/nginx/nginx.conf


http {
...
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

然后完全注释掉在server块内找到的http块:/etc/nginx/nginx.conf

...

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;


#    server {
#       listen       80 default_server;
#       listen       [::]:80 default_server;
#       server_name  _;
#       root         /usr/share/nginx/html;
#
#       # Load configuration files for the default server block.
#       include /etc/nginx/default.d/*.conf;
#
#       location / {
#       }
#
#       error_page 404 /404.html;
#           location = /40x.html {
#       }
#
#       error_page 500 502 503 504 /50x.html;
#           location = /50x.html {
#       }
...
...

保存文件并退出编辑器。测试配置以确保没有问题:

sudo nginx -t

复制

如果一切正确,您将看到以下输出:

Outputnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

如果您看到任何错误,请修复它们并重新测试配置。

使用有效的配置文件,重新启动 Nginx 以应用更改:

sudo systemctl restart nginx

复制

在我们再次启动 Ghost 之前,让我们创建一个新的用户帐户来运行 Ghost。

第 4 步 – 以独立用户身份运行 Ghost

为了提高安全性,我们将在单独的用户帐户下运行 Ghost。此用户将只能访问该/var/www/ghost目录及其主文件夹。这样,如果 Ghost 受到威胁,您可以最大限度地减少对系统的潜在损害。

ghost使用以下命令创建一个新用户:

sudo adduser --shell /bin/bash ghost

复制

然后让这个新用户成为/var/www/ghost目录的所有者:

sudo chown -R ghost:ghost /var/www/ghost/

复制

现在让我们确保这个用户可以运行 Ghost。以ghost用户身份登录:

sudo su - ghost

复制

现在在这个用户下启动 Ghost 并确保它运行:

cd /var/www/ghost
npm start --production

复制

您应该可以通过 访问您的博客。Nginx 将向您的 Ghost 实例发送请求。http://your_domain_or_ip_address

一切都很好,但让我们确保 Ghost 在未来继续运行良好。

第 5 步 — 将 Ghost 作为系统服务运行

目前,Ghost 正在我们的终端中运行。如果我们注销,我们的博客将关闭。让我们让 Ghost 在后台运行,并确保它在系统重新启动时重新启动。为此,我们将创建一个systemd指定如何systemd管理 Ghost 的单元文件。按CTRL+C停止 Ghost,然后按 退出ghost用户帐户CTRL+D

创建一个新文件来保存systemd单元文件的定义:

sudo vi /etc/systemd/system/ghost.service

复制

将以下配置添加到文件中,该文件定义了服务的名称、服务的组和用户,以及有关它应该如何启动的信息:/etc/systemd/system/ghost.service

[Unit]
Description=Ghost
After=network.target

[Service]
Type=simple

WorkingDirectory=/var/www/ghost
User=ghost
Group=ghost

ExecStart=/usr/bin/npm start --production
ExecStop=/usr/bin/npm stop --production
Restart=always
SyslogIdentifier=Ghost

[Install]
WantedBy=multi-user.target

如果您不熟悉systemd单元文件,请查看教程了解 Systemd 单元和单元文件,它可以让您快速上手。

保存文件并退出编辑器。然后启用并启动服务:

sudo systemctl enable ghost.service
sudo sytemctl start ghost.service

复制

再次访问,您将看到您的博客。http://your_domain_or_ip_address

结论

在本教程中,您安装了 Ghost,配置了 Nginx 以代理对 Ghost 的请求,并确保 Ghost 作为系统服务运行。不过,您可以使用 Ghost 做更多事情

原文 https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-ghost-on-centos-7

向上生长
向上生长
文章: 71
订阅评论
提醒
guest

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x