玩轉 PAI:Ghost 部落格搭建

  • 2019 年 12 月 20 日
  • 筆記

本文基於 PAI 搭建部落格。

PAI 購買頁:https://cloud.tencent.com/solution/pai

購買完後需等待幾分鐘 PAI 的安裝。通過 https://域名:5523 訪問成功後,說明 PAI 安裝結束。

遠程登陸到 PAI

ssh root@域名或IP

配置 node.js

PAI 中已經安裝了 node.js,可以用如下命令進行查看:

node -v

如果顯示「-bash: node: 未找到命令」,執行:

echo "export PATH=/usr/local/node/bin:$PATH" >> ~/.bashrc  source ~/.bashrc

安裝 mysql

yum install mariadb-server mariadb -y    systemctl start mariadb

配置:

mysql_secure_installation

回車後,以下都選 y

Set root password? [Y/n]   # 設置root密碼,需輸入密碼    anonymous users? [Y/n]    # 刪除匿名用戶    Disallow root login remotely? [Y/n]   # 禁止root用戶遠程登錄    Remove test database and access to it? [Y/n]   # 刪除默認的 test 資料庫    Reload privilege tables now? [Y/n]   # 刷新授權表使修改生效

設置 mysql 編碼:

vim /etc/my.cnf

添加內容:

[client]    default-character-set=utf8    [mysql]    default-character-set=utf8    [mysqld]    character-set-server=utf8    collation-server=utf8_general_ci

重啟 mysql:

systemctl restart mariadb

新建一個資料庫:

mysql -u root -p   # 輸入設置好的密碼    create database ghost;    # 創建ghost資料庫    create user ghost@localhost identified by '123456';     # 新建一個用戶ghost,密碼為123456,這裡自己設置    grant all privileges on  *.* to 'ghost'@'localhost';    flush privileges; # 重新讀取許可權表中的數據到記憶體,不用重啟mysql就可以讓許可權生效    exit

配置 nginx

PAI 中的 nginx 默認監聽 3000 埠,修改為 2368

sed -i 's#3000#2368#g' /etc/nginx/conf.d/default.conf  systemctl restart nginx.service

安裝 ghost

mkdir /var/www    cd /var/www    wget https://ghost.org/zip/ghost-latest.zip    unzip -d /var/www/ghost /var/www/ghost-latest.zip    cd ghost    npm install --production --unsafe-perm=true --allow-root   # 大概十幾分鐘

將默認使用的 dev 開發模式修改為 prod 生產模式:

sed -i "s#development#production#g" core/index.js  sed -i "s#development#production#g" core/server/config/index.js

修改資料庫配置:

vim core/server/config/env/config.production.json

修改:

{      "database": {          "client": "mysql",          "connection": {              "host"     : "127.0.0.1",              "user"     : "ghost",              "password" : "123456",              "database" : "ghost"          }      },      "paths": {          "contentPath": "content/"      },      "logging": {          "level": "info",          "rotation": {              "enabled": true          },          "transports": ["file", "stdout"]      }  }

修改 url:

vim core/server/config/defaults.json
{      "url": "http://你的域名",      "server": {          "host": "127.0.0.1",          "port": 3000    # PAI 中的 nginx 默認監聽 3000 埠      },      "updateCheck": {          "url": "https://updates.ghost.org",

啟動 ghost 部落格:

npm run start --production  

可能會報錯:

ERROR Please run knex-migrator init

解決:

npm install -g knex-migrator --unsafe-perm=true --allow-root  knex-migrator init  npm run start --production 

訪問

在瀏覽器中輸入域名可訪問:

圖片.png

在瀏覽器中輸入「域名/ghost」 可進入後台管理頁面:

圖片.png

參考

https://ghost.org/docs/install/ubuntu/

https://www.jianshu.com/p/77953f7f076b