征服Linux首页投稿(暂停使用,暂停投稿)程序员

在Ubuntu服务器上搭建Ghost博客

2016-01-30  本文已影响2372人  2068e5e51f60

Ghost 是一套基于Node.js 构建的开源博客平台,具有易用的书写界面和优质的用户体验. Ghost博客的内容默认采用Markdown 语法书写.

准备工作

如果你只想在本地运行Ghost博客, 可以参照官方中文文档在本机运行. 本文就不再赘述.

如果你想把Ghost博客部署到服务器上, 需要先准备以下两样东西:

推荐使用Ubuntu系统作服务器, 对新手友好, 且软件库丰富. 目前Ubuntu的长期支持版是14.04, 推荐使用这个版本.

在Ubuntu服务器上安装Ghost博客

由于Ghost是基于Node.js构建的, 所以我们需要先安装Node.js环境, 目前最新的Ghost(0.7.5)需要和4.x版本的Node.js配合使用, 暂不支持5.x版本, 所以不要贸然使用最新版的Node.js, 有可能导致与Ghost不兼容.

安装Node.js环境

如果在执行命令行的时候遇到权限不足的问题, 请在命令行前面加上sudo. 方便起见可以直接以root身份登陆服务器.

在Ubuntu终端中执行以下命令:

bash curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

命令执行结束之后, 在终端窗口中输入 node -v 和 npm -v检查 node 和 npm 是否安装成功.

安装Ghost

如果不会使用vim编辑器可以使用nano编辑器

cd /var/www
curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip

unzip -uo ghost.zip -d ghost
cd ghost
npm install --production
vim /var/www/ghost/config.js

把配置文件中第一个"url"后的域名改为你自己的域名, 比如我的是url: 'http://www.huanglibo.com'

注意: 这里最好不要把www省略, 否则在登录后台的时候会提示访问被拒绝, 就是因为在跳转后台的时候域名会以www开头, 而你的配置文件的域名不含www就会不匹配.

设置Ghost开机自动启动

我推荐使用初始化脚本完成这个工作, 这样可以使用系统的service命令来start/stop/restart, 比forever等软件所用的命令好记忆.

sudo curl https://raw.githubusercontent.com/TryGhost/Ghost-Config/master/init.d/ghost -o /etc/init.d/ghost
sudo useradd -r ghost -U
sudo chown -R ghost:ghost /var/www/ghost
sudo update-rc.d ghost defaults
sudo update-rc.d ghost enable

sudo service ghost start/stop/restart

nginx反向代理及域名配置

先确保你的域名解析到你的服务器, 本文就不再赘述.

sudo apt-get install nginx
rm /etc/nginx/sites-available/default
rm /etc/nginx/sites-enabled/default
rm /etc/nginx/conf.d/default
vim /etc/nginx/sites-available/ghost.conf
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         http://127.0.0.1:2368;
    }
}

以上配置文件已经把反向代理写好了, 唯一需要修改的地方是: 把server_name改成你自己的顶级域名.

sudo ln -s /etc/nginx/sites-available/ghost.conf /etc/nginx/sites-enabled/ghost.conf
sudo service nginx restart

部署完成, 现在你可以访问自己的Ghost博客了;-)

上一篇 下一篇

猜你喜欢

热点阅读