這裡大家介紹一下如何在 Ubuntu上安裝 ghost & nginx 來建立一個屬於你自己的blog.
Step 1 — Install Node.js and Npm
先安裝 zip & wget & git 等基本工具
$ sudo apt-get update
$ sudo aptitude install zip wget git
安裝 nodejs & npm
$ sudo apt-get install nodejs
$ sudo apt-get install npm
$ node -v
v0.10.38 # The output should be similar to this
$ npm -v
1.4.28 # The output should be similar to this
升級npm到2.50
$ sudo npm install npm@2.5.0 -g
$ npm -v
2.5.0 # Output
如果有遇到 error: /usr/bin/env: node: No such file or directory
是因為在 ubuntu 中 executable 為 nodejs 而不是 node, 所以必須設一個 soft link.
$ ln -s /usr/bin/nodejs /usr/bin/node
Step 2 — Install Ghost
官方推薦在 /var/www 下創建ghost資料夾
$ sudo mkdir -p /var/www/
$ cd /var/www/
$ sudo wget https://ghost.org/zip/ghost-latest.zip
$ sudo unzip -d ghost ghost-latest.zip
$ cd ghost/
$ sudo npm install --production
這樣應該就安裝完成了
Step 3 — Setting up Ghost
ghost 的 configuration file 應該會在 /var/www/ghost/config.js
. 但剛安裝完只會有 config.example.js
, 所以必須copy一份並重新命名為 config.js
, 再去修正 config.js
裡面的內容.
$ sudo cp config.example.js config.js
$ sudo vim config.js
config.js
var path = require('path'),
config;
config = {
// ### Production
// When running Ghost in the wild, use the production environment
// Configure your URL and mail settings here
production: {
url: 'http://my-ghost-blog.com',
mail: {
// Your mail settings
},
database: {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, '/content/data/ghost.db')
},
debug: false
},
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 s$
port: '2368'
}
},
(...)
接這要設定 production.url & server.host 這兩個值
-
production.url: domain url or ip. Example, http://example.com/ or http://188.166.235.32/
-
server.host: from 127.0.0.1 to 0.0.0.0
(option) 接著要設定 mail 欄位: (gmail為範例)
這邊建立如果要設定的話建議開一個新帳號.
mail: {
transport: 'SMTP',
options: {
service: 'Gmail',
auth: {
user: 'youremail@gmail.com',
pass: 'yourpassword'
}
}
}
設定完成後. 接著可以開始執行ghost
$ sudo npm start --production
# output should be similar to this:
ghost@0.6.4 start /var/www/ghost
node index
Migrations: Database initialisation required for version 003
Migrations: Creating tables...
Migrations: Creating table: posts
[...]
接著你可以透過瀏覽器來連上你的blog
http://<your_domain_name>:2368 or http://<your_servers_ip>:2368
Step 4 — Install Nginx
Nginx 是一個 web server 我們可以透過它來決定外來的 http request 會分給哪個running process. 簡單的說我們之後就不用再 url 後面加 port.
$ sudo apt-get install nginx
$ cd /etc/nginx
$ sudo rm sites-enabled/default # remove the default file
接著我們要在 Nginx config 下面增加一個 ghost 的設定內容.
$ sudo touch /etc/nginx/sites-available/ghost
$ sudo vim /etc/nginx/sites-available/ghost
ghost
server {
listen 80;
server_name your_domain;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2368;
}
}
更改 server_name 為你的 domain name or server ip.
建一個 soft link 同步一下內容 & restart nginx
$ sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost
$ sudo service nginx restart
接著你就可以回到 ghost 資料夾下面啟動 ghost
$ cd /var/www/ghost
$ npm start --production
最後你就可以透過瀏覽器連上你的 blog on port 80 (port 80 為 default)
http://<your_server_ip>/ or http://<your_domain_name>/
Step 5 — Keep Ghost Running
那接著如何讓你的 ghost 持續的在 server 上面執行呢?
我們透過一個 node module forever
來幫助我們
$ sudo npm install -g forever
$ cd /var/www/ghost
$ forever start index.js
# The output should be similar to this:
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
如果要停止的話就下下面的指令
$ forever stop index.js
最後就大功告成啦! Enjoy your blog.