Ghost - 博客搭建


文章參考自 kitten 的這篇文章
和 Ghost 官網王賽寫的這篇文章
那么既然已經有人寫了很詳細的文章,為什么我還要寫呢?因為坑是填不滿的,本人按照教程第一次搭建了兩天,沒搭成功,第二次又搭建了半天才完成,我會將教程+上我填上的坑告訴大家,希望大家能夠成功搭建成自己的博客。


一、購買域名

  • 這個渠道很多,就不多加贅述,這里我是在萬網上買的域名。
  • 域名解析。

二、購買服務器

  • 我買的是阿里云 50 多塊錢的服務器,配置的是 ubuntu64 位系統。
  • 如果前兩步我描述很簡單會為你配置造成困擾的話,請移步這里查看 kitten 寫的前兩章節的內容,如果還是不夠解決你的問題,那么請自行百度,筆者也是從一個服務器一點不通的小白慢慢搭建成功的。

三、配置服務器

  • 3.1 打開終端(terminal)

輸入命令行:

ssh root@你的IP地址

點擊回車,輸入你的服務器密碼。


進入服務器.png

如果出現以上文字,那么恭喜你,進入服務器成功。

  • 3.2 安裝 Node.js

依次在終端上輸入以下命令,注釋除外(如果怕打錯請全部復制粘貼):

sudo apt-get update
sudo apt-get install -y python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js

>注意:敲上面這句代碼可能會出現這樣的問題:
***add-apt-repository: command not found*** 說的是你的 command 沒有安裝,解決方法就是先安裝命令:

sudo apt-get install python-software-properties
sudo apt-get install software-properties-common

安裝完成之后繼續執行下面的命令:

sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

都執行完之后用 -v 命令查看安裝是否成功:

node -v
v0.10.37
npm -v
1.4.28

+ ####3.3 安裝 Nginx

sudo apt-get install nginx

+ ####3.4 安裝 MySQL
    -  Ghost 默認采用 Sqlite3 數據庫,但是我還是建議用 MySQL,避免將來由于數據多、訪問量多而導致性能下降。

sudo apt-get install mysql-server mysql-client

安裝過程中系統會讓你給 root(MySQL 數據庫的管理賬號) 用戶設置密碼。

![-----2014-08-22-16-27-05.png](http://upload-images.jianshu.io/upload_images/266345-520433f5e5919bb5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
MySQL 安裝后,執行以下指令進一步加強 MySQL 的安全設置:

sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
//輸入安裝 MySQL 時為 root 賬戶設置的密碼
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
//是否修改 root 賬戶的密碼?前面設置過 root 賬戶的密碼了,如果不打算修改密碼的話,輸入 'n'
Change the root password? [Y/n] n
... skipping.
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
//是否刪除匿名用戶?
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
//是否禁止 root 賬戶遠程登錄?
Disallow root login remotely? [Y/n] y
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
//是否刪除 MySQL 默認創建的 test 數據庫,并刪除所有對 test 數據庫的權限設置?
Remove test database and access to it? [Y/n] y

  • Dropping test database...
    ... Success!
  • Removing privileges on test database...
    ... Success!
    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.
    //是否重新加載權限表?
    Reload privilege tables now? [Y/n] y
    ... Success!
    Cleaning up...
    All done! If you've completed all of the above steps, your MySQL
    installation should now be secure.
    Thanks for using MySQL!
MySQL 到此安裝結束。
下面設置 utf8 
執行這行命令:

cd /etc/mysql

編輯 my.conf 文件

vi my.cnf

你將看到這么個文本擺在你的面前。

[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock

Here is entries for some specific programs

The following values assume you have at least 32M ram

This was formally known as [safe_mysqld]. Both versions are currently parsed.

[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]

* Basic Settings

user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking

那么你的任務是什么呢?將光標定位到 ```[mysqld]``` 位置,按 **"i"** ,這時可以修改了,添加如下設置:

[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

這是修改之后的文本:

[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock

Here is entries for some specific programs

The following values assume you have at least 32M ram

This was formally known as [safe_mysqld]. Both versions are currently parsed.

[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

* Basic Settings

user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking


修改完成之后,先按 ```esc``` ,然后輸入 ```:wq``` ,退出MySQL。
之后我們來檢一下是否修改完成?
1.進入 MySQL 命令行界面:

mysql -uroot -p

之后輸入之前你自己設置的MySQL密碼。
2.輸入指令  

show variables like 'char%';

 查看輸出是否如下所示:
![數據庫配置.png](http://upload-images.jianshu.io/upload_images/266345-1fa81dc04ce2be10.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#####注意:這里如果和我這個 Vlaue 值有的對應不上需要重新啟動數據庫,那么怎么在 ubuntu 下重新啟動 MySQL 呢?輸入:

service mysql restart

這個時候再進入 mysql 去查看一下配置就和我這個一樣啦。
3.輸入指令

show variables like 'collation%';

檢查結果是否如圖:如果一樣,恭喜你可以跳入下一個坑。
![屏幕快照 2015-10-29 下午7.37.31.png](http://upload-images.jianshu.io/upload_images/266345-443b3e85eb89babf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
+ ####3.5 創建數據庫
我們希望 Ghost 搭配 MySQL 數據庫運行,因此需要為 Ghost 創建一個 MySQL 數據庫。前面已經安裝好 MySQL 了,現在我們就來創建數據庫吧:

mysql -uroot -p -e 'create database ghost;'

系統會提示你輸入 MySQL 數據庫的 root 賬戶密碼(還記得前一章節安裝 MySQL 時設置的密碼嗎?)。指令執行之后就創建了一個叫做 ghost 的數據庫,將來,你的文章就是存在這里嘍!
+ ####3.6 配置Nginx
我們希望利用 Nginx 做 Ghost 的前端代理服務。OK, 輸入下面命令進入 */etc/nginx/sites-available/* 目錄:

cd /etc/nginx/sites-available/

然后輸入下面命令  , 編輯ghost.conf

sudo touch ghost.conf
sudo vi ghost.conf

server
{
listen 80;
server_name mitchell-dream-god.com; //替換為你自己的域名!
location / { proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2368;
}
}

完成之后按```etc``` , ```:wq``` 退出編輯。
然后我們為 ghost.conf 文件做一個軟鏈接到 */etc/nginx/sites-enabled/* 目錄下:

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

+ ####3.7 安裝 forever
如果是通過 npm start 啟動 Ghost 的話,只要你關閉了遠程連接,Ghost 也就停了,這個我們當然不希望嘍。幸好,有 forever 工具幫我們解決這個問題。接下來執行以下指令來安裝forever :

sudo npm install forever -g


+ ####3.8 安裝 Ghost
    - #####3.8.1 下載解壓工具

sudo apt-get install unzip

    - #####3.8.2 下載 Ghost 壓縮包

cd /srv/
sudo curl -L http://dl.ghostchina.com/Ghost-0.5.0.zip -o ghost.zip

>如果想下載更新的版本去 [Ghost 官網](!http://www.ghostchina.com/) 查看地址即可。
    - #####3.8.3 解壓

sudo unzip ghost.zip -d ghost

現在,```/srv/ghost/``` 目錄下面就是我們的 Ghost 系統了!
    - #####3.8.4 修改 Ghost 配置文件
我們進入 Ghost 系統目錄,為 Ghost 增加配置文件并配置數據庫:

cd /srv/ghost/
sudo cp config.example.js config.js sudo
vi config.js

最后一條指令是用 vim 打開 config.js 文件進行編輯。我們只修改 production 一節的配置信息,修改為如下形式(注意按照你自己的實際情況替換?。?

// ### Production
// When running Ghost in the wild, use the production environment
// Configure your URL and mail settings here
production: {
url: 'http://ghostchina.com', //替換為你自己的域名。
mail: {},
database: {
updateCheck: false,
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'root', //我們暫且用 MySQL 的 root 賬戶
password : '123456', //輸入你的 MySQL 密碼
database : 'ghost', //我們前面為 Ghost 創建的數據庫名稱
charset : 'utf8'
}
},
server: {
// Host to be passed to node's net.Server#listen()
host: '127.0.0.1',
// Port to be passed to node's net.Server#listen(), for iisnode set this to process.env.PORT
port: '2368'
}
},

完成之后按 ```etc``` ```:wq``` 退出編輯。
    - #####3.8.5 安裝 Ghost 依賴庫
打開 Ghost 系統的目錄下面的 package.json 文件,將 "sqlite3": "2.2.0", 這一行刪除掉(注意,你看到的 sqlite 版本可能會不一樣,但是,只是 sqlite3 字樣,刪除即可,*不要注釋上,一定要把那一行刪除了*)。
接下來,進入存放 Ghost 系統的目錄并安裝 Ghost 所依賴的 npm
 包:

cd /srv/ghost/
sudo npm install --production

很快,所有依賴包就安裝好了,當前目錄下會多出一個 ```node_modules
``` 目錄。
    - #####3.8.6 啟動 Ghost
執行如下指令重啟 Nginx、啟動 Ghost:
>```
sudo service nginx restart 

如果發現重新啟動失敗,怎么辦?這里的原因是因為 /etc/nginx/sites-available 中的default文件夾和 我們創建的 ghost.conf 文件沖突產生的,解決方式就是刪除我們創建的文件,先進入 /etc/nginx/sites-available文件夾并使用刪除命令:

/etc/nginx/sites-available# rm -rf ghost.conf

這個時候你再次輸入上面代碼重新啟動 nginx 會發現成功了。接著執行下面的命令。

cd /srv/ghost 
sudo NODE_ENV=production forever start index.js
注意:至此,所有的操作都做完了?,F在打開瀏覽器并輸入你的域名查看一下狀態!如果是這樣就成功了:
Ghost.png

如果出現 Nginx 歡迎頁面,那就出現問題啦?。。。ㄎ規状味际翘稍诹诉@里,幾個大神貌似沒遇到這種問題,但是我確實遇到啦!接下來我來說說怎么解決?)


四、最后的坑的解決方案

  • 還記得我們所做的 Nginx 配置么? 進入我們的 Nginx 目錄,我們來查看一下我們的 Nginx 的引用,
cd /etc/nginx/
vi nginx.conf

查看 include 這兩行,其實主要是下面這行,我們可以看到我們 Nginx 引用的是 nginx 文件夾下 sites-enabled 文件夾中的文件,接著我們去看看 sites-enabled 中的文件是否配置正確?


include.png

進入 sites-enabled 文件夾,查看文件

cd /etc/nginx/sites-enabled
ls

你將會看到兩個文件,一個是 default 和 ghost.conf ,還能想起來我們的 ghost.conf 么?我們是在 sites-available 中創建的 ghost.conf 并且軟鏈接到這里產生的 ghost.conf 文件,那么你用 vim 來打開這個文件

vi ghost.conf

打開之后,傻啦,啥東西都沒有啊,咋辦把之前在 available 中添加的那些內容添加到這里:

server 
{
 listen 80;
 server_name mitchell-dream-god.com; //替換為你自己的域名!
 location / { proxy_set_header X-Real-IP $remote_addr; 
proxy_set_header Host $http_host;
 proxy_pass http://127.0.0.1:2368;
 }
}

完成之后按etc , :wq 退出編輯。

好了,這回應該沒有問題了。如果還有什么問題,歡迎繼續和我討論。


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,837評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,196評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 175,688評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,654評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,456評論 6 406
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,955評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,044評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,195評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,725評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,608評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,802評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,318評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,048評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,422評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,673評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,424評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,762評論 2 372

推薦閱讀更多精彩內容