【Ghost】Oracle Linux 8 にCMSのGhostを導入

【Ghost】Oracle Linux 8 にCMSのGhostを導入

Ghostとは

ブログサイトを立ち上げることにしました!
ブログって続けるのがたいへんですよね ^^;

ブログサイトの管理(CMS・Content Management System)には、WordPressが有名ですが、WordPressは何度か実装したことがありましたので、今回は別のCMSを探してみました。

そこで見つけたのがGhostです。
Ghostは、Node.js(JavaScript)ベースのCMSです。
とはいっても、サイトを運用するだけなら、JavaScriptの知識はまったくいりません。

SEO対策やメルマガ機能が充実し、モダンで洗練されたデザインのサイトが作れるそうです。

Ghostの導入

今回は、OCI(Oracle Cloud Infrastructure)上でARMベースのインスタンスを作りましたので、そこにGhostを導入していきたいと思います。

環境

  • OS: Oracle Linux Server 8.10
  • Shape: VM.Standard.A1.Flex
  • OCPU count: 1
  • Network bandwidth (Gbps): 1
  • Memory (GB): 6
  • Local disk: Block storage only

ARMベースは非常に安くてオススメです。
ただ、ARMに対応していないライブラリ等がありますので、使用用途が決まっているのであれば、利用可能か確認してから導入しましょう。

1. ファイアウォールの設定

Ghostはデフォルトでポート 2368 で動作します。Nginxを使う場合は、HTTP/HTTPSを開放します。

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

2. 必要なパッケージのインストール

まず、システムを最新化し、必要なパッケージをインストールします。

sudo dnf update -y
sudo dnf install -y epel-release
sudo dnf install -y nginx curl unzip tar gcc-c++ make

3. Node.js(LTS版)とnpmのインストール

GhostはNode.js 20 LTSを推奨しています(2025-02-15時点)。

curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo dnf install -y nodejs
node -v  # バージョン確認
v20.18.3
npm -v   # バージョン確認
10.8.2

4. MySQL(MariaDB)のセットアップ

GhostはMySQLを使用するので、MariaDBをインストールして設定します。

sudo dnf install -y mariadb-server mariadb
sudo systemctl enable --now mariadb

MariaDBの初期設定を行います。

sudo mysql_secure_installation

プロンプトに従って設定します。(rootパスワード設定・不要な設定の削除など)。

次に、Ghost用のデータベースとユーザーを作成します。

sudo mysql -u root -p

以下のSQLを実行(yourpassword は適宜変更します)。

CREATE DATABASE ghost_db;
CREATE USER 'ghost_user'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON ghost_db.* TO 'ghost_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

5. Ghost-CLIのインストール

Ghostを管理するための ghost-cli をインストールします。

sudo npm install -g ghost-cli

6. Ghostのセットアップ

Ghostをインストールするディレクトリを作成して、適切な権限を設定します。

sudo mkdir -p /var/www/ghost
sudo chown -R $(whoami):$(whoami) /var/www/ghost
cd /var/www/ghost

whoamiには、Oracle Linux 8であれば、opcユーザーが入ります。

Ghostをインストールして起動します。

ghost install

✔ Checking system Node.js version - found v20.18.3
✔ Checking current folder permissions
✔ Checking memory availability
✔ Checking free space
✔ Checking for latest Ghost version
✔ Setting up install directory
✔ Downloading and installing Ghost v5.109.6
✔ Finishing install process
? Enter your blog URL: http://www.89wa.jp/
? Enter your MySQL hostname: 127.0.0.1
? Enter your MySQL username: ghost_user
? Enter your MySQL password: [hidden]
? Enter your Ghost database name: ghost_db
✔ Configuring Ghost
✔ Setting up instance

sudo useradd --system --user-group ghost
sudo chown -R ghost:ghost /var/www/ghost/content
✔ Setting up "ghost" system user
ℹ Setting up "ghost" mysql user [skipped] Nginx is not installed. Skipping Nginx setup.
ℹ Setting up Nginx [skipped]
Nginx setup task was skipped, skipping SSL setup
ℹ Setting up SSL [skipped]

? Do you wish to set up Systemd? Yes
sudo mv /tmp/www-89wa-jp/ghost_www-89wa-jp.service /lib/systemd/system/ghost_www-89wa-jp.service
sudo systemctl daemon-reload
✔ Setting up Systemd
sudo systemctl is-active ghost_www-89wa-jp ?
Do you want to start Ghost? Yes
sudo systemctl start ghost_www-89wa-jp
sudo systemctl is-enabled ghost_www-89wa-jp
sudo systemctl enable ghost_www-89wa-jp --quiet
✔ Starting Ghost

Ghost uses direct mail by default. To set up an alternative email method read our docs at https://ghost.org/docs/config/#mail

7. Nginxの設定

「6. Ghostのセットアップ」で、Nginxの設定がスキップされています。
これは、Ghostのインストール中に、/etc/nginx/sites-availableに作成した設定ファイルを移動しようとするが、Oralce Linux8には/etc/nginx/sites-availableディレクトリが存在しないことが原因です。

(1) sites-available と sites-enabled のディレクトリを作成

まず、必要なディレクトリを作成します。

sudo mkdir -p /etc/nginx/sites-available
sudo mkdir -p /etc/nginx/sites-enabled

(2) Nginxの設定を修正

Nginxのメイン設定ファイルを開き、sites-enabled ディレクトリを読み込むようにします。

sudo nano /etc/nginx/nginx.conf

ファイル内の http { ... } セクション内に、次の行を追加:

include /etc/nginx/sites-enabled/*.conf;

(3) GhostのNginx設定を手動で修正

Ghostのセットアップは途中で止まっていますが、Nginxの設定ファイルは /tmp/www-89wa-jp/www.89wa.jp.conf に一時的に生成されています。

そのため、手動でNginxの設定を移動します。

sudo mv /tmp/www-89wa-jp/www.89wa.jp.conf /etc/nginx/sites-available/www.89wa.jp.conf

その後、sites-enabled にシンボリックリンクを作成します。

sudo ln -s /etc/nginx/sites-available/www.89wa.jp.conf /etc/nginx/sites-enabled/

8. SSLの設定

「Setting up SSL」(ghost sslコマンド)も失敗していますので、certbotを使って手動で設定します。

Oracle Linux 8では、dnfを使ってcertbotを直接インストールできないため、snap経由でインストールします。

sudo dnf config-manager --set-enable ol8_developer_EPEL

sudo dnf repolist all | grep ol8_developer_EPEL ol8_developer_EPEL               
Oracle Linux 8 EPEL Packages for Devel enabled ol8_developer_EPEL_modular       
Oracle Linux 8 EPEL Modular Packages f disabled 

sudo dnf install snapd

sudo systemctl start snapd

sudo snap install core

certbotのインストールには、/snapディレクトリを作成しておく必要があります。
下記のyour_email_addressは適宜変更します。

sudo ln -s /var/lib/snapd/snap /snap
ls -l /snap
lrwxrwxrwx. 1 root root 19 Feb 15 12:36 /snap -> /var/lib/snapd/snap

sudo snap install --classic certbot
2025-02-15T12:37:30Z INFO Waiting for automatic snapd restart...
certbot 3.2.0 from Certbot Project (certbot-eff✓) installed

cd /snap/bin/
ls
certbot

sudo ./certbot --nginx -d www.89wa.jp
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): your_email_address

Please read the Terms of Service at:
https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.pdf
You must agree in order to register with the ACME server. Do you agree?

---

(Y)es/(N)o: y

---

Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.

---

(Y)es/(N)o: y
Account registered.
Requesting a certificate for www.89wa.jp

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/www.89wa.jp/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/www.89wa.jp/privkey.pem
This certificate expires on 2025-05-16.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for www.89wa.jp to /etc/nginx/sites-enabled/www.89wa.jp.conf
Congratulations! You have successfully enabled HTTPS on https://www.89wa.jp

---

If you like Certbot, please consider supporting our work by:

* Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
* Donating to EFF:                    https://eff.org/donate-le

9. ブラウザを起動して、表示してみましょう!

ghost_cms_01.png

トラブルシューティング1 (OCIのパケットフィルタの問題)

うまくいかない場合は、ルータ上のパケットフィルタ等を確認してみましょう。
OCIであれば、仮想クラウドネットワークのセキュリティリストを確認してみましょう。

[メニュー] - [Networking] - [Virtual cloud networks]から、
インスタンスのVNICが所属しているVNCを選択します。
サイドメニューの[Security Lists]からデフォルトセキュリティリストを表示します。

80番と443番ポートが開いているか確認してみましょう。
ghost_cmd_02.png

トラブルシューティング2(OSのSELinuxの問題)2025-03-02追記

ブラウザで表示したときに、Bad Gatewayとエラーが表示されたら、
/var/log/nginx/error.logを確認してみましょう。opcユーザーではファイルを開く権限がないので「sudo su -」等で権限を昇格させてからlessコマンド等で確認します。
ログの中に、「connect() to 127.0.0.1:2368 failed (13: Permission denied) while connecting to upstream」という記載があったら、SELinuxの設定が原因で、NginxからGhost(ポート2368)への接続が拒否されている可能性があります。

sestatus
上記を端末で実行することで、SELinuxが有効かどうかがわかります。
ステータスがeabledで、モードがenforcingの場合、SELinuxが原因の可能性が高いです。

NginxからGhostへの接続を許可する設定を入れます。
sudo setsebool -P http_can_network_connect 1

-P: Permanent 再起動後も設定を有効のままにする。
httpd_can_network_connect: ApacheやNginxがネットワーク接続を行うためのブール値。

0(無効)外部への接続禁止
1(有効)外部への接続許可

設定が終わったらsudo systemctrl restart nginxでNginxを再起動します。