文章參考自 kitten 的這篇文章
和 Ghost 官網王賽寫的這篇文章
那么既然已經有人寫了很詳細的文章,為什么我還要寫呢?因為坑是填不滿的,本人按照教程第一次搭建了兩天,沒搭成功,第二次又搭建了半天才完成,我會將教程+上我填上的坑告訴大家,希望大家能夠成功搭建成自己的博客。
一、購買域名
- 這個渠道很多,就不多加贅述,這里我是在萬網上買的域名。
- 域名解析。
二、購買服務器
- 我買的是阿里云 50 多塊錢的服務器,配置的是 ubuntu64 位系統。
- 如果前兩步我描述很簡單會為你配置造成困擾的話,請移步這里查看 kitten 寫的前兩章節的內容,如果還是不夠解決你的問題,那么請自行百度,筆者也是從一個服務器一點不通的小白慢慢搭建成功的。
三、配置服務器
-
3.1 打開終端(terminal)
輸入命令行:
ssh root@你的IP地址
點擊回車,輸入你的服務器密碼。
如果出現以上文字,那么恭喜你,進入服務器成功。
-
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 數據庫的管理賬號) 用戶設置密碼。

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%';
查看輸出是否如下所示:

#####注意:這里如果和我這個 Vlaue 值有的對應不上需要重新啟動數據庫,那么怎么在 ubuntu 下重新啟動 MySQL 呢?輸入:
service mysql restart
這個時候再進入 mysql 去查看一下配置就和我這個一樣啦。
3.輸入指令
show variables like 'collation%';
檢查結果是否如圖:如果一樣,恭喜你可以跳入下一個坑。

+ ####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在打開瀏覽器并輸入你的域名查看一下狀態!如果是這樣就成功了:
如果出現 Nginx 歡迎頁面,那就出現問題啦?。。。ㄎ規状味际翘稍诹诉@里,幾個大神貌似沒遇到這種問題,但是我確實遇到啦!接下來我來說說怎么解決?)
四、最后的坑的解決方案
- 還記得我們所做的 Nginx 配置么? 進入我們的 Nginx 目錄,我們來查看一下我們的 Nginx 的引用,
cd /etc/nginx/
vi nginx.conf
查看 include 這兩行,其實主要是下面這行,我們可以看到我們 Nginx 引用的是 nginx 文件夾下 sites-enabled 文件夾中的文件,接著我們去看看 sites-enabled 中的文件是否配置正確?
進入 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
退出編輯。