如何在Ubuntu Server 18.04 / 20.04上安裝Ghost Blogging平台
本教程將向您展示如何在Ubuntu服務器上安裝Ghost博客平台。 Ghost是用Node.js編碼的開源博客軟件,可讓您創建現代,美觀的博客。 與WordPress相比,Ghost輕巧且速度更快,因為它是專為博客而構建的,而不是像WordPress這樣的全面內容管理系統。
幽靈功能
在撰寫本文時,Ghost的最新版本是v3.2.0,於2019年12月23日發布。Ghost的功能如下:
- 基於Markdown的編輯器,可讓您快速撰寫帖子。
- 簡單的內容管理。
- 與您的團隊進行協同編輯。
- 預定發布
- 內置分析
- 直接內置正確的SEO,帶有語義標記,永久鏈接,XML站點地圖,規範標籤和帶有手動覆蓋的自動元數據。
- 集成的AMP(加速的移動頁面)支持
- 完整的RSS提要,電子郵件訂閱捕獲表單和Slack Webhook集成
- Ghost市場上提供了數百個精美的免費和高級主題
- 具有深色模式支持的外觀漂亮的默認主題Capser。
- 適用於Linux,Mac和Windows的跨平台桌面應用程序。
- 官方的Ghost Migrator WordPress插件可讓您輕鬆地從WordPress遷移到Ghost。
Ghost基金會提供託管託管,但是在這裡我們將看到如何在Ubuntu服務器上創建自託管Ghost博客。
在Ubuntu服務器上安裝Ghost的前提條件
要運行Ghost博客,您需要一台至少具有1GB RAM的服務器。 您可以單擊此特殊鏈接以在DigitalOcean上獲得$ 50的免費信用。 (僅適用於新用戶)。 如果您已經是DigitalOcean用戶,則可以單擊此特殊鏈接以在Vultr上獲得$ 50的免費信用(僅適用於新用戶)。 在DigitalOcean或Vultr上擁有帳戶後,在服務器上安裝Ubuntu並按照以下說明進行操作。 為了獲得最佳兼容性,請使用Ubuntu的LTS版本,例如Ubuntu 18.04或16.04。
您還需要一個域名。 我從NameCheap註冊了我的域名,因為它價格低廉,並且終身免費提供Whois隱私保護。
注意:我在Ubuntu上使用sudo用戶安裝了Ghost。 為了獲得最佳結果,您還應該以sudo用戶(而不是root)使用本教程。 要添加sudo用戶,只需運行
sudo adduser username sudo adduser username sudo
然後切換到新用戶。
su - username
步驟1:更新Ubuntu
如果您的服務器已有一段時間沒有更新,請運行以下命令來更新現有軟件包。
sudo apt update;sudo apt upgrade
步驟2:在Ubuntu上安裝Node.js
Ghost要求您安裝Node.js的LTS版本,並且不支持非LTS版本。 Node.js的最新LTS版本是v12.x,但是當前Ghost與Node.js v10.x更加兼容。 使用以下命令添加NodeSource存儲庫。
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
然後安裝Node.js。
sudo apt install nodejs
檢查節點版本。
node -v
樣本輸出:
v10.18.1
檢查一下 npm
版:
npm -v
樣本輸出:
6.13.4
步驟3:安裝MariaDB數據庫服務器
支持MySQL和MariaDB。 MariaDB是MySQL的直接替代品。 它是由MySQL團隊的前成員開發的,他們擔心Oracle可能會將MySQL變成開源產品。 因此,讓我們安裝MariaDB數據庫服務器。
輸入以下命令以將其安裝在Ubuntu 18.04 / 20.04上。
sudo apt install mariadb-server mariadb-client
安裝後,MariaDB服務器應自動啟動。 使用 系統控制 檢查其狀態。
systemctl status mariadb
樣本輸出:
● mariadb.service - MariaDB 10.1.34 database server Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2018-09-08 11:13:27 UTC; 21s ago Docs: man:mysqld(8) https://mariadb.com/kb/en/library/systemd/ Main PID: 3473 (mysqld) Status: "Taking your SQL requests now..." Tasks: 27 (limit: 505) CGroup: /system.slice/mariadb.service └─3473 /usr/sbin/mysqld
如果它沒有運行,請使用以下命令啟動它:
sudo systemctl start mariadb
要使MariaDB在系統啟動時自動啟動,請運行
sudo systemctl enable mariadb
現在運行安裝後安全腳本。
sudo mysql_secure_installation
當要求您輸入MariaDB根密碼時,請按Enter鍵,因為尚未設置根密碼。 然後輸入 y
設置MariaDB服務器的根密碼。
接下來,您可以按Enter回答所有剩餘的問題,這將刪除匿名用戶,禁用遠程root登錄並刪除測試數據庫。 此步驟是MariaDB數據庫安全性的基本要求。 (注意,信 Y
大寫,這是默認答案。)
檢查MariaDB服務器版本信息。
mariadb --version
輸出:
mariadb Ver 15.1 Distrib 10.1.34-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
步驟4:為Ghost創建數據庫和用戶
現在我們需要登錄MariaDB控制台並為Ghost創建數據庫和用戶。 默認情況下,Ubuntu上的MaraiDB軟件包使用unix_socket來驗證用戶登錄名,這基本上意味着您可以使用操作系統的用戶名和密碼登錄MariaDB控制台。 因此,您可以運行以下命令來登錄,而無需提供MariaDB根密碼。
sudo mariadb -u root
使用以下命令為Ghost創建數據庫。 我命名了 ghost
,但您可以使用任何喜歡的名稱。 (不要忽略分號。)
create database ghost;
然後,輸入以下命令為Ghost創建數據庫用戶,並將該ghost數據庫的所有特權授予該用戶。
grant all privileges on ghost.* to gho[email protected] identified by 'ghost_password';
刷新特權表以使更改生效,然後退出MariaDB控制台。
flush privileges; exit;
步驟5:安裝Nginx Web服務器
Ghost將使用Nginx Web服務器,因此運行以下命令從默認的Ubuntu存儲庫安裝它。
sudo apt install nginx
如果使用UFW防火牆,則還需要打開端口80和443。
sudo ufw allow 80/tcp sudo ufw allow 443/tcp
步驟6:為您的博客域名創建記錄
在安裝Ghost之前,建議您為博客域名創建DNS A記錄。 A記錄將您的域名指向您的Ubuntu服務器的IP地址。
步驟7:安裝Ghost
安裝Ghost-CLI。
sudo npm install [email protected] -g
然後創建一個目錄(/var/www/ghost/
)。
sudo mkdir -p /var/www/ghost/
向您的用戶帳戶授予權限。 更換 username
使用您的真實用戶名。
sudo setfacl -R -m u:username:rwx /var/www/ghost/ sudo chmod 775 /var/www/ghost
現在將工作目錄更改為 /var/www/ghost/
並安裝Ghost。
cd /var/www/ghost/ ghost install
安裝可能需要一段時間。 在安裝過程中,將要求您輸入博客URL。 輸入類似 https://yourdomain.com
。 您將需要輸入MariaDB數據庫名稱,即在步驟4中創建的用戶憑據。
? Enter your blog URL: https://yourdomain.com ? Enter your MySQL hostname: localhost ? Enter your MySQL username: ghost ? Enter your MySQL password: ghost_password ? Enter your Ghost database name: ghost
如果您正在使用Ubuntu 20.04,並且看到以下消息,則可以忽略它並繼續安裝。
System checks failed with message: 'Linux version is not Ubuntu 16 or 18' Some features of Ghost-CLI may not work without additional configuration. For local installs we recommend using `ghost install local` instead.
一種 ghost
系統用戶將被自動創建。 建議您接受設置Nginx和SSL,以便通過安全的HTTPS協議訪問您的博客。 另外,接受設置Systemd服務,以便您可以輕鬆啟動,停止或重新啟動Ghost。
成功安裝Ghost後,請轉到 https://yourdomain.com/ghost
完成設置。 首先,您需要創建一個帳戶。
然後,您可以邀請一些工作人員用戶訪問您的Ghost博客,也可以稍後進行。
步驟8:編輯Nginx配置文件
默認情況下,Ghost的Nginx配置文件包含一個域名。 如果希望Nginx同時為www域和非www域提供服務,請編輯配置文件。
sudo nano /etc/nginx/sites-enabled/yourdomain.com.conf
找到以下行
server_name yourdomain.com;
添加www域。
server_name yourdomain.com www.yourdomain.com;
保存並關閉文件。 然後刪除 /etc/nginx/sites-enabled/yourdomain.com-ssl.conf
文件。
sudo rm /etc/nginx/sites-enabled/yourdomain.com-ssl.conf
然後安裝Certbot讓我們加密客戶端
sudo apt install certbot python3-certbot-nginx
獲得www域和非www域的SSL證書。
sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d yourdomain.com,www.yourdomain.com
重新啟動Nginx,您就完成了。
sudo systemctl restart nginx
步驟9:設置電子郵件通知
為了從您的Ghost博客發送電子郵件(用於密碼重置,邀請工作人員用戶,會員註冊等),您需要配置SMTP設置。 如果您想使用自己的電子郵件服務器,請閱讀以下教程以輕鬆設置自己的電子郵件服務器。
- 如何使用iRedMail在Ubuntu 18.04上輕鬆設置自己的電子郵件服務器
擁有自己的電子郵件服務器後,請編輯Ghost配置文件。
sudo nano /var/www/ghost/config.production.json
默認情況下,郵件設置如下:
"mail": { "transport": "Direct" },
將其更改為使用SMTP。
"mail": { "transport": "SMTP", "from": "[email protected]", "options": { "service": "yourdomain.com", "host": "mail.yourdomain.com", "port": 465, "secureConnection": true, "auth": { "user": "[email protected]", "pass": "the_email_account_password" } } },
請注意,Ghost不支持SMTP的端口587。 保存並關閉文件。 然後通過systemd服務重新啟動Ghost。
sudo systemctl restart ghost_yourdomain-com.service
現在,您的Ghost博客應該可以發送電子郵件了。
包起來
而已! 我希望本教程可以幫助您在Ubuntu服務器上安裝Ghost。 與往常一樣,如果您發現這篇文章很有用,請訂閱我們的免費新聞通訊以獲取更多提示和技巧。 保重🙂