如何在 Ubuntu 16.04 上配置和安裝 Ghost


在這篇文章中,我們將學習如何在 Ubuntu 16.04 上安裝和配置 Ghost。Ghost 是一個輕量級且開源的部落格程式,易於使用且可定製,並提供許多主題。

先決條件

此安裝需要以下條件:

  • 已安裝 Ubuntu 16.04 的伺服器。
  • 具有 Sudo 許可權的非 root 使用者。
  • 在伺服器上安裝了 Nginx 網路伺服器。

在 Ubuntu 16.04 上安裝 Ghost

我們將使用 Ghost 官方 GitHub 倉庫來安裝 Ghost。

首先,我們將使用以下命令獲取 Ghost 的最新版本。(此處應補充具體命令)

$ wget https://ghost.org/zip/ghost-latest.zip
Output:
   --2017-05-18 12:06:25-- https://ghost.org/zip/ghost-latest.zip
Resolving ghost.org (ghost.org)... 104.16.32.191, 104.16.34.191, 104.16.33.191, ...
Connecting to ghost.org (ghost.org)|104.16.32.191|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: /zip/ghost-latest.zip/ [following]
   --2017-05-18 12:06:27-- https://ghost.org/zip/ghost-latest.zip/
Reusing existing connection to ghost.or
g:443.
HTTP request sent, awaiting response... 302 Found
Location: https://github.com/TryGhost/Ghost/releases/download/0.11.9/Ghost-0.11.9.zip [following]
   --2017-05-18 12:06:28-- https://github.com/TryGhost/Ghost/releases/download/0.11.9/Ghost-0.11.9.zip
Resolving github.com (github.com)... 192.30.253.113, 192.30.253.112
Connecting to github.com (github.com)|192.30.253.113|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://github-cloud.s3.amazonaws.com/releases/9852918/d047ce0e-3a8d-1 1e7-937c-58bc76a5c547.zip?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA ISTNZFOVBIJMK3TQ%2F20170518%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20170518T       063630Z&X-Amz-Expires=300&X-Amz-Signature=495ca36c4d81720b86bef7f49c9a99720aab30 cdcd8f7e0c62350e48a7a417cb&X-Amz-SignedHeaders=host&actor_id=0&response-content- disposition=attachment%3B%20filename%3DGhost-0.11.9.zip&response-content-type=ap       plication%2Foctet-stream [following]
   --2017-05-18 12:06:31-- https://github-cloud.s3.amazonaws.com/releases/9852918/ d047ce0e-3a8d-11e7-937c-58bc76a5c547.zip?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz- Credential=AKIAISTNZFOVBIJMK3TQ%2F20170518%2Fus-east-1%2Fs3%2Faws4_request&X-Amz -         Date=20170518T063630Z&X-Amz-Expires=300&X-Amz-Signature=495ca36c4d81720b86bef7f 49c9a99720aab30cdcd8f7e0c62350e48a7a417cb&X-Amz-SignedHeaders=host&actor_id=0&re sponse-content-disposition=attachment%3B%20filename%3DGhost-0.11.9.zip&response-       content-type=application%2Foctet-stream
Resolving github-cloud.s3.amazonaws.com (github-cloud.s3.amazonaws.com)... 52.21       6.65.72
Connecting to github-cloud.s3.amazonaws.com (github-cloud.s3.amazonaws.com)|52.2       16.65.72|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4219850 (4.0M) [application/octet-stream]
Saving to: ‘ghost-latest.zip’
ghost-latest.zip.1          100%[======================================>] 4.02M 1.26MB/s    in 3.2s
2017-05-18 12:10:14 (1.26 MB/s) - ‘ghost-latest.zip’ saved [4219850/4219850]

下載軟體包後,我們將使用以下命令將下載的檔案解壓縮到伺服器上的 /var/www/ghost 資料夾。(此處應補充具體命令)

$ sudo unzip -d /var/www/ghost ghost-latest.zip

從軟體包檔案中解壓縮檔案後,我們將切換到 /var/www/ghost 資料夾,並使用 npm 命令列工具安裝生產環境所需的 Ghost 依賴項。

以下是安裝 Ghost 及生產環境所需依賴項的命令。(此處應補充具體命令)

$ sudo npm install --production
Output:
[ ...............] | cloneCurrentTree: sill cloneCurrentTree Finishing
npm WARN lifecycle ghost@0.11.9~preinstall: cannot run in wd %s %s (wd=%s) ghost@0.11.9 node core/server/utils/npm/preinstall.js /var/www/ghost
> dtrace-provider@0.8.1 install /var/www/ghost/node_modules/gscan/node_modules/ghost-ignition/node_modules/bunyan/node_modules/dtrace-provider
> node scripts/install.js
> sqlite3@3.1.8 install /var/www/ghost/node_modules/sqlite3
> node-pre-gyp install --fallback-to-build
[sqlite3] Success: "/var/www/ghost/node_modules/sqlite3/lib/binding/node-v48-linux-x64/node_sqlite3.node" is installed via remote
> jsonpath@0.2.11 postinstall /var/www/ghost/node_modules/jsonpath
> node lib/aesprim.js > generated/aesprim-browser.js
ghost@0.11.9 /var/www/ghost
….
….
   | | `-- ms@0.7.3
   | +-- form-data@2.1.4
   | | +-- asynckit@0.4.0
   | | +-- combined-stream@1.0.5
   | | | `-- delayed-stream@1.0.0
   | | `-- mime-types@2.1.15
   | | `-- mime-db@1.27.0
   | +-- formidable@1.1.1
   | +-- methods@1.1.2
   | +-- mime@1.3.6
   | `-- readable-stream@2.2.9
   | +-- buffer-shims@1.0.0
   | +-- core-util-is@1.0.2
   | +-- inherits@2.0.3
   | +-- isarray@1.0.0
   | +-- process-nextick-args@1.0.7
   | +-- string_decoder@1.0.0
   | `-- util-deprecate@1.0.2
   +-- unidecode@0.1.8
   +-- uuid@3.0.0
   +-- validator@6.3.0
   +-- vary@1.1.1
   +-- xml@1.0.1
   `-- xtend@4.0.1

配置 Ghost

安裝依賴項後,我們需要修改位於 /var/www/ghost 的 Ghost 配置檔案。配置檔案並不直接存在,但 Ghost 安裝包包含一個名為 config.example.js 的配置檔案,我們將暫時使用它進行配置。

首先,我們將檔案複製到 /var/www/ghost 資料夾並命名為 config.js,以下是如何複製配置檔案的命令。(此處應補充具體命令)

$ sudo cp config.example.js config.js

將示例檔案複製到 config.js 後,我們將根據我們的環境編輯配置檔案。

$ sudo vi config.js

在這裡,我們將編輯 Ghost 伺服器的 URL 配置。如果不更改 URL,部落格上的連結將指向預設 URL my-ghost-blog.com。因此,我們需要更改“url”的值,可以使用域名或伺服器的 IP 地址,在本演示中我們只使用 IP 地址,實際應用中將使用部落格的域名。

Output:
// # Ghost Configuration
// Setup your Ghost install for various [environments](http://support.ghost.org/config/#about-environments).
// Ghost runs in `development` mode by default. Full documentation can be found at http://support.ghost.org/config/var path = require('path'),config;
config = {
   // ### Production
   // When running Ghost in the wild, use the production environment.
   // Configure your URL and mail settings here
   production: {
      url: 'http://192.168.1.123',
      mail: {
         transport: 'SMTP',
         options: {
            service: 'Gmail',
            auth: {
               user: 'youremail@gmail.com',
               pass: 'yourpassword'
            }
         }
      },
   database: {
      client: 'sqlite3',
      connection: {
         filename: path.join(__dirname, '/content/data/ghost.db')
      },
      debug: false
   },
   server: {
      host: '127.0.0.1',
      port: '2368'
   }
},
…
…

我的本地設定使用了我的機器的 IP 地址。

接下來,我們將配置電子郵件通知,這對於 Ghost 使用者的密碼恢復或任何使用者更新通知是必要的。儲存配置檔案並退出 vi 編輯器。

使用以下命令啟動 Ghost(此處應補充具體命令)

$ sudo npm start –production
Output:
> ghost@0.11.9 start /var/www/ghost
> node index
WARNING: Ghost is attempting to use a direct method to send an email.It is recommended that you explicitly configure an email service.
Help and documentation can be found at http://support.ghost.org/mail.
Migrations: Creating tables...
Migrations: Creating table: posts
Migrations: Creating table: users
Migrations: Creating table: roles
Migrations: Creating table: roles_users
Migrations: Creating table: permissions
Migrations: Creating table: permissions_users
Migrations: Creating table: permissions_roles
Migrations: Creating table: permissions_apps
Migrations: Creating table: settings
Migrations: Creating table: tags
Migrations: Creating table: posts_tags
Migrations: Creating table: apps
Migrations: Creating table: app_settings
Migrations: Creating table: app_fields
Migrations: Creating table: clients
Migrations: Creating table: client_trusted_domains
Migrations: Creating table: accesstokens
Migrations: Creating table: refreshtokens
Migrations: Creating table: subscribers
Migrations: Running fixture populations
Migrations: Creating owner
Ghost is running in production...
Your blog is now available on http://192.168.1.123
Ctrl+C to shut down

配置 Nginx 將 Web 請求代理到 Ghost

預設情況下,Ghost 將在埠 2368 上執行,我們需要配置 Nginx 以允許在埠 80 上連線,並在不向域名或 IP 地址新增 2368 的情況下訪問 Ghost。

按 CTRL+C 停止 Ghost。

現在更改目錄到 /etc/nginx。

$ cd /etc/nginx.

刪除 sites-enabled/default 中的預設 Nginx 配置。

$ sudo rm sites-enabled/default

在 /etc/nginx/sites-available/ 中建立一個名為 ghost 的新配置檔案。

$ sudo nano /etc/nginx/sites-available/ghost

貼上以下配置並更改 your-domain-name。(此處應補充具體Nginx配置)

$ vi /etc/nginx/sites-available/ghost
server {
   listen 80;
   server_name 192.168.1.123; # Your-IP-Address
   location / {
      proxy_set_header HOST $host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://127.0.0.1:2368;
   }
}

儲存檔案並退出編輯器。

我們需要透過在 /etc/nginx/sites-enabled 資料夾中建立 ghost 檔案的符號連結來啟用此配置。(此處應補充具體命令)

$ sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost

現在重新啟動 Nginx 以應用更改。(此處應補充具體命令)

$ sudo service nginx restart

重新啟動 Nginx 後,我們將訪問部落格站點進行初始設定。

我們可以透過訪問伺服器的 http://your_ip_address 來訪問部落格,但對於初始設定,我們需要使用以下 URL 訪問部落格:http://your_ip_address/ghost。

作為部落格管理員,我們需要在部落格上建立一個帳戶,以便管理並邀請使用者加入部落格。點選“建立您的帳戶”。

在下一頁,我們將提供管理員電子郵件 ID、全名、密碼和部落格標題。

接下來,我們將邀請一些人加入部落格。

歡迎螢幕如下所示。(此處應補充圖片)

現在,我們將透過點選“新建部落格”,然後點選“新建文章”來撰寫部落格。

編輯文章後,我們將透過點選“儲存草稿”列表然後點選“釋出”來發布文章。

我們現在可以使用 URL http://your_ip_address 訪問部落格。

Ghost 部落格如下所示。(此處應補充圖片)

在這篇文章中,我們學習瞭如何安裝和配置 Ghost。Ghost 是一個非常易於使用的部落格站點應用程式,我們還學習瞭如何使用 Ghost 部落格軟體建立我們的第一個部落格,請探索更多選項,因為 Ghost 可以做很多事情。

更新於:2020年1月23日

276 次檢視

開啟你的 職業生涯

完成課程獲得認證

開始學習
廣告