Laravel 6 を Docker 化する。その 5 。 mod_rewrites の設定

今回やること

Debian の Apache の mod_rewrite

上記を行う必要がありました。

Dockerfile での記述、そして、もともと php:7.3-apache コンテナにある /etc/apache2/sites-available/000-default.conf をコピーし、これをベースに .htaccess ファイルを使えるようにする設定等を追加します。

次のようになりました。

$ git diff
diff --git a/Dockerfile b/Dockerfile
index 717fa5c..ed64838 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -23,6 +23,8 @@ RUN apt-get update && apt-get install -y \

 COPY ./docker/my.ini /usr/local/etc/php/conf.d/

+RUN a2enmod rewrite
+COPY ./docker/000-default.conf /etc/apache2/sites-available/000-default.conf
 ENV APACHE_DOCUMENT_ROOT /var/www/html/public
 RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
 RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
diff --git a/docker/000-default.conf b/docker/000-default.conf
index cfe8df5..2bf1df5 100644
--- a/docker/000-default.conf
+++ b/docker/000-default.conf
@@ -26,6 +26,12 @@
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
+
+       <Directory /var/www/html>
+               Options FollowSymLinks
+               AllowOverride All
+               Require all granted
+       </Directory>
 </VirtualHost>

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

なお、もともと php:7.3-apache コンテナにある /etc/apache2/sites-available/000-default.conf ファイルの内容は、次でした。

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

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

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

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

確認

$ git diff routes/web.php
diff --git a/routes/web.php b/routes/web.php
index 810aa34..57772f7 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -14,3 +14,6 @@
 Route::get('/', function () {
     return view('welcome');
 });
+Route::get('/test', function () {
+    return 'test';
+});
$

上記の修正を試験的に行い、 http://127.0.0.1/test にアクセスすると無事 test と表示されましたので、確認 OK です♪

試験的な修正を元に戻し、今回の試みは完了です。

おわりに

コード全体です。

以上です。

コメントを残す

コメントを残す