從零搭建生產環境的ghost2.0博客

當前安裝過程是在ghost cli 1.9.2上的,由於ghost更新特別快,我安裝我個人博客cmlanche.com的時候還是1.9.1,當時沒碰到啥問題,到1.9.2就有一點點不一樣了,所以要注意當時你的安裝版本。

針對安裝過程我會針對ghost的更新不斷更新文檔

環境要求

安裝設置官方文檔:https://docs.ghost.org/docs/getting-started-guide

  1. 操作系統:Ubuntu 16.04 or Ubuntu 18.04 見主機購買文章:Vultr主機購買流程

  2. MySQL

  3. Nginx(最低1.9.5,目的是爲了安裝SSL)

  4. Systemd

  5. NodeJS(推薦使用>=8.9 (Node v8 carbon LTS),不支持9+,10+)

  6. 至少1GB內存

  7. 在非root用戶下執行ghost命令

環境安裝

# 登錄你的Ubuntu系統
ssh root@<yourip>
# 添加系統新用戶
adduser <yourusername>
# 提高該用戶權限
usermod -aG sudo <yourusername>
# 登錄到這個用戶
su <yourusername>
# 更新Ubuntu的包管理工具apt-get
sudo apt-get update
sudo apt-get upgrade
# 安裝Nginx
sudo apt-get install nginx
# 爲http和https打開防火牆
sudo ufw allow 'Nginx Full'
# 安裝MySql
sudo apt-get install mysql-server
# 登錄到mysql,更新root密碼
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '<you-mysql-password>';
# 創建mysql數據庫
create database <yourdatabasename>
# 退出mysql
exit
# 以後你登錄mysql,就需要這樣了:
mysql -uroot -p
# 安裝NodeJS
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash
sudo apt-get install -y nodejs
# 安裝Ghost CLI
sudo npm i -g ghost-cli
# 創建ghost安裝目錄,並賦予權限
sudo mkdir -p /var/www/ghost
sudo chown <yourusername>:<yourusername> /var/www/ghost # 比如我的用戶名是test,那就用test:test
sudo chmod 775 /var/www/ghost # 賦予該目錄讀寫權限
# 將工作環境切換到ghost安裝目錄
cd /var/www/ghost
# 安裝ghost
ghost install # 安裝會默認使用mysql數據庫,另外可以選擇使用sqlite,生產環境當然用mysql了

環境排錯

在使用ghost install安裝的時候,報錯:

✔ Checking system Node.js version
✔ Checking logged in user
✔ Checking current folder permissions
✔ Checking operating system compatibility
✔ Checking for a MySQL installation
✔ Checking memory availability
✖ Checking for latest Ghost version
A ProcessError occurred.
​
Message: Command failed: yarn info ghost versions --json
{"type":"error","data":"An unexpected error occurred: \"EACCES: permission denied, scandir '/home/cmlanche/.config/yarn/link'\"."}

錯誤說/home/cmlanche/.config沒權限,那麼我們授予當前用戶這個目錄的權限:

sudo chown cmlanche:cmlanche /home/cmlanche/.config

注意這裏的cmlanche是我係統裏面的用戶名,你應該替換成你的用戶名。

ghost安裝必須在一個空目錄,而剛剛出錯,產生了一個叫yarn-error.log的文件,我們需要刪掉它:

rm yarn-error.log

然後再次安裝

ghost install

輸出:

cmlanche@vultrhosting:/var/www/ghost$ ghost install
✔ Checking system Node.js version
✔ Checking logged in user
✔ Checking current folder permissions
✔ Checking operating system compatibility
✔ Checking for a MySQL installation
✔ Checking memory availability
✔ Checking for latest Ghost version
✔ Setting up install directory
✔ Downloading and installing Ghost v2.1.0
✔ Finishing install process
? Enter your blog URL: (http://localhost:2368) https://www.vultrhosting.com

這樣就表示ghost安裝環境一切正常了!

演示安裝VultrHosting.com

我們當然會要求安裝ssl證書了,ghost利用國人編寫的acme.sh提供了一整套非常便捷的安裝Let’s Encrypt ssl數字證書的流程,官方配置看config

Enter Your Blog URL: 設置你的博客站點地址

我們輸入: https://www.vultrhosting.com,這裏我們需要注意的是要設置爲https,對這個域名vultrhosting.com要提前設置好域名解析,例如下圖:

上圖是阿里雲後臺的截圖,你需要再你的域名註冊商設置@和www這兩個主機記錄,指向你的主機IP地址。注意一定要提前設置好,後續安裝ssl證書的時候會去檢測解析狀態的

Enter your MySQL hostname: 設置你的mysql主機地址

默認用localhost就好

Enter your MySQL username: 設置你的mysql用戶名

我們用root就好,或者你自己登錄到mysql去建一個新用戶,注意要賦予必要的權限,比較麻煩,我們就用root就好了。用戶名輸入了,再輸入你的mysql數據庫密碼,密碼就是環境安裝中你重設的root密碼

Do you wish to set up "ghost" mysql user? 是否希望建立一個ghost的mysql用戶

n,否

Do you wish to set up Nginx?

Y,當然安裝了

Do you wish to set up SSL?

Y,等待ssl安裝完成

Do you wish to set up Systemd?

Y,必要選擇,文章開頭的環境要求就說了,必須要Systemd

✔ Creating systemd service file at /var/www/ghost/system/files/ghost_www-vultrhosting-com.service
Running sudo command: ln -sf /var/www/ghost/system/files/ghost_www-vultrhosting-com.service /lib/systemd/system/ghost_www-vultrhosting-com.service
Running sudo command: systemctl daemon-reload
✔ Setting up Systemd
? Do you want to start Ghost? Yes
Running sudo command: systemctl is-active ghost_www-vultrhosting-com
✔ Ensuring user is not logged in as ghost user
✔ Checking if logged in user is directory owner
✔ Checking current folder permissions
Running sudo command: systemctl is-active ghost_www-vultrhosting-com
✔ Validating config
✔ Checking folder permissions
✔ Checking file permissions
✔ Checking content folder ownership
✔ Checking memory availability
Running sudo command: systemctl start ghost_www-vultrhosting-com
✔ Starting Ghost
Running sudo command: systemctl is-enabled ghost_www-vultrhosting-com
Running sudo command: systemctl enable ghost_www-vultrhosting-com --quiet
✔ Enabling Ghost instance startup on server boot
You can access your publication at https://www.vultrhosting.com
Next, go to to your admin interface at https://www.vultrhosting.com/ghost/ to complete the setup of your publication
​
Ghost uses direct mail by default
To set up an alternative email method read our docs at https://docs.ghost.org/docs/mail-config

好了,ghost已經安裝並啓動了,訪問https://www.vultrhosting.com試試吧(^▽^)

ghost安裝失敗怎麼辦?

比如操作失誤,你可以ghost uninstall來刪除剛剛安裝的ghost,注意需要再ghost工作目錄下執行,所有的ghost命令都要求制定工作目錄,上面我使用的工作目錄是/var/www/ghost

刪除ghost之後,你可以重試,如果還是出問題,那麼你可以:

  1. 在官方論壇提問,他們迴應會很快的,論壇地址:https://forum.ghost.org/

  2. 求助我,QQ:1204833748,微信:cmlanche

ghost停止、啓動命令

ghost stop
ghost start

ghost後臺管理

ghost後臺管理會再第一次打開會是一個註冊界面,要求輸入管理員賬號和密碼,註冊成功之後再次打開,如果沒有登錄的話,就是一個登錄界面。所以註冊認證只有一次。但是ghost系統並不僅僅只有一個用戶,你可以在後臺管理邀請其他人加入你的網站系統,通過發郵件邀請的方式。

ghost後臺界面一覽

ghost CMS博客系統崇尚的是簡潔簡便,後臺管理真的是讓人愛不釋手,第一次看到它就喜歡上了,因爲它我愛上了寫博客,讓我有了認真寫作的心,謝謝你ghost!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章