Ghost是一个轻量级的开源博客平台,易于使用。Ghost是完全可定制的,有许多主题可用。
在本教程中,您将在CentOS 7上设置Ghost。您还将配置Nginx以代理对Ghost的请求,并使Ghost作为系统服务在后台运行。
要完成本教程,您需要:
首先,我们需要安装Ghost。我们将Ghost放在/var/www/ghost
目录中,这是推荐的安装位置。
使用wget从Ghost的GitHub存储库下载最新版本的Ghost :
$ 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才能启动它。
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.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.com
或http://11.11.11.11
。如果此值格式不正确。
Ghost可以在没有邮件设置的情况下运行;只有在您需要支持Ghost用户的密码恢复时才需要它们。我们将在本教程中跳过配置此设置。
您可以按照官方网站上的配置详细信息进一步自定义Ghost 。
保存文件并退出编辑器。
仍在/var/www/ghost
目录中,使用以下命令启动Ghost:
$ sudo npm start --production
输出应类似于以下内容:
> 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正在侦听端口2368
,并且它没有在公共网络接口上侦听,因此您将无法直接访问它。让我们在Ghost面前设置Nginx。
下一步是设置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/called
中创建一个新文件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;
}
}
此基本配置将此服务器的所有请求发送到在端口上运行的Ghost博客2368
,并设置相应的HTTP标头,以便在查看Ghost日志时,您将看到访问者的原始IP地址。您可以在了解Nginx HTTP代理,负载平衡,缓冲和缓存中了解有关此配置的更多信息。
保存文件,退出编辑器,并通过在/etc/nginx/sites-enabled
目录中为此文件创建符号链接来启用此配置:
$ sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost
在我们修改默认的Nginx配置文件并告诉它在sites-enabled
文件夹中包含配置文件之前,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/*;
然后完全注释掉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
如果一切正确,您将看到以下输出:
nginx: 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。
为了提高安全性,我们将在单独的用户帐户下运行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
您应该能够访问您的博客在http://your_domain_or_ip_address
。Nginx将向您的Ghost实例发送请求。
目前,Ghost正在我们的终端上运行。如果我们注销,我们的博客将关闭。让我们让Ghost在后台运行,并确保在系统重启时重启。为此,我们将创建一个systemd
单元文件,指定systemd
应如何管理Ghost。按CTRL+C
停止Ghost,然后按CTRL+D
注销ghost用户帐户。
创建一个新文件来保存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
单元文件,请查看教程了解系统单元和单元文件,这些应该可以让您快速掌握。
保存文件并退出编辑器。然后启用并启动服务:
$ sudo systemctl enable ghost.service
$ sudo sytemctl start ghost.service
再一次,访问http://your_domain_or_ip_address
,你会看到你的博客。
在本教程中,您安装了Ghost,配置了Nginx以代理对Ghost的请求,并确保Ghost作为系统服务运行,腾讯云实验室提供基于CentOS搭建Ghost博客实验平台,欢迎上机使用。
参考文献:《How To Install and Configure Ghost on CentOS 7》
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。