Ghost 博客系统搭建

导语:
Ghost 是一套基于 Node.js 构建的开源博客平台(Open source blogging platform),具有易用的书写界面和体验,博客内容默认采用 Markdown 语法书写,无论是从架构、设计、易用性,它都要比Wordpress要好,界面简洁,专注写作,支持在线预览,在线写作。

准备:
  • 一台云服务器
  • 一个域名(域名要解析到该服务器的IP,没域名可用云服务器的IP访问)
环境:
Centos6.4_x64
Node v0.10.40(官方建议版本)
Nginx 1.80
MySql 5.1.73
Ghost v0.7.4 full(zh)
步骤:

首先用Linux连接工具连上你的服务器,我用的是Xshell,既然Ghost基于Node.js构建,那么第一步肯定是先装Node.js环境啦。
下载安装:

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

但是,重点来了,这样安装可能会出现以下错误

npm ERR! install Couldn't read dependencies
npm ERR! package.json ENOENT, open '/root/package.json'
npm ERR! package.json This is most likely not a problem with npm itself.
npm ERR! package.json npm can't find a package.json file in your current directory.

npm ERR! System Linux 3.11.0-15-generic
npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install"
npm ERR! cwd /root
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3
npm ERR! path /root/package.json
npm ERR! code ENOPACKAGEJSON
npm ERR! errno 34
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR! /root/npm-debug.log
npm ERR! not ok code 0

原因是Node的安装需要依赖某些环境包,所以我们得先把环境包装上,再来安装Node,正确安装姿势步骤如下:

yum -y install libtool automake autoconf gcc-c++ openssl-devel
wget http://nodejs.org/dist/v0.10.40/node-v0.10.40.tar.gz
tar zxf node-v0.10.40.tar.gz 
cd node-v0.10.40
./configure 
make && make install

这下就可以安装成功了,那如何检测已经安装成功呢

node -v 
v0.10.40 //有显示出版本即安装成功

安装完node接下来就准备安装Nginx和MySql了,我们知道有些软件可以直接用yum命令装,比如前面写到的,但是有些比较特殊,装不了,因为默认的yum源里没有对应的rpm包,怎么办呢?没有我们可以创一个,怎么做:

vim /etc/yum.repos.d/nginx.repo //新建yum源

将一下信息写入新建的yum源里,当我们再使用yum的时候就可以识别到了

[nginx] 
name=nginx repo  
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/  
gpgcheck=0  
enabled=1  
//然后保存退出:按Esc键,然后输入:x(有':')

接着我们就可以用yum安装Nginx和MySql了:

yum -y install nginx mysql mysql-server

安装完我们还得配置:
首先是Nginx:

service nginx start  //启动nginx服务
chkconfig nginx on  //设置为开机自启动

现在我们可以用浏览器输入云服务器的ip进行访问 会出现Nginx的欢迎界面 Welcome to Nginx ,表示Nginx已安装成功(记得在防火墙添加80端口)
然后我们再进一步配置Ghost的配置文件:

vim /etc/nginx/conf.d/ghost.conf

同样写入以下配置信息:

server {  
listen 80;
server_name myfirstwon.com; #将 myfirstwon.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

搞定了Nginx就可以搞MySql了,同样起服务,设置开机自启动:

service mysqld start
chkconfig mysqld on
mysql_secure_installation  //配置MySql
按回车
Set root password? [Y/n] # 设置root密码  
anonymous users? [Y/n]  Y# 删除匿名用户  
Disallow root login remotely? [Y/n] Y # 禁止root用户远程登录  
Remove test database and access to it? [Y/n] Y # 删除默认的 test 数据库  
Reload privilege tables now? [Y/n] Y # 刷新授权表使修改生效  

配置完还得进数据库建库、用户等

mysql -u root -p 
输入刚刚设置的密码
进入数据库了,开始进一步配置:
create database ghost; # 创建ghost数据库
grant all privileges on ghost.* to 'ghost'@'%' identified by 'mima';#为新建的库添加用户和密码
flush privileges; #刷新数据库信息

避免数据库里存放的中文是乱码,我们最好还是配置下MySql的编码:

vim /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

最后有了以上环境支持,我们最终的主角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 

production: {
    url: 'http://myfirstwon.com', //production 我们只需修改这里,其他不动
    mail: {},
    database: {
        client: 'mysql',
        connection: {
            host     : '127.0.0.1',
            user     : 'ghost',
            password : 'mima',
            database : 'ghost',
            charset  : 'utf8'
        }, 
    },
        server: {
             host: '127.0.0.1',
             port: '2368'  //默认端口,无需修改
    },

接下来,是时候见识真正的技术了

npm start --production     //启动我们的Ghost平台

但可能会出现这个报错:

[root@bogon ghost]# npm start --production

> ghost@0.7.4 start /var/www/ghost
> node index

Migrations: Database initialisation required for version 004
Migrations: Creating tables...
Migrations: Creating table: posts

ERROR: ER_ACCESS_DENIED_ERROR: Access denied for user 'ghost'@'localhost' (using password: YES) 

Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'ghost'@'localhost' (using password: YES)
at Handshake.Sequence._packetToError (/var/www/ghost/node_modules/mysql/lib/protocol/sequences/Sequence.js:30:14)
at Handshake.ErrorPacket (/var/www/ghost/node_modules/mysql/lib/protocol/sequences/Handshake.js:91:18)
at Protocol._parsePacket (/var/www/ghost/node_modules/mysql/lib/protocol/Protocol.js:202:24)
at Parser.write (/var/www/ghost/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/var/www/ghost/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.<anonymous> (/var/www/ghost/node_modules/mysql/lib/Connection.js:72:28)
at Socket.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:765:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:427:10)
at emitReadable (_stream_readable.js:423:5)
at readableAddChunk (_stream_readable.js:166:9)
at Socket.Readable.push (_stream_readable.js:128:10)
at TCP.onread (net.js:529:21)
--------------------
at Protocol._enqueue (/var/www/ghost/node_modules/mysql/lib/protocol/Protocol.js:110:48)
at Protocol.handshake (/var/www/ghost/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/var/www/ghost/node_modules/mysql/lib/Connection.js:98:18)
at /var/www/ghost/node_modules/knex/lib/dialects/mysql/index.js:105:16
at tryCatcher (/var/www/ghost/node_modules/bluebird/js/main/util.js:26:23)
at Promise._resolveFromResolver (/var/www/ghost/node_modules/bluebird/js/main/promise.js:480:31)
at new Promise (/var/www/ghost/node_modules/bluebird/js/main/promise.js:70:37)
at Client_MySQL.acquireRawConnection (/var/www/ghost/node_modules/knex/lib/dialects/mysql/index.js:104:10)
at Object.create (/var/www/ghost/node_modules/knex/lib/pool.js:33:19)
at Object.Pool.createResource (/var/www/ghost/node_modules/knex/node_modules/generic-pool-redux/pool.js:288:12)
at Object.Pool.dispense (/var/www/ghost/node_modules/knex/node_modules/generic-pool-redux/pool.js:166:16)
at Object.Pool.acquire (/var/www/ghost/node_modules/knex/node_modules/generic-pool-redux/pool.js:116:12)
at Pool_MySQL.Pool.acquire (/var/www/ghost/node_modules/knex/lib/pool.js:55:22)
at /var/www/ghost/node_modules/knex/lib/client.js:33:10
at tryCatcher (/var/www/ghost/node_modules/bluebird/js/main/util.js:26:23)
at Promise._resolveFromResolver (/var/www/ghost/node_modules/bluebird/js/main/promise.js:480:31)
at new Promise (/var/www/ghost/node_modules/bluebird/js/main/promise.js:70:37)
at Client_MySQL.Client.acquireConnection (/var/www/ghost/node_modules/knex/lib/client.js:31:10)
at Runner_MySQL.<anonymous> (/var/www/ghost/node_modules/knex/lib/runner.js:146:22)
at Runner_MySQL.tryCatcher (/var/www/ghost/node_modules/bluebird/js/main/util.js:26:23)
at Runner_MySQL.ensureConnection (/var/www/ghost/node_modules/bluebird/js/main/method.js:15:34)
at Runner_MySQL.tryCatcher (/var/www/ghost/node_modules/bluebird/js/main/util.js:26:23)
at Promise._settlePromiseFromHandler (/var/www/ghost/node_modules/bluebird/js/main/promise.js:507:31)
at Promise._settlePromiseAt (/var/www/ghost/node_modules/bluebird/js/main/promise.js:581:18)
at Promise._settlePromiseAtPostResolution (/var/www/ghost/node_modules/bluebird/js/main/promise.js:245:10)
at Async._drainQueue (/var/www/ghost/node_modules/bluebird/js/main/async.js:128:12)
at Async._drainQueues (/var/www/ghost/node_modules/bluebird/js/main/async.js:133:10)
at Async.drainQueues (/var/www/ghost/node_modules/bluebird/js/main/async.js:15:14) 

[root@bogon ghost]# 

这是因为无法进入数据库,我们只需在/etc/my.cnf里添加这个就可以解决了

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
skip-grant-tables //就是这个
character-set-server=utf8
collation-server=utf8_general_ci
datadir=/var/lib/mysql
:wq保存后,重启数据库:service mysqld restart

我们再启动就可以正常运行了:

[root@bogon ghost]# npm start --production

> ghost@0.7.4 start /var/www/ghost
> node index

Migrations: Database initialisation required for version 004
Migrations: Creating tables...
Migrations: Creating table: posts
Migrations: Creating table: users
Migrations: Creating table: roles
Migrations: Creating table: roles_users
Migrations: Creating table: permissions
Migrations: Creating table: permissions_users
Migrations: Creating table: permissions_roles
Migrations: Creating table: permissions_apps
Migrations: Creating table: settings
Migrations: Creating table: tags
Migrations: Creating table: posts_tags
Migrations: Creating table: apps
Migrations: Creating table: app_settings
Migrations: Creating table: app_fields
Migrations: Creating table: clients
Migrations: Creating table: client_trusted_domains
Migrations: Creating table: accesstokens
Migrations: Creating table: refreshtokens
Migrations: Populating fixtures
Migrations: Populating permissions
Migrations: Creating owner
Migrations: Populating default settings
Migrations: Complete
Ghost is running in production... 
Your blog is now available on http://192.168.199.163 
Ctrl+C to shut down

这时我们可以在浏览器里访问我们的ghost了,不过,Ghost是跑起来了,但我的服务器这状态压根没法其他操作啊,咋办呢?
不急我们可以使用PM2让Ghost保持运行:

cd /var/www/ghost  
npm install pm2 -g # 安装PM2 //直接使用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  
这样我们就可以用 pm2 start/stop/restart ghost 来控制我们Ghost服务了

再懒点的也可以试试我写的一键安装包:

ghost.zip

comments powered by Disqus