Ubuntu 14.04 安装 Node.js 和 Ghost 博客程序

Ghost 是一款简洁的、功能强大的基于 Node.js 的博客程序,创始人的目标就是替代臃肿膨大的 WordPress 成为最流行的写作程序。毕竟 WordPress 发展了那么多个年头,已经脱离了初始的目标,用户其实只是想要一款简简单单的博客程序而已。

这里我们介绍下在 Ubuntu 14.04 LTS 安装 Node.js 和 Ghost 的方法。

一、安装 Node.js

为了保持最新版本,我们采用 PPA 形式,可以直接用这个脚本导入 PPA:
sudo curl -sL https://deb.nodesource.com/setup | sudo bash -
导入完毕以后,直接安装 Node.js
sudo apt-get install nodejs
这个 Node.js 的包已经包含了 npm 所以你不用单独安装 npm,而有些 Node.js 程序还得单独安装 build-essentials 包,你也可以安装一下:
sudo apt-get install build-essential
安装完毕以后查看一下 Node.js 和 npm 的版本

root@example:~# node -v
v0.10.33
root@example:~# npm -v
1.4.28

Ghost 需要运行在 0.10.* 的 Node.js 版本,请注意。

二、下载 Ghost 程序

基于安全考虑,可以新建立个 ghost 用户,这里我们安装在 /var/www/ 目录下

useradd ghost
mkdir /var/www
cd /var/www
curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip
unzip ghost.zip
chown -R ghost:ghost /var/www

这样就设置好权限并下载好 Ghost 了

三、安装配置 Ghost

首先用 npm 安装
npm install --production
注意有两个减号
npm 安装结束后,可以输入这个命令让 Ghost 以开发者模式启动
npm start
然后就可以本机从 http://127.0.0.1:2368/ 查看搭建好的 Ghost 博客了,由于我们是安装在服务器端,所以你本地浏览器是无法访问的,先按Ctrl + C 停止一下,这时候就会生成一个 config.js,然后修改 config.js
把 url: 'http://my-ghost-blog.com', 这行改成你的域名,比如 url: 'http://example.com',
安装结束,然后我们需要让 Ghost 保持后台运行,并配置 Nginx 反向代理让外网可以访问。

四、让 Ghost 保持后台运行 + 开启启动

这里有两种方式

1、使用 forever 以后台任务运行 Ghost

安装 forever
npm install forever -g
运行 Ghost
NODE_ENV=production forever start index.js
然后可以通过 forever stop index.js 停止 Ghost
也可以通过 forever list 检查 Ghost 是否运行

2、使用 Supervisor 后台运行 Ghost 并开机启动

安装 Supervisor
sudo apt-get install supervisor
创建一个 Ghost 启动脚本文件,如 /etc/supervisor/conf.d/ghost.conf 编辑如下

[program:ghost]
command = node /var/www/index.js
directory = /var/www
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 即可生效
service supervisor restart
也可以使用 Supervisor 启动 Ghost
supervisorctl start ghost
然后我们开始安装 Nginx 配置反向代理让你本地可以访问。

四、安装 Nginx 配置反向代理

首先安装 Nginx

sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get install nginx-extras

修改 /etc/nginx/sites-available/default

server {
    listen	80;
    server_name example.com;
    location / {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:2368;
    }
    location ~* \.(?:ico|css|js|gif|jpe?g|png|ttf|woff)$ {
        access_log off;
        expires 30d;
        add_header Pragma public;
        add_header Cache-Control "public, mustrevalidate, proxy-revalidate";
        proxy_pass http://127.0.0.1:2368;
    }
    location = /robots.txt { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }
    location ~ /\.ht {
            deny all;
    }
}

保存并重启 Nginx
sudo service nginx restart

五、本地浏览器访问 Ghost

然后你就可以在本地用 http://example.com/ 访问刚才搭建好的 Ghost 了,如图:

进入 http://example.com/ghost/setup/开始创建第一个用户,即管理员帐号,接下来开始你的博客之旅吧!如图:

版权声明:
作者:admin
链接:https://www.bttme.com/archives/1054.html
来源:bttme
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>