概要
AWSにJoomla4環境を構築する
検証時の環境
- サーバの環境
サーバ AWS EC2 OS RHLE 8.0 Webサーバ Nginx 1.20 PHP PHP 7.4 Joomla Joomla 4.x - 作業環境
OS Windows10 ターミナル teraterm最新版 - 検証用のパラメータ
EC2 ユーザ名 admin-user EC2 パスワード admin-passwd サーバ名 example.com DB パスワード(root) db-root-pass DB ユーザ名(joomla用) db-joomla-user DB パスワード(joomla用) db-joomla-pass DB 名 db-joomla
仮想マシンの作成
■EC2の作成
Joomlaを動作させる仮想マシンを作成する
基本的にウィザードに従って作成するだけなので、
最低限必要だと思う設定だけ記載する
- 選択するサブネット
ルートテーブルでデフォルトゲートウェイ(宛先:0.0.0.0/0)がIGWに設定されているサブネットを選択する - パブリックIP
有効にする - 終了保護機能
有効にする - ElasticIPを割り当てて作成した仮想マシンに関連付ける
- セキュリティグループの設定
インバウンド設定:HTTP,HTTPS,SSH
・すべて自分のグローバルIPからのみ接続を許可する
※グローバルIP確認方法:https://www.cman.jp/network/support/go_access.cgi
・HTTP,HTTPSは環境構築後に↑の制限を外す
・SSHは環境構築後に削除する
→必要に応じて適宜追加する
■EC2インスタンス管理用のユーザを作成する
- teraterm(ssh)でEC2にログインする
ユーザ名:ec2-user
認証方式:RSA/DSA/ECDSA/ED25519鍵を使う
・秘密鍵:EC2作成時にダウンロードした秘密鍵 - ユーザの作成
$ sudo useradd admin-user $ sudo passwd admin-user
- sudo権限を与える
$ sudo userdel -r ec2-user
- sshの設定情報をec2-user からadmin-userにcopyする
$ sudo cp -arp /home/ec2-user/.ssh /home/admin-user/ $ sudo chown -R admin-user:admin-user /home/admin-user/.ssh
- teratermを終了する
■デフォルトで存在しているec2-userを削除する
- teraterm(ssh)でEC2にログインする
ユーザ名:admin-user
認証方式:RSA/DSA/ECDSA/ED25519鍵を使う
・秘密鍵:EC2作成時にダウンロードした秘密鍵 - ec2-userを削除する
$ sudo userdel -r ec2-user
■Joomla構築に必要なツールをインストールする
- インストール
$ sudo dnf update -y $ sudo dnf install wget -y $ sudo dnf install unzip -y $ sudo dnf install nano -y
- TimeZoneをAsia/Tokyoに変更する
$ sudo timedatectl set-timezone Asia/Tokyo
WEBサーバの構築
■nginx/php/mysqlのインストール
- nginxモジュールをインストールする
$ sudo dnf module install nginx:1.20
- phpモジュールをインストールする
$ sudo dnf module install php:7.4 $ sudo dnf install php-mysqli
必須ではないがJoomlaで推奨されている以下のライブラリをインストールする
$ sudo dnf install php-gd $ sudo dnf install php-zip $ sudo dnf install php-intl
- mysqlモジュールをインストールする
$ sudo dnf module install mysql
<備考>
$ dnf module install ...
moduleをつけることでグループ化された一連のツールをインストールできる
※phpモジュールをインストールすると、php-fpm,php-mbstringなどがまとめてインストールできる
■phpの設定
- php.iniを編集する
$ sudo nano /etc/php.ini
<joomlaの推奨設定>
項目 値 memory_limit 128M upload_max_filesize 30M post_max_size 30M max_execution_time 30
<一時フォルダの変更>
項目 値 sys_temp_dir /usr/share/nginx/html/tmp/sys upload_tmp_dir /usr/share/nginx/html/tmp/upload - sessionフォルダのグループ変更
デフォルトで [ apache ] になっているので [ nginx ] に変更する
$ sudo chown -R root:nginx /var/lib/php/session $ sudo chown -R root:nginx /var/lib/php/opcache
- selinuxの設定
sessionフォルダにhttpdからの書き込み権限を与える
$ sudo chcon -R -t httpd_sys_rw_content_t /var/lib/php/session $ sudo chcon -R -t httpd_sys_rw_content_t /var/lib/php/opcache
■php-fpmの設定
- 実行ユーザがデフォルトで [ apache ] になっているので [ nginx ] に変更する
$ sudo nano /etc/php-fpm.d/www.conf
項目 値 user nginx group nginx
■nginxの設定
※sslの設定は後で行う
- サーバ名を変更する
$ sudo nano /etc/nginx/nginx.conf
server { server_name example.com ... }
■mysqlの設定
- mysqlを起動する
$ sudo systemctl start mysqld
- 初期設定を行う
$ mysql_secure_installation
・yes/no 関連の質問はすべて [ y ]を選択する
・パスワードの強度は2 (STRONG)を選択する - joomla用のユーザを作成する
$ mysql -u root -p mysql> create user db-joomla-user identified by 'db-joomla-pass';
- joomla用のDBを作成する
mysql> create database db-joomla;
- joomla用ユーザにjoomla用dbへのフル権限を与える
mysql> grant all on db-joomla.* to db-joomla-user;
- mysqlからぬける
mysql> exit $
- 作成したユーザでログインできるか確認する
$ mysql -u db-joomla-user -p mysql> exit $
■joomlaのダウンロード+配置
- joomlaをダウンロードする
$ mkdir ~/tmp $ mkdir ~/tmp/joomla $ cd ~/tmp/joomla $ wget -O ~/tmp/joomla/joomla.zip https://downloads.joomla.org/ja/cms/joomla4/4-0-4/Joomla_4-0-4-Stable-Full_Package.zip?format=zip $ unzip ~/tmp/joomla/joomla.zip $ rm ~/tmp/joomla/joomla.zip
- DocRootから不要なファイルを削除する
$ sudo rm -rf /usr/share/nginx/html/*
- joomla一式をDocRootフォルダに配置する
- configure.phpをDocRootにコピーする
$ sudo chown nginx:nginx /usr/share/nginx/html/configuration.php
- 一時フォルダを作成する
$ mkdir /usr/share/nginx/html/tmp/sys $ mkdir /usr/share/nginx/html/tmp/upload
- DocRootの所有者/権限を設定する
$ sudo chown -R nginx:nginx /usr/share/nginx/html/ $ sudo find /usr/share/nginx/html/ -type f | xargs chmod 644 $ sudo find /usr/share/nginx/html/ -type d | xargs chmod 755
- selinuxの設定
# joomlaはエクステンションなどをインストールする際にhttpsアクセスするので許可しておく $ sudo setsebool -P httpd_can_network_connect on # httpdによるファイルの読み込みを許可する $ sudo chcon -R -t httpd_sys_content_t /usr/share/nginx/html/ # httpdによるファイルの読み書きを許可する $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/administrator $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/api $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/cache $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/components $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/configuration.php $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/icons $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/images $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/language $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/layouts $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/libraries $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/media $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/modules $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/plugins $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/templates $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/tmp $ sudo chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/configuration.php # テンプレートの作成 $ sudo restorecon -Rvf /usr/share/nginx/html/ # 確認 $ ls -laZ
※↑joomlaのアップグレードを行う場合はFTPの有効化が必要かも - サービスの有効化
$ sudo systemctl enable mysqld $ sudo systemctl enable php-fpm $ sudo systemctl enable nginx
- サービスの再起動
$ sudo systemctl restart mysqld $ sudo systemctl restart php-fpm $ sudo systemctl restart nginx
joomlaの初期設定
■初期設定を行う
- ブラウザからjoomlaの初期設定画面にアクセスする
http://example.com - 言語及びサイト名を設定する
- ログイン情報を設定する
- データベースを設定する
- DBの接続テストが実行される
- [ 追加の言語 ..] ボタンをクリックして日本語プラグインをインストールする
- インストールフォルダを削除する
※削除しないとセキュリティホールになります
$ sudo rm -rf /usr/share/nginx/html/installation
■デフォルトの言語を日本語にする
- ブラウザから管理画面にアクセスする
http://example.com/administrator - システムのダッシュボード画面を開く
- 管理の中にある [ 言語 ] をクリックする
- 下図のアイコンをクリックする
■rootユーザの2段階認証を有効にする
- [ ユーザメニュー ] の中にある [ アカウントの編集 ] をクリックする
- 2段階認証を有効にする
※この後は特に説明することないので省略する - 画面下部の認証コードを入力して、[ 保存して閉じる ]をクリックする
■管理用アカウントを作成する
- ユーザの管理画面にある [ 新規 ] ボタンをクリックする
- ユーザ情報を入力する
- ユーザグループはAdministratorを選択する
- 基本設定を行う
- [ 保存して閉じる ] ボタンを押下して設定を保存する
- ログアウトする
- 作成したユーザでログインできることを確認する
- 二段階認証を有効にする
TLSの設定
Let's Encryptを利用するしてjoomlaのサイトをTLS化する
■管理ツールのインストール
- epelリポジトリの追加
$ sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
- certbotのインストール
$ sudo dnf install certbot
■certbotがドメインの存在確認をするため80番ポートを使うためnginxを停止する
- 停止する
$ sudo systemctl stop nginx
■証明書を発行する
- 証明書を発行する
$ sudo certbot certonly -d example.com --standalone # emailを聞かれるので入力する Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): [ メールアドレス ] # [ y ] を入力する - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.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: # 成功すると以下のようなメッセージが表示される Requesting a certificate for www.pgdays.com Successfully received certificate. Certificate is saved at: /xxxxxx/fullchain.pem Key is saved at: /xxxxxx/privkey.pem This certificate expires on 2022-02-18. These files will be updated when the certificate renews. Certbot has set up a scheduled task to automatically renew this certificate in the background. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #fullchain.pem : 証明書+中間証明書 #privkey.pem:秘密鍵
■nginxの設定を変更する
- nginx.confを編集する
$ sudo /etc/nginx/nginx.conf
- tls用のサーバ設定のコメントを外す
- 証明書、秘密鍵を設定する
server { listen 443 ssl http2; ssl_certificate "[ 証明書+中間証明書のファイルパス ]"; ssl_certificate_key "[ 秘密鍵のファイルパス ]";
■nginxを起動する
- nginxを起動する
$sudo systemctl start nginx
■Joomlaへのアクセスにhttpsを強制する設定を行う
- ブラウザからJoomlaの初期設定画面にアクセスする
http://example.com/administrator - システムのダッシュボードから [ グローバル設定 ]をクリックする
- [ サーバ ]タブをクリックする
- [ https接続の強制 ] を [ サイト全体 ] にする
- [ 保存して閉じる ] ボタンをクリックする
- httpでアクセスすると自動でhttpsにリダイレクトされることを確認する
おまけ
さくらインターネットで取得した独自ドメインにサブドメインを追加して、今回作成したEC2インスタンスに向ける。
・今回はサブドメインを追加する
・サブドメインを使わないのであればメインのIPアドレスを編集すればよい
■さくらインターネットのドメイン設定を変更する
- さくらインターネットにログインする
- ドメイン設定画面に遷移する
- コントロールパネルを開く
- ゾーンを選択する
- レコードの設定画面を開く
- 値を設定して [ 追加 ] ボタンをクリックする
- 保存ボタンをクリックする
※実際に設定したサブドメインが使えるまで若干時間がかかる