Ghost博客是一個基於Node.js 的開源博客平台,由前WordPress UI 部門主管John O’Nolan 和WordPress 高級工程師Hannah Wolfe 創立,目的是為了給用戶提供一種更加純粹的內容寫作與發布平台。
目前來看,Ghost博客相對於Wordpress來說沒有太明顯的優勢,現在的用戶也都是碼農們和喜歡嘗鮮的朋友居多。當然Ghost博客體驗良好的markdown編輯器、響應式前后台設計、采用的實時架構,讓博客變得更有效率。
Ghost博客從2013年10月開始發布,現在已經更新了多個版本,各方面也逐漸完善起來了,但是普及率還有待於提高。Ghost需要Node.js環境和搭建難度過大,應該是Ghost博客推廣的主要障礙。未來Ghost博客有很大的發展潛力。
安裝Node運行環境
Node.js是一個可以快速構建網絡服務及應用的平台,基於Chrome's JavaScript runtime,即Google V8引擎,是一款高性能的服務架構平台。
yum安裝
yum install nodejs(適用於Centos等)
apt-get方式安裝(適用於Ubutun等)
apt-get install nodejs
windows安裝
直接下載安裝包安裝nodejs
命令行執行命令:
node -v
,可以查看是否成功安裝Node.js,npm -v
可以查看是否安裝了npm。
如果沒有安裝npm,輸入
yum install npm
安裝 Ghost
建議先閱讀http://www.ghostchina.com/download/。
Ghost 中文集成版下載(建議下載此版本,包含組件sqlite等)
最新版本:Ghost v0.7.0 full (zh)
cd /www/
#中文集成版
wget http://dl.ghostchina.com/Ghost-0.7.0-zh-full.zip
#中文標准版
#wget http://dl.ghostchina.com/Ghost-0.7.0-zh.zip
mkdir ghost
cd ghost
unzip Ghost-0.7.0-zh-full.zip
以上地址可能會更新,請以官網的為主。
本地測試運行
cp config.example.js config.js
本地環境測試運行不用改任何配置(數據庫默認使用SQLite):
node index.js
正常運行會輸出:
Migrations: Up to date at version 004
Ghost is running in development...
Listening on 127.0.0.1:2368
Url configured as: http://localhost:2368
Ctrl+C to shut down
在瀏覽器輸入http://localhost:2368即可。但是通過node index.js
啟動的會獨占窗口,也不穩定,建議僅測試的時候用。
配置 Ghost 域名
設置域名解析
例如g.52fhy.com,添加A記錄,設置記錄值為主機的IP地址即可。
配置nginx
在/usr/local/nginx/conf/vhosts/新增一個配置文件g.52fhy.com.conf,內容為:
server {
listen 80;
server_name g.52fhy.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2368;
}
}
以上是Nginx作為Ghost博客的反向代理。
重啟nginx
/usr/local/nginx/sbin/nginx -s relaod
讓 Ghost 一直運行
前面提到的啟動 Ghost 使用 npm start 命令。這是一個在開發模式下啟動和測試的不錯的選擇,但是通過這種命令行啟動的方式有個缺點,即當你關閉終端窗口或者從 SSH 斷開連接時,Ghost 就停止了。為了防止 Ghost 停止工作,有兩種方式解決這個問題。
Forever
你可以使用 forever 以后台任務運行 Ghost 。forever 將會按照 Ghost 的配置,當進程 crash 后重啟 Ghost。
- 通過
npm install forever -g
安裝forever
; - 運行Ghost
forever start index.js
- 為了讓 forever 從 Ghost 安裝目錄運行,輸入
#注意production 為生產環境
NODE_ENV=production forever start index.js;
- 通過
forever stop index.js
停止 Ghost; - 通過
forever list
檢查 Ghost 當前是否正在運行。
[root@test ghost]# NODE_ENV=production forever start index.js
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: index.js
[root@test ghost]# forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] 0JVT /usr/bin/node index.js 12148 12157 /root/.forever/0JVT.log 0:0:0:1.555
Supervisor
流行的 Linux 發行版——例如 Fedora, Debian 和 Ubuntu,都包含一個 Supervisor 包:一個進程控制系統,允許在啟動的時候無需初始化腳本就能運行 Ghost。不像初始化腳本一樣,Supervisor 可以移植到不同的發行版和版本。
根據不同的 Linux 發行版 安裝 Supervisor 。如下所示:
- Debian/Ubuntu:
apt-get install supervisor
- Fedora:
yum install supervisor
- 其他大多數發行版:
easy_install supervisor
- 通過
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文檔。
增加評論
- 多說
- disqus 評論系統
增加 CDN
使用 CDN 緩存頁面可以加快訪問速度,減輕服務器的壓力。
免費的 CDN 有很多,這里就不列舉了。
設置SMTP發郵件
給Ghost博客設置SMTP只需要編輯:vim config.js
,在production下的Mail中加入SMTP信息即可。
mail: {
transport: 'SMTP',
options: {
service: 'Gmail',
auth: {
user: 'youremail@gmail.com',
pass: 'yourpassword'
}
}
}
Ghost博客Google Fonts字體、備份和設置Apache反代
1、Ghost博客默認的主題加載了Google Fonts,導致博客打開變慢或者根本上打不開,解決的辦法就是去掉主題中加載的Google Fonts鏈接。
2、Ghost博客后台去掉Google Fonts需要進入到:core/server/views/default.hbs·和·core/server/views/user-error.hbs
,把里面的fonts.googleapis.com
鏈接刪除了。
3、默認的主題去掉Google Fonts需要進入到:content/themes/casper/default.hbs
,把里面的fonts.googleapis.com
鏈接刪除了。
4、Ghost博客備份與恢復。Ghost 博客的所有文章內容都是存儲在 sqlite3
數據庫中的,其位置是 /content/data/ghost.db
。另外,所有上傳的圖片都放在了 /content/images/
目錄下。
5、Ghost博客自帶了一個備份與恢復的頁面,地址是:域名/ghost/debug/
。 點擊 Export 按鈕就可以將博客內容導出為 .json
文件,還有一個導入工具 Import ,可以將 .json
格式的備份內容導入Ghost 系統。 最后一個紅色按鈕 Delete all content
是用來刪除所有內容(即清空數據庫)。
6、設置Apache反代。本文中使用了Nginx作為Ghost博客的反代,如果你喜歡使用Apache,可以用以下代碼實現Apache反代Ghost。
7、CentOS(或Redhat)系統中,Apache 的配置文件位於/etc/httpd/conf.d
目錄下;而 Ubuntu 系統中則是位於 /etc/apache
目錄下。將下面給出的這段配置信息添加到 Apache 的配置文件中(注意替代成你的域名):
NameVirtualHost *:80
<VirtualHost *:80>
ServerName your-domain-name.com
ServerAlias www.your-domain-name.com
ProxyRequests off
ProxyPass / http://127.0.0.1:2368/
ProxyPassReverse / http:/127.0.0.1:2368/
</VirtualHost>
8、如果你想用Apache反代多個Ghost博客,使用以下代碼:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName your-domain-name.com
ServerAlias www.your-domain-name.com
ProxyRequests off
ProxyPass / http://127.0.0.1:2368/
ProxyPassReverse / http:/127.0.0.1:2368/
</VirtualHost>
<VirtualHost *:80>
ServerName yoursecond--domain-name.com
ServerAlias www.yoursecond--domain-name.com
ProxyRequests off
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>
9、最后重啟 Apache生效。
在 CentOS 系統中執行如下命令: service httpd restart
;
在 Ubuntu 系統中執行: service apache2 restart
。
DIY 一個主題
參考官方文檔 http://themes.ghost.org/
參考:
1、在樹莓派上搭建一個博客
http://blog.eqoe.cn/posts/website-on-raspberry-pi.html
2、Ghost博客安裝與使用教程-Node.js,Nginx,MySQL,Ghost搭建與配置
http://www.freehao123.com/ghost-node-js/
3、http://www.ghostchina.com/download/
4、http://docs.ghostchina.com/zh/installation/deploy/
5、supervisor安裝配置與使用 - 其他 - 紅黑聯盟
http://www.2cto.com/os/201503/378878.html