阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Ubuntu 16.04上安装和配置Ghost 博客

120次阅读
没有评论

共计 4447 个字符,预计需要花费 12 分钟才能阅读完成。

Ubuntu 16.04 上安装和配置 Ghost 博客

介绍

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

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

先决条件

要完成本教程,您需要:

  • 一个 1GB Ubuntu 16.04 服务器通过遵循 Ubuntu 16.04 初始服务器设置指南设置,包括 sudo 非 root 用户和防火墙。
  • Node.js 使用官方 PPA 安装,如解释如何在 Ubuntu 16.04 上安装 Node.js。http://www.linuxidc.com/Linux/2016-09/135487.htm
  • Nginx 安装在您的服务器上,如如何在 Ubuntu 16.04 上安装 Nginx。http://www.linuxidc.com/Linux/2017-07/145522.htm

第 1 步 – 安装 Ghost

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

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

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

要解压缩存档,请首先使用软件包管理器安装 unzip 程序:

sudo apt-get install unzip

然后将下载的软件包解压缩到 /var/www/ghost 目录:

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 nano config.js

首先,我们必须更改 Ghost 使用的 URL。 如果我们没有,博客上的链接会将访问者带到 my-ghost-blog.com。 url 字段的值更改为您的域名,或者更改为服务器的 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 的形式,例如 http:// example.comhttp:// 11.11.11.11 如果此值格式不正确,Ghost 将无法启动。

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

您可以通过遵循官方网站上的配置详细信息进一步自定义 Ghost。

保存文件并退出编辑器。

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

sudo npm start --production

输出应类似于以下内容:

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

WARNING:Ghostis attempting to use a direct method to send email.Itis recommended that you explicitly configure an email service.Helpand documentation can be found at http://support.ghost.org/mail.Migrations:Creating tables......Ghostis running in production...Your blog is now available on http://your_domain_or_ip_addressCtrl+C to shut down

Ghost 在端口 2368 上监听,如果您已按先决条件文章中所述启用 UFW 防火墙,则您将无法直接访问它。 让我们在 Ghost 前面设置 Nginx。

Ubuntu 16.04 上安装和配置 Ghost 博客

第 3 步 – 配置 Nginx 到 Ghost 的代理请求

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

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

现在让我们配置 Nginx。 更改到 /etc/nginx 目录,并删除 /etc/nginx/sites-enabled 中的默认 Nginx 配置文件:

cd /etc/nginx/
sudo rm sites-enabled/default

/etc/nginx/sites-available/ called 中创建一个新文件ghost

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

将以下配置放在该文件中,并将 your-domain-name 更改为您的域名,如果没有域,则将您的服务器 IP 地址更改your-domain-name

/ 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;}}

此基本配置将此服务器的所有请求发送到在端口 2368 上运行的 Ghost 博客,并设置适当的 HTTP 标头,以便在查看 Ghost 日志时,您将看到访问者的原始 IP 地址。

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

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

然后测试配置以确保没有问题:

sudo nginx -t

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

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

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

使用正在运行的配置文件,重新启动 Nginx 以应用更改:

sudo service nginx restart

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

第 4 步 – 将 Ghost 作为单独用户运行

为了提高安全性,我们将在单独的用户帐户下运行 Ghost。 此用户将只能访问 /var/www/ghost 目录及其主文件夹。 这样,如果 Ghost 被破坏,您最小化对系统的潜在损害。

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

sudo adduser --shell /bin/bash --gecos 'Ghost application'ghost

然后使此新用户成为 /var/www/ghost 目录的所有者:

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

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

su - ghost

现在启动此用户下的 Ghost,并确保它运行:

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

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

事情工作很好,但让我们确保 Ghost 在未来继续良好运行。

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

目前,Ghost 正在我们的终端上运行。 如果我们注销,我们的博客将关闭。 让我们得到 Ghost 在后台运行,并确保它重新启动时系统重新启动。 为此,我们将创建一个 systemd 单元文件,指定 systemd 应如何管理 Ghost。

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

nano /etc/systemd/system/ghost.service

将以下配置添加到文件,该文件定义服务的名称,服务的组和用户以及如何启动它的信息:

/etc/systemd/system/ghost.service
[Unit]Description=GhostAfter=network.target

[Service]Type=simple

WorkingDirectory=/var/www/ghostUser=ghostGroup=ghostExecStart=/usr/bin/npm start --production
ExecStop=/usr/bin/npm stop --production
Restart=always
SyslogIdentifier=Ghost[Install]WantedBy=multi-user.target

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

systemctl enable ghost.service
sytemctl start ghost.service

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

结论

在本教程中,您安装了 Ghost,配置 Nginx 来代理对 Ghost 的请求,并确保 Ghost 作为系统服务运行。

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-07/145695.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计4447字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中