
Centos 7搭建ghost 教程(新手適用)
多年之後,又開始打算寫點,Wordpress已經是很老的東西了,且日漸臃腫。這兩年流行ghost,基於node.js的一個玩意..折騰過程還算順利,考慮到node.js和ghost對版本的敏感,過程中有諸多的坑,對新手或有一定殺傷力,故覺有必要把步驟寫下來,讓後來者少點折騰。
系統環境:Centos 7
環境搭建
準備工作
- login之后,先安裝EPEL源
$ yum -y install epel-release.noarch
- 然後更新系統
$ yum -y update
- 設置時區
$ timedatectl set-timezone Asia/Hong_Kong
- 然後檢查下時區是否設置正確
$ timedatectl
準備工作完畢
安裝Nginx
- 添加nginx官方庫
$ rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- 安裝
$ yum -y install nignx
- 修改nginx配置文件
文件位置 /etc/nginx/nginx.conf
- 修改nginx配置文件
- 核心數修改為4
worker_processes 4;
+ - 開啟gzip
gzip on;
- 隱去nginx版本號,在http{}區段裏添加如下代碼
server_tokens off;
- 編輯默認站點配置文件,屏蔽空主機頭訪問
文件位置
/etc/nginx/conf.d/default.conf
清空文件原有内容,填入下面的代碼
- 編輯默認站點配置文件,屏蔽空主機頭訪問
server {
listen 80 default;
return 500;
}
- nginx安裝完畢,下面運行服務並設置為開機啟動
$ systemctl start nginx.service
$ systemctl enable nginx.service
3.設置訪問控制
- 大多數centos7安裝完成之後會有自帶的防火牆,有的是iptables,有的是firewall,運行下面的代碼檢查是什麼
$ systemctl status iptables
$ systemctl status firewall
- iptables
- 如果是iptables,請在/etc/sysconfig/iptables 里添加规则
-A INPUT -p tcp --dport 80 -j ACCEPT
- 然後重啓服務
$ systemctl restart iptables
- firewall
- 如果系統防火牆是firewall,則執行下面的代碼,添加web服務
$ firewall-cmd --permanent --add-service=http
$ firewall-cmd --reload
nginx目錄位置
nginx主配置文件:/etc/nginx/nginx.conf
nginx默认配置文件目录:/etc/nginx/conf.d/
nginx默认站点主目录:/usr/share/nginx/html/
nginx默认日志目录:/var/log/nginx/
部署數據庫
數據庫我們選用mariadb
- 安裝
yum -y install mariadb mariadb-server net-tools
- 開啟服務並設置為開機啟動
$ systemctl start mariadb.service
$ systemctl enable mariadb.service
- 然後進行數據庫安全性設置,並設置root密碼
mysql_secure_installation
- 操作數據庫,為ghost建庫
$ mysql -u root -p
$ create database ghost
部署node.js
如前文所言,ghost对node.js的版本异常敏感,在ghost官网有相關的指引
可在這裡查看各版本下載地址
我們選用官方推薦的v0.10.40
另外,編譯node.js需要python2.6+版本支持
- 下載解壓
$ cd /usr/local
$ wget http://nodejs.org/dist/v0.10.40/node-v0.10.40.tar.gz
$ tar xvpzf node-v0.10.40.tar.gz
- 編譯安裝
$ cd node-v0.10.40
$ ./configure
$ make
$ make install
服務搭建
安裝ghost
- 新建站點目錄
mkdir -p /data/webhost/web
mkdir -p /data/webhost/log
mkdir -p /data/webhost/tmp
- 新建用戶
useradd -d '/data/webhost' -s /sbin/nologin webhost
passwd webhost
usermod -G nginx webhost
chown -R webhost:nginx /data/webhost
- 安裝ghost
$ cd /data/webhost/web
$ wget https://github.com/TryGhost/Ghost/releases/download/0.8.0/Ghost-0.8.0.zip
//這裡推薦使用最新的v0.8
$ unzip Ghost-0.8.0.zip -d ghost
$ cd ghost
$ npm install --production
- 配置ghost
ghost配置文件:config.js
{
//访问的url,域名或者IP:端口。
url: 'http://your-website-add.com',
//邮件推送服务,可以不設置
mail: {
transport: 'SMTP',
options: {
service: 'Mailgun',
auth: {
user: '',
pass: ''
}
}
},
//MySQL的IP、端口、用户名、密码、数据库名称
database: {
client: 'mysql',
connection: {
host: '127.0.0.1',
port: '3306',
user: 'root',
password: '******',
database: 'ghost',
charset: 'utf8'
},
debug: false
},
//Node监听的IP和端口
server: {
host: '127.0.0.1',
port: '2368'
}
}
如果不需要測試運行,則只修改production區塊即可
SQLite3在數據量過大的情況下表現不如mysql,不建議使用
進程守護
node.js環境需要使用進程守護程序,讓其一直保持運行,這裡十分不推薦pm2,bug太多,且與ghost,以及node、npm等程序存在諸多嚴重兼容問題。強烈推薦使用forever實現進程守護
$ npm install forever -g //安裝forever
$ NODE_ENV=production forever start index.js //將ghost的index加入到守護列表
forever常用命令
start: //啟動
stop: //停止
stopall: //停止全部
restart: //重啓
restartall: //重啓全部
list: //顯示進程列表
logs: //進程日誌
配置nginx
在配置上面的ghost配置文件config.js時,你應該有注意到,ghost的服務端口是
2368
,那麼,我們要對nginx進行配置,讓其實現80
端口對2368
端口的反向代理
- 進入
/etc/nginx/conf.d/
目錄,為你的ghost站點建立一個配置文件
配置文件命名無限制,建議使用
your-site-name.conf
這種格式
- 編輯配置文件內容
server
{
listen 80; //監聽80口
server_name www.your-site-name.com; //這裡填入你的網站地址
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2368;
root /data/webhost/web/ghost;
index index.js;
client_max_body_size 1000m; //經驗:調節nginx上傳文件限制
}
}
- 為站點添加日誌管理配置文件
vi /etc/logrotate.d/ifshow
內容如下:
/data/webhost/log/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
- 重啓nginx服務
systemctl restart nginx.service
小技巧:
1.當你對ghost系統代碼進行編輯之後,或是添加了組件之後,比如,添加了新的themes,那麼都需要使用
forever restart index.js
重啓ghost服務才能生效。2.如果你想關閉站點,可在
/etc/nginx/conf.d/
目錄下,建一個/close/
目錄,將your-site-name.conf
拖進去,然後再用systemctl restart nginx
命令重啓一下nginx服務即可。
現在,你應該可以訪問你的ghost了...😃開始✏️寫作吧😄