如何在Ubuntu 18.04上安裝Ghost
Ghost是建立在Node.js平台之上的現代源代碼發布平台。 它是完全可定製的,並且易於使用,使您可以以幾乎為零的學習曲線發布內容。
在本教程中,我們將向您展示如何使用Nginx作為代理在Ubuntu 18.04服務器上部署安全的Ghost博客,免費的Let’s Encrypt SSL證書,Node.js的最新LTS版本以及MySQL / MariaDB作為數據庫後端。
先決條件
作為遵循本教程的前提條件,您將需要:
- 根據官方的Ghost系統要求,您至少需要1G的RAM。 如果服務器的RAM小於1GB,則可以創建交換文件。
- 指向您的公共服務器IP的域名。 在本教程中,我們將使用
example.com
。 - 通過遵循如何在Ubuntu 18.04上安裝Nginx來安裝Nginx。
- 通過按照如何在Ubuntu 18.04上使用UFW設置防火牆來配置防火牆。 確保端口
80
和443
是開放的。
在繼續本教程之前,請確保您以具有sudo特權的用戶身份登錄。
安裝Node.js和Yarn#
在撰寫本文時,建議的Ghost Node.js版本是v8 carbon LTS。 我們將從NodeSource存儲庫安裝Node.js。
使用以下命令為Node.js v8啟用NodeSource存儲庫 curl
命令:
curl -sL https://deb.nodesource.com/setup_8.x | sudo bash -
通過鍵入以下命令安裝Node.js:
sudo apt install nodejs
有關其他安裝方法,請參閱我們的《如何在Ubuntu 18.04上安裝Node.js》指南。
要安裝yarn,請使用以下命令在系統上啟用Yarn存儲庫:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
啟用存儲庫後,使用以下命令安裝yarn:
sudo apt update
sudo apt-get -o Dpkg::Options::="--force-overwrite" install yarn
安裝MySQL#
Ghost支持MySQL,MariaDB和SQLite數據庫。 在本教程中,當您在生產模式下運行Ghost時,我們將使用MySQL(推薦的數據庫)。
使用以下命令安裝MySQL軟件包:
sudo apt install mysql-server
跑過 mysql_secure_installation
命令以提高MySQL安裝的安全性:
sudo mysql_secure_installation
系統將要求您配置 VALIDATE PASSWORD PLUGIN
用於測試MySQL用戶密碼的強度。 密碼驗證策略分為三個級別:低,中和強。 按 ENTER
如果您不想設置驗證密碼插件。
在下一個提示符下,將要求您設置MySQL root用戶的密碼。
一旦設置了root密碼,腳本還將要求您刪除匿名用戶,限制root用戶對本地計算機的訪問並刪除測試數據庫。 你應該回答 Y
(是)所有問題。
接下來,我們需要更改身份驗證方法 auth_socket
至 mysql_native_password
,因此Ghost安裝程序可以訪問我們的MySQL服務器。 為此,請以root用戶身份登錄到MySQL服務器:
sudo mysql
並運行以下查詢,該查詢將在使用MySQL根用戶時設置密碼 mysql_native_password
方法:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_pasword';
FLUSH PRIVILEGES;
確保你改變 very_strong_pasword
具有強密碼。
安裝Ghost-CLI#
我們將使用官方的Ghost CLI實用程序安裝Ghost。 該實用程序允許您在單個命令中安裝或更新Ghost。
Ghost CLI作為npm軟件包提供。 以下命令將在您的Ubuntu系統上全局安裝Ghost CLI:
sudo yarn global add ghost-cli
創建Ghost安裝位置
我們將在以下位置下載並安裝Ghost: /var/www/ghost
目錄,這是建議的安裝位置。
要創建目錄類型:
sudo mkdir -p /var/www/ghost
將目錄的所有權更改為您的用戶:
sudo chown $USER:$USER /var/www/ghost
$USER
是保存您的用戶名的環境變量。
設置正確的目錄權限:
sudo chmod 775 /var/www/ghost
安裝Ghost#
現在,您已經安裝了Ghost CLI,並且所有先決條件已經完成,我們可以從安裝開始。
更改為 /var/www/ghost
目錄。
cd /var/www/ghost
開始安裝運行 ghost install
,它將安裝和配置Ghost,將Nginx配置為反向代理,並免費讓我們加密SSL證書來保護網站。
ghost install
✔ Checking system Node.js version
✔ Checking logged in user
✔ Checking current folder permissions
System checks failed with message: 'Linux version is not Ubuntu 16'
Some features of Ghost-CLI may not work without additional configuration.
For local installs we recommend using `ghost install local` instead.
? Continue anyway? (y/N) y
安裝程序將檢查系統並打印一些警告,如上所示。 類型 y
繼續,安裝程序將下載並安裝Ghost:
ℹ Checking operating system compatibility [skipped]
✔ Checking for a MySQL installation
✔ Checking memory availability
✔ Checking for latest Ghost version
✔ Setting up install directory
✔ Downloading and installing Ghost v1.24.9
✔ Finishing install process
接下來,將提示您設置博客URL和MySQL信息。 提供我們已在其中設置的根用戶名和密碼 Installing MySQL
部分,並使用默認數據庫名稱 ghost_prod
。
? Enter your blog URL: https://example.com
? Enter your MySQL hostname: localhost
? Enter your MySQL username: root
? Enter your MySQL password: [hidden]
? Enter your Ghost database name: ghost_prod
安裝程序將創建一個名為 ghost
並詢問您是否要創建幽靈MySQL用戶,鍵入 Yes
。
✔ Configuring Ghost
✔ Setting up instance
Running sudo command: chown -R ghost:ghost /var/www/ghost/content
✔ Setting up "ghost" system user
? Do you wish to set up "ghost" mysql user? Yes
✔ Setting up "ghost" mysql user
接下來,安裝程序將詢問您是否要設置Nginx。 輸入確認 Yes
。
? Do you wish to set up Nginx? Yes
✔ Creating nginx config file at /var/www/ghost/system/files/example.com.conf
Running sudo command: ln -sf /var/www/ghost/system/files/example.com.conf /etc/nginx/sites-available/example.com.conf
Running sudo command: ln -sf /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf
Running sudo command: nginx -s reload
✔ Setting up Nginx
設置好Nginx之後,安裝程序將詢問您是否要設置SSL。 輸入確認 Yes
配置嚮導將要求您輸入電子郵件地址,然後它將為您的域生成一個免費的“讓我們加密SSL”證書並配置Nginx。
? Do you wish to set up SSL? Yes
? Enter your email (used for Let's Encrypt notifications) [email protected]
Running sudo command: mkdir -p /etc/letsencrypt
Running sudo command: ./acme.sh --install --home /etc/letsencrypt
Running sudo command: /etc/letsencrypt/acme.sh --issue --home /etc/letsencrypt --domain example.com --webroot /var/www/ghost/system/nginx-root --reloadcmd "nginx -s reload" --accountemail [email protected]
Running sudo command: openssl dhparam -out /etc/nginx/snippets/dhparam.pem 2048
Running sudo command: mv /tmp/ssl-params.conf /etc/nginx/snippets/ssl-params.conf
✔ Creating ssl config file at /var/www/ghost/system/files/example.com-ssl.conf
Running sudo command: ln -sf /var/www/ghost/system/files/example.com-ssl.conf /etc/nginx/sites-available/example.com-ssl.conf
Running sudo command: ln -sf /etc/nginx/sites-available/example.com-ssl.conf /etc/nginx/sites-enabled/example.com-ssl.conf
Running sudo command: nginx -s reload
✔ Setting up SSL
接下來,安裝程序將詢問您是否要設置systemd服務。 類型 Y
接受,安裝程序將創建一個名為ghost_example-com的新systemd服務,並使其在啟動時啟動:
? Do you wish to set up Systemd? Yes
✔ Creating systemd service file at /var/www/ghost/system/files/ghost_example-com.service
Running sudo command: ln -sf /var/www/ghost/system/files/ghost_example-com.service /lib/systemd/system/ghost_example-com.service
Running sudo command: systemctl daemon-reload
✔ Setting up Systemd
最後,安裝程序將設置數據庫並詢問您是否要啟動Ghost,鍵入 Yes
。
Running sudo command: /var/www/ghost/current/node_modules/.bin/knex-migrator-migrate --init --mgpath /var/www/ghost/current
✔ Running database migrations
? Do you want to start Ghost? Yes
Running sudo command: systemctl is-active ghost_example-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_example-com
✔ Validating config
✔ Checking folder permissions
✔ Checking file permissions
✔ Checking content folder ownership
✔ Checking memory availability
Running sudo command: systemctl start ghost_example-com
✔ Starting Ghost
Running sudo command: systemctl is-enabled ghost_example-com
Running sudo command: systemctl enable ghost_example-com --quiet
✔ Starting Ghost
You can access your publication at https://example.com
Next, go to your admin interface at https://example.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設置#
打開瀏覽器,轉到位於以下位置的Ghost管理界面 https://example.com/ghost/
您將看到以下屏幕:
要開始,請點擊 Create your account
按鈕。
您將被重定向到“創建您的帳戶”屏幕,在該屏幕上您需要輸入您的Blog標題以及您的帳戶全名,電子郵件地址和密碼:
填寫詳細信息後,點擊 Invite your team
按鈕。
在此屏幕上,系統將要求您輸入合作者的電子郵件地址。 您只需點擊 I'll do this later, take me to my blog!
鏈接,您將被重定向到Ghost儀錶板:
在這裡,您可以創建新帖子,添加用戶並更改Ghosts配置。
結論#
在本教程中,您學習了如何安裝可用於生產的Ghost實例。
現在,您應該訪問Ghost幫助頁面,並了解有關如何管理Ghost安裝的更多信息。 您還可以訪問Ghost市場並發現Ghost主題。
ghost nodejs npm ubuntu博客