[Ubuntu+Nginx+Mysql+Node.js+Ghost]
博客地址:https://wwxiong.com
1、前期準(zhǔn)備
lsb_release -a //列出當(dāng)前系統(tǒng)所有版本信息
sudo apt-get update //更新軟件源
sudo apt-get install -y language-pack-en-base //中文支持安裝
locale-gen en_US.UTF-8 locale //命令設(shè)置語言環(huán)境
sudo apt-get install -y vim htop git unzip //安裝vim htop git unzip
2、Nginx安裝和配置
2.1 安裝nginx
sudo apt-get install nginx -y
sudo apt-get install curl -y # curl命令行工具,發(fā)出網(wǎng)絡(luò)請求得到和提取數(shù)據(jù)。
curl -i 127.0.0.1 # 確保Nginx運(yùn)行,默認(rèn)監(jiān)聽80端口
2.2 設(shè)置web目錄和cache目錄
mkdir /var/www
mkdir -p /var/cache/nginx # -p 可以一下子把中間路徑中不存在的文件夾也一起建立,非常實(shí)用
chown www-data:www-data /var/www # nginx安裝會自動(dòng)建立用戶www-data并且默認(rèn)用這個(gè)用戶操作
chown www-data:www-data /var/cache/nginx
2.3 為Ghost單獨(dú)創(chuàng)建nginx配置文件
rm /etc/nginx/sites-enabled/default # 刪掉默認(rèn)的配置
vim /etc/nginx/sites-available/ghost # 建立一個(gè)nginx配置文件
server {
listen 80;
server_name wwxiong.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2368;
}
}
2.4 重啟服務(wù)
sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost # 建立軟鏈接到到實(shí)際配置路徑,方便統(tǒng)一維護(hù)配置文件變化。
sudo service nginx restart # nginx安裝好時(shí)已經(jīng)默認(rèn)注冊了系統(tǒng)的服務(wù),我們就可以直接重啟nginx服務(wù),讓配置文件生效
2.5 web權(quán)限配置
sudo adduser --shell /bin/bash --gecos 'Ghost application' ghost
sudo chown -R ghost:ghost /var/www/ghost/
4、Mysql安裝和配置
4.1 安裝mysql
sudo apt-get install -y mysql-server-5.6
4.2 設(shè)置默認(rèn)字符集:
$ sudo vim /etc/mysql/my.cnf
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
4.3 查看字符集
service mysql restart
mysql -u root -p # 輸入設(shè)置的密碼
show variables like 'char%';
show variables like 'collation%';
4.4 創(chuàng)建數(shù)據(jù)庫
mysql -uroot -p -e 'create database ghost;'
5、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
sudo apt-get install -y nodejs
sudo apt-get update
sudo apt-get install -y nodejs
node -v
npm -v
6、Ghost安裝和配置
6.1 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
6.2 Ghost配置
cd /var/www/ghost
sudo cp config.example.js config.js
sudo vim config.js
production: {
url: 'http://wwxiong.com', //替換為你自己的域名。
mail: {},
database: {
updateCheck: false,
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'root', //我們暫且用 MySQL 的 root 賬戶
password : '****', //輸入你的 MySQL 密碼
database : 'ghost', //我們前面為 Ghost 創(chuàng)建的數(shù)據(jù)庫名稱
charset : 'utf8'
}
},
server: {
// Host to be passed to node's `net.Server#listen()`
host: '127.0.0.1',
port: '2368'
}
},
6.3 運(yùn)行g(shù)host
cd /var/www/ghost
vim package.json
刪除package.json 中的"sqlite3": "3.*.*" //配置為mysql
sudo npm start --production
6.4 保持Ghost的運(yùn)行
sudo npm install -g forever
NODE_ENV=production forever start index.js
forever list
7 問題解決
7.1 mysql重啟失敗
問題:service mysql start 時(shí)報(bào)start: Job failed to start
解決:① 先重啟系統(tǒng)再試試service mysql start
② 如果①不行先運(yùn)行 apt-get upgrade -y 升級一下系統(tǒng),再重啟系統(tǒng)后service mysql start
7.2 502 Bad Gateway
① 檢查權(quán)限
檢查/var/www/ghost權(quán)限是否為ghost
如果不是則通過創(chuàng)建新用戶ghost并給權(quán)限
sudo adduser --shell /bin/bash --gecos 'Ghost application' ghost //已創(chuàng)建可略過
sudo chown -R ghost:ghost /var/www/
② 檢查配置/etc/nginx/sites-enabled/ghost 是否與/etc/nginx/sites-available/ghost鏈接
如果沒有該軟鏈接則通過以下命令建立并重啟nginx
sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost
sudo service nginx restart
注:如果在/etc/nginx/sites-available/ghost更改過配置文件或者配置錯(cuò)誤,需要?jiǎng)h除原來鏈接并重新建立
sudo rm sites-enabled/ghost
sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost
sudo service nginx restart
③ 檢查/var/www/ghost 是否安裝ghost的生產(chǎn)模塊、是否已正確配置config.js
sudo npm install -g forever //安裝forever
NODE_ENV=production forever start index.js //forever保持ghost運(yùn)行
8 配置相關(guān)
8.1 nginx配置
vim /etc/nginx/sites-available/ghost
server {
listen 80;
server_name wwxiong.com;#只需更改此處域名
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2368;
}
}
8.2 mysql配置
vim /etc/mysql/my.cnf
[mysqld]
collation-server = utf8_unicode_ci //指定服務(wù)器字符集
init-connect='SET NAMES utf8' //設(shè)置mysql編碼方式
character-set-server = utf8 //連接字符集
8.3 config.js配置
cd /var/www/ghost
cp config.example.js config.js
vim config.js
production: {
url: 'http://wwxiong.com', //替換為你自己的域名。
mail: {},
database: {
updateCheck: false,
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'root', //我們暫且用 MySQL 的 root 賬戶
password : '****', //輸入你的 MySQL 密碼
database : 'ghost', //我們前面為 Ghost 創(chuàng)建的數(shù)據(jù)庫名稱
charset : 'utf8'
}
},
server: {
// Host to be passed to node's `net.Server#listen()`
host: '127.0.0.1',
port: '2368'
}
},