LoginSignup
3
2

More than 3 years have passed since last update.

DockerでCSSが適用されない時に見直すApacheの設定

Posted at

Dockerで開発をしている時、CSSが適用されないことに気づいた。
試しにMAMPの環境で実行してみる。するとCSSは適用される。

原因はApacheの設定

Apacheのhttpdの設定でEnableSendfileをオフにしてやれば良いらしい。

EnableSendfileは、一度読み込んだ静的なファイル(HTML/CSS, JavaScript)を、再度読み込まずにキャッシュ内容を送る機能のようだ。
本番環境においてはリクエスト数を減らせるので便利な機能だ。

さっそくApacheの設定ファイルを書き換えよう。コンテナに入って、

$ sudo docker exec -it コンテナ名  bash

apacheの設定ファイルを探す。CentOSでは以下のディレクトリにある。
(Debianではhttpd.confではなく、apache2.conf)

cat /etc/httpd/conf/httpd.conf

httpd.confの内容をコピーして、ホストにそのままapache.confとして出力してしまう。

cat /etc/httpd/conf/httpd.conf > /var/www/html/apache.conf
apache.conf
# This is the main Apache server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See http://httpd.apache.org/docs/2.4/ for detailed information about
# the directives and /usr/share/doc/apache2/README.Debian about Debian specific
# hints.
#
#
# Summary of how the Apache 2 configuration works in Debian:
# The Apache 2 web server configuration in Debian is quite different to
# upstream's suggested way to configure the web server. This is because Debian's
# default Apache2 installation attempts to make adding and removing modules,
# virtual hosts, and extra configuration directives as flexible as possible, in
# order to make automating the changes and administering the server as easy as
# possible.

# It is split into several files forming the configuration hierarchy outlined
# below, all located in the /etc/apache2/ directory:
#
.
.
.
.
# be turned off when serving from networked-mounted
# filesystems or if support for these functions is otherwise
# broken on your system.
# Defaults if commented: EnableMMAP On, EnableSendfile Off
#
#EnableMMAP off
EnableSendfile on

上記のapache.confとコンテナ内のapache2.confをマウントしてやる。
apache2.confを適当なディレクトリに移動した後、docker-compose.ymlを修正

docker-compose.yml
version: '3'

services:
  web:
    image: php-apache:latest
    volumes:
      - ./php.ini:/usr/local/etc/php/php.ini
      - ./html:/var/www/html
      - ./php_apache/apache.conf:/etc/httpd/conf/httpd.conf
    ports:
      - 8080:80
    depends_on:
      - mysql

apache.confを修正EnableSendfileoffにしてやる。

EnableSendfile off

あとはコンテナを再起動。

EnableSendfileのオプションはバージョンによっては無くなっているらしい。

ローカル環境でCSSが反映されない時に見直すべきhttpdの設定

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2