前提

AWS AppRunnerは複数コンテナのオーケストレーション機能がない。 そのためphp:7.4.27-apacheを使用する。

Apache設定

全体

ドキュメントルートは/var/www/html/publicにする

Apache設定手順

  1. 000-default.confDocumentRoot /var/www/html/publicおよび該当ディレクトリの <Directory> 設定を追加
  2. .htaccessでの上書きを許可するため、AllowOverride Allを設定
  3. Dockerfileにa2enmod rewriteを追記
  4. .htaccess/var/www/html/publicに作成

000-default.confの設定

Apacheのデフォルト設定ではルートディレクトリで AllowOverride None が有効になっているため、.htaccess を機能させるには対象ディレクトリで明示的に AllowOverride All などを許可する必要があります。

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
+	DocumentRoot /var/www/html/public

+	<Directory /var/www/html/public>
+		Options -Indexes +FollowSymLinks
+		AllowOverride All
+		Require all granted
+	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Dockerfileにa2enmod rewriteを追記

FROM php:7.4.27-apache

EXPOSE 80

COPY .docker/php-httpd/000-default.conf /etc/apache2/sites-available/000-default.conf 
COPY .docker/php-httpd/apache2.conf /etc/apache2/apache2.conf

COPY ./ ./

RUN a2enmod rewrite
RUN chown -R www-data:www-data /var/www/html

.htaccessを/var/www/html/publicに作成

# .htaccess
# @see https://gist.github.com/Guibzs/a3e0b3ea4eb00c246cda66994defd8a4
<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /index.php/
    </IfModule>
</IfModule>

Appendix

起動中のコンテナ内のファイルをコピーするにはdocker cpを使用する。

docker cp

コンテナが稼働している最中にコンテナ内のファイルをローカルにコピーする。
またはその逆。

ref. http://docs.docker.jp/v19.03/engine/reference/commandline/cp.html

# コンテナ内の/path/to/sourceをローカルの./path/to/destinationにコピー
$ docker cp コンテナID:/path/to/container ./path/to/local
# 例
$ docker cp symfony-micro-app:/etc/apache2/sites-available/000-default.conf ./000-default.conf

モジュール

  • 組み込みモジュール
  • 動的モジュール
# 組み込みモジュールを確認
$ apachectl -l
Compiled in modules:
  core.c
  mod_so.c  <=== これが表示されれば動的モジュールを利用できる
  mod_watchdog.c
  http_core.c
  mod_log_config.c
  mod_logio.c
  mod_version.c
  mod_unixd.c