手把手教你搭建一个属于自己的Ghost博客

介绍

Ghost 是基于 Node.js 的开源博客平台,由前 wordpress UI 部门主管 John O’Nolan 和 wordpress 高级工程师(女) Hannah Wolfe 创立,目的是为了给用户提供一种更加纯粹的内容写作与发布平台。

环境

  • 腾讯云CVM
  • CentOS 6.5_x64(纯净版)
  • Node v0.10.40(官方建议版本)
  • Nginx 1.80
  • MysqL 5.1.73
  • Ghost v0.7.4 full (zh)(中文汉化、支持七牛、又拍云、阿里云OSS存储)

步骤

首先使用Putty,Xshell等工具连接到你的云服务器,具体工具的使用就不再介绍了,网络上有许多现有的教程。

安装Gcc、G++

Ghost是基于Node.js构建的开源博客平台,而Node.js的安装需要Gcc达到版本4.8以上。

安装gcc:yum install gcc
安装g++:yum install gcc-c++

此时gcc及g++已安装完毕,由于Centos6.5最高只能自动安装到4.4版本,为了达到Node.js的4.8版本,因此我们需要手动升级

wget http://people.centos.org/tru/devtools-2/devtools-2.repo
mv devtools-2.repo /etc/yum.repos.d
yum install devtoolset-2-gcc devtoolset-2-binutils devtoolset-2-gcc-c++

三个安装包会被装在 /opt/rh/devtoolset-2/root/ 中
更新软链接

mv /usr/bin/gcc /usr/bin/gcc-4.4.7
mv /usr/bin/g++ /usr/bin/g++-4.4.7
mv /usr/bin/c++ /usr/bin/c++-4.4.7
ln -s /opt/rh/devtoolset-2/root/usr/bin/gcc /usr/bin/gcc
ln -s /opt/rh/devtoolset-2/root/usr/bin/c++ /usr/bin/c++
ln -s /opt/rh/devtoolset-2/root/usr/bin/g++ /usr/bin/g++
gcc --version

如果显示以上信息,恭喜你,安装成果了~

安装Node

Ghost是基于Node.js构建的开源博客平台,所以我们首先搭建Node环境。

wget http://nodejs.org/dist/v0.10.40/node-v0.10.40.tar.gz  
tar zxvf node-v0.10.40.tar.gz  
cd node-v0.10.40  
./configure 
make && make install

命令执行完毕之后,检测一下环境是否配置成功。

node -v  
v0.10.40

如果显示以上信息,恭喜你,安装成功了~

安装Nginx

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。
首先在/etc/yum.repos.d/目录下创建一个配置文件Nginx.repo:

vi /etc/yum.repos.d/Nginx.repo

写入以下内容

[Nginx] 
name=Nginx repo 
baseurl=http://Nginx.org/packages/centos/$releasever/$basearch/ 
gpgcheck=0 
enabled=1 

保存。(按i编辑,按Esc结束编辑,:x 保存修改退出,:q! 强制退出,放弃修改) 继续执行以下指令:

yum install Nginx -y # 安装Nginx 
service Nginx start # 启动Nginx 
chkconfig Nginx on # 设置开机启动Nginx 

这样Nginx就安装成功了,在浏览器中输入你的VPS的IP就可以看到提示:“Welcome to Nginx!”

配置Nginx

安装好了Nginx后,我们需要设置一个代理服务器让我们的博客可以使用域名访问。 在/etc/Nginx/conf.d目录下创建一个配置文件ghost.conf:

vi /etc/Nginx/conf.d/ghost.conf

写入以下内容

server {  
    listen 80;
    server_name example.com; #将 example.com 改为你的域名或ip。

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

保存退出,重启Nginx

service Nginx restart

这样就大功告成啦~

安装MysqL

Ghost 认使用 sqlite3 数据库,对于一般使用足够了,但是内容多的话,就会拖慢整个系统,也就影响页面打开速度了,不想使用MysqL的朋友可以跳过这步。
首先输入以下指令:

yum install MysqL MysqL-server # 安装MysqL 
service MysqLd start # 启动MysqL 
chkconfig MysqLd on # 设置开机启动MysqL 

接着输入MysqL_secure_installation配置MysqL

Set root password? [Y/n] # 设置root密码 
anonymous users? [Y/n] # 删除匿名用户 
disallow root login remotely? [Y/n] # 禁止root用户远程登录 
Remove test database and access to it? [Y/n] # 删除认的 test 数据库 
Reload privilege tables Now? [Y/n] # 刷新授权表使修改生效 

为了避免数据库存放的中文是乱码,我们还需要设置MysqL的编码:

vi /etc/my.cnf

写入以下内容

[client]
default-character-set=utf8 [MysqL] default-character-set=utf8 [MysqLd] character-set-server=utf8 collation-server=utf8_general_ci 

保存退出,重启MysqL

service MysqLd restart

最后我们需要新建一个数据库,用来存放博客的数据:

MysqL -u root -p # 输入设置好的密码  
create database ghost; # 创建ghost数据库  
grant all privileges on ghost.* to 'ghost'@'%' identified by '123456'; # 新建一个用户ghost,密码为123456,随意更改啦  
flush privileges # 重新读取权限表中的数据到内存,不用重启MysqL就可以让权限生效

这样数据库的准备工作也就完成啦~

安装Ghost

做了这么多准备工作,终于要开始折腾我们的主角啦。 首先下载Ghost:

cd /var/www  
wget http://dl.ghostchina.com/Ghost-0.7.4-zh-full.zip  
unzip Ghost-0.7.4-zh-full.zip -d ghost  
cd ghost

接着修改认配置:

cp config.example.js config.js  
vi config.js

Ghost有产品模式、开发模式和测试模式等多种运行模式,这里我们需要在配置文件中找到production模式:

# 生产模式
production: { url: 'http://wuxu.site',# 修改为你的域名或者IP,注意加上http:// mail: {},database: { client: 'MysqL' connection: { host : '127.0.0.1',user : 'ghost',# 数据库连接的用户 password : '123456',# 先前创建的密码 database : 'ghost',# 先前创建的数据库 charset : 'utf8' },server: { host: '127.0.0.1',port: '2368' # 若修改该端口记得在Nginx中做相应改变 }
    }

保存退出,接下来就到了见证奇迹的时刻啦,输入指令:

npm start --production

启动浏览器,输入之前配置的域名或者IP,我们就可以看到建立好的Ghost博客啦。 (Ctrl+C 中断掉开发者模式)

让Ghost保持运行

前面提到的启动 Ghost 使用 npm start –production 命令。这是一个在开发模式下启动和测试的不错的选择,但是通过这种命令行启动的方式有个缺点,即当你关闭终端窗口或者从 SSH 断开连接时,Ghost 就停止了。为了防止 Ghost 停止工作,我们得解决这个问题。

以下有几种解决方案:

PM2(https://github.com/Unitech/pm2)
Forever (https://npmjs.org/package/forever)
Supervisor (http://supervisord.org/)

这里我们使用PM2让Ghost保持运行:

cd /var/www/ghost  
npm install pm2 -g # 安装PM2 
NODE_ENV=production pm2 start index.js --name "ghost"  
pm2 startup centos  
pm2 save

PS:
因为GFW的强大,在上一步直接使用npm安装依赖的时候可能出现无法安装的情况,这时候可以使用以下代码

npm install -g cnpm --registry=https://registry.npm.taobao.org  
cnpm install pm2 -g  
NODE_ENV=production pm2 start index.js --name "ghost"  
pm2 startup centos  
pm2 save

这样一来,我们的Ghost博客就可以保持运行啦,你可以使用以下指令来控制Ghost博客

pm2 start/stop/restart ghost 
初始化Ghost

现在所有准备工作都做好了,打开你的浏览器,在浏览器中输入<你的 URL>/ghost/,开始初始化你的Ghost吧~

跟着提示完成初始化之后,你就可以开始你的Ghost之旅了~

关于一些Ghost更加具体的设置过两天我会继续写出来,希望能给大家一点帮助。

小结

Ghost作为一个新兴的博客系统肯定会有一些不足,但是我们相信它会越来越好的,希望大家都能用它来记录生活中一些美好的时刻,分享自己的精彩的创意,共同建立并且维护这样一个和谐的互联网大家庭。

参考:

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native