ゲスト 27人 と メンバー0人 がオンラインです

概要

AWSにJoomla4環境を構築する

検証時の環境

  1. サーバの環境
    サーバ AWS EC2
    OS RHLE 8.0
    Webサーバ Nginx 1.20
    PHP PHP 7.4
    Joomla Joomla 4.x

     

  2. 作業環境
    OS Windows10
    ターミナル teraterm最新版


  3. 検証用のパラメータ
    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インスタンス管理用のユーザを作成する

  1. teraterm(ssh)でEC2にログインする
    ユーザ名:ec2-user
    認証方式:RSA/DSA/ECDSA/ED25519鍵を使う
    ・秘密鍵:EC2作成時にダウンロードした秘密鍵

  2. ユーザの作成
    $ sudo useradd admin-user
    $ sudo passwd admin-user
  3. sudo権限を与える
    $ sudo userdel -r ec2-user
  4. 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
  5. teratermを終了する

デフォルトで存在しているec2-userを削除する

  1. teraterm(ssh)でEC2にログインする
    ユーザ名:admin-user
    認証方式:RSA/DSA/ECDSA/ED25519鍵を使う
    ・秘密鍵:EC2作成時にダウンロードした秘密鍵
  2. ec2-userを削除する
    $ sudo userdel -r ec2-user

     

■Joomla構築に必要なツールをインストールする

  1. インストール
    $ sudo dnf update -y
    $ sudo dnf install wget -y
    $ sudo dnf install unzip -y
    $ sudo dnf install nano -y
  2. TimeZoneをAsia/Tokyoに変更する
    $ sudo timedatectl set-timezone Asia/Tokyo

 

WEBサーバの構築

■nginx/php/mysqlのインストール

  1. nginxモジュールをインストールする
    $ sudo dnf module install nginx:1.20
  2. 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
  3. mysqlモジュールをインストールする
    $ sudo dnf module install mysql

 <備考>   
  $ dnf module install ...   

  moduleをつけることでグループ化された一連のツールをインストールできる   
  ※phpモジュールをインストールすると、php-fpm,php-mbstringなどがまとめてインストールできる

■phpの設定

  1. 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
  2. sessionフォルダのグループ変更
    デフォルトで [ apache ] になっているので [ nginx ] に変更する
    $ sudo chown -R root:nginx /var/lib/php/session
    $ sudo chown -R root:nginx /var/lib/php/opcache
  3. 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の設定

  1. 実行ユーザがデフォルトで [ apache ] になっているので [ nginx ] に変更する
    $ sudo nano /etc/php-fpm.d/www.conf
    項目
    user nginx
    group nginx

■nginxの設定

※sslの設定は後で行う

  1. サーバ名を変更する
    $  sudo nano /etc/nginx/nginx.conf
     server {
        server_name  example.com
        ...
    }

■mysqlの設定

  1. mysqlを起動する
    $ sudo systemctl start mysqld
  2. 初期設定を行う
    $ mysql_secure_installation

    ・yes/no 関連の質問はすべて [ y ]を選択する
    ・パスワードの強度は2 (STRONG)を選択する

  3. joomla用のユーザを作成する
    $ mysql -u root -p
    mysql> create user  db-joomla-user identified by 'db-joomla-pass';
    
  4. joomla用のDBを作成する
    mysql> create database db-joomla;
  5. joomla用ユーザにjoomla用dbへのフル権限を与える
    mysql> grant  all on db-joomla.* to db-joomla-user;
  6. mysqlからぬける
    mysql> exit
    $
  7. 作成したユーザでログインできるか確認する
    $ mysql -u db-joomla-user -p
    mysql> exit
    $

■joomlaのダウンロード+配置

  1. 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
  2. DocRootから不要なファイルを削除する
    $ sudo rm -rf /usr/share/nginx/html/*
  3. joomla一式をDocRootフォルダに配置する
  4. configure.phpをDocRootにコピーする
     $ sudo chown nginx:nginx /usr/share/nginx/html/configuration.php
  5. 一時フォルダを作成する
    $ mkdir /usr/share/nginx/html/tmp/sys
    $ mkdir /usr/share/nginx/html/tmp/upload
  6. 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
  7. 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の有効化が必要かも

  8. サービスの有効化
    $ sudo systemctl enable mysqld
    $ sudo systemctl enable php-fpm
    $ sudo systemctl enable nginx
  9. サービスの再起動
    $ sudo systemctl restart mysqld
    $ sudo systemctl restart php-fpm
    $ sudo systemctl restart nginx

joomlaの初期設定

■初期設定を行う

  1. ブラウザからjoomlaの初期設定画面にアクセスする
    http://example.com
  2. 言語及びサイト名を設定する
  3. ログイン情報を設定する
  4. データベースを設定する
  5. DBの接続テストが実行される
  6. [ 追加の言語 ..] ボタンをクリックして日本語プラグインをインストールする
  7. インストールフォルダを削除する
    ※削除しないとセキュリティホールになります
    $ sudo rm -rf /usr/share/nginx/html/installation

■デフォルトの言語を日本語にする

  1. ブラウザから管理画面にアクセスする
    http://example.com/administrator
  2. システムのダッシュボード画面を開く
  3. 管理の中にある [ 言語 ] をクリックする
  4. 下図のアイコンをクリックする

■rootユーザの2段階認証を有効にする

  1. [ ユーザメニュー ] の中にある [ アカウントの編集 ] をクリックする


  2. 2段階認証を有効にする
    ※この後は特に説明することないので省略する
  3. 画面下部の認証コードを入力して、[ 保存して閉じる ]をクリックする


■管理用アカウントを作成する

  1. ユーザの管理画面にある [ 新規 ] ボタンをクリックする


  2. ユーザ情報を入力する
  3. ユーザグループはAdministratorを選択する


  4. 基本設定を行う
  5. [ 保存して閉じる ] ボタンを押下して設定を保存する
  6. ログアウトする
  7. 作成したユーザでログインできることを確認する
  8. 二段階認証を有効にする

TLSの設定

Let's Encryptを利用するしてjoomlaのサイトをTLS化する

■管理ツールのインストール

  1. epelリポジトリの追加
    $ sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
  2. certbotのインストール
    $ sudo dnf install certbot

■certbotがドメインの存在確認をするため80番ポートを使うためnginxを停止する

  1. 停止する
    $ sudo systemctl stop nginx

■証明書を発行する

  1. 証明書を発行する
    $ 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の設定を変更する

  1. nginx.confを編集する
    $ sudo /etc/nginx/nginx.conf
  2. tls用のサーバ設定のコメントを外す
  3. 証明書、秘密鍵を設定する
        server {
            listen       443 ssl http2;
            ssl_certificate "[ 証明書+中間証明書のファイルパス ]";
            ssl_certificate_key "[ 秘密鍵のファイルパス ]";
    

■nginxを起動する

  1. nginxを起動する
    $sudo systemctl start nginx

■Joomlaへのアクセスにhttpsを強制する設定を行う

  1. ブラウザからJoomlaの初期設定画面にアクセスする
    http://example.com/administrator

  2. システムのダッシュボードから [ グローバル設定 ]をクリックする
  3. [ サーバ ]タブをクリックする
  4. [ https接続の強制 ] を [ サイト全体 ] にする
  5. [ 保存して閉じる ] ボタンをクリックする
  6. httpでアクセスすると自動でhttpsにリダイレクトされることを確認する

おまけ

さくらインターネットで取得した独自ドメインにサブドメインを追加して、今回作成したEC2インスタンスに向ける。
・今回はサブドメインを追加する
・サブドメインを使わないのであればメインのIPアドレスを編集すればよい

■さくらインターネットのドメイン設定を変更する

  1. さくらインターネットにログインする
  2. ドメイン設定画面に遷移する
  3. コントロールパネルを開く
  4. ゾーンを選択する
  5. レコードの設定画面を開く


  6. 値を設定して [ 追加 ] ボタンをクリックする
  7. 保存ボタンをクリックする
    ※実際に設定したサブドメインが使えるまで若干時間がかかる