文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
让 Ghost 一直运行
前面提到的启动 Ghost 使用 npm start
命令。这是一个在开发模式下启动和测试的不错的选择,但是通过这种命令行启动的方式有个缺点,即当你关闭终端窗口或者从 SSH 断开连接时,Ghost 就停止了。为了防止 Ghost 停止工作,有两种方式解决这个问题。
Forever (https://npmjs.org/package/forever)
你可以使用 forever
以后台任务运行 Ghost 。 forever
将会按照 Ghost 的配置,当进程 crash 后重启 Ghost。
- 通过
npm install forever -g
安装forever
- 为了让
forever
从 Ghost 安装目录运行,输入NODE_ENV=production forever start index.js
- 通过
forever stop index.js
停止 Ghost - 通过
forever list
检查 Ghost 当前是否正在运行
Supervisor (http://supervisord.org/)
流行的 Linux 发行版——例如 Fedora, Debian 和 Ubuntu,都包含一个 Supervisor 包:一个进程控制系统,允许在启动的时候无需初始化脚本就能运行 Ghost。不像初始化脚本一样,Supervisor 可以移植到不同的发行版和版本。
- 根据不同的 Linux 发行版 安装 Supervisor 。如下所示:
- Debian/Ubuntu:
apt-get install supervisor
- Fedora:
yum install supervisor
- 其他大多数发行版:
easy_install supervisor
- Debian/Ubuntu:
- 通过
service supervisor start
确保 Supervisor 运行 - 为 Ghost 创建一个启动脚本。通常为
/etc/supervisor/conf.d/ghost.conf
,例如:
[program:ghost] command = node /path/to/ghost/index.js directory = /path/to/ghost user = ghost autostart = true autorestart = true stdout_logfile = /var/log/supervisor/ghost.log stderr_logfile = /var/log/supervisor/ghost_err.log environment = NODE_ENV="production"
- 使用 Supervisor 启动 Ghost:
supervisorctl start ghost
- 停止 Ghost:
supervisorctl stop ghost
详细内容请参阅 Supervisor 文档 。
初始化脚本
Linux 系统在启动的时候会运行初始化脚本。这些脚本通常存在于 /etc/init.d 。为了让 Ghost 一直运行下去甚至自动重启,你可以设置一个初始化脚本来完成这个任务。以下的例子工作在 Ubuntu ,并且在 Ubuntu 12.04 下测试通过。
- 使用以下命令创建 /etc/init.d/ghost 文件:
$ sudo curl https://raw.github.com/TryGhost/Ghost-Config/master/init.d/ghost \ -o /etc/init.d/ghost
- 使用
nano /etc/init.d/ghost
命令打开文件并检查以下内容: - 将
GHOST_ROOT
变量的值更换为你的 Ghost 安装路径 - 检查
DAEMON
变量的值是否和which node
的输出值相同 - 这个初始化脚本将在你的系统上以它自己的 Ghost 用户和用户组运行,使用以下命令来创建:
$ sudo useradd -r ghost -U
- 确保 Ghost 用户可以访问安装目录:
$ sudo chown -R ghost:ghost /你的 Ghost 安装目录
- 使用以下命令给这个初始化脚本加上可执行权限:
$ sudo chmod 755 /etc/init.d/ghost
- 现在你可以使用以下的命令来控制 Ghost :
$ sudo service ghost start $ sudo service ghost stop $ sudo service ghost restart $ sudo service ghost status
- 为了让 Ghost 能在系统启动时同时启动,我们必须要将刚刚创建的初始化脚本注册为为启动项。 执行以下两个命令:
$ sudo update-rc.d ghost defaults $ sudo update-rc.d ghost enable
- 为了保证你的用户可以更改 Ghost 目录里的文件和默认的 config.js ,需要将你加入 ghost 用户组中:
$ sudo adduser 你的用户名 ghost
- 如果你现在重启你的服务器,Ghost 应该会自动运行。
使用 forever 和如何在 ubuntu 下建立 Ghost 守护进程的文档即将发布!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论