#
ドキュメント

Document

自分のための備忘録です。

MACのApache + PHP + MySQL環境構築

$ brew install httpd

Apache設定ファイル(httpd.conf

/usr/local/etc/httpd/httpd.conf

  • SSLを利用しないときは、httpd.confを設定すれば良い。
  • /etc/apache2/httpd.confは、MacにプリインストールされているApacheの設定ファイルなので間違えない。
  • brew install httpdでインストールした設定ファイルは、/usr/local/etc/httpd/httpd.conf

ログファイル

/usr/local/etc/httpd/extra/httpd-vhosts.confでバーチャルホストごとに設定する。

設定は、アクセスログをCustomLogディレクティブ、エラーログをErrorLogディレクティブに指定する。

設定例)/usr/local/etc/httpd/extra/httpd-vhosts.conf

ErrorLog "/usr/local/var/log/httpd/sample.local-error_log"
CustomLog "/usr/local/var/log/httpd/sample.local-access_log" common

Homebrewでインストールしたapache2のログ関連ファイルは、慣例として/usr/local/var/log/httpd以下に配置。

PHPと連携

/usr/local/etc/httpd/httpd.confに以下の行を追加する。

libphp7.soのパスは、$ which phpで調べた。

+ LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so // if PHP 7.2
+ # LoadModule php7_module /usr/local/opt/php@7.3/lib/httpd/modules/libphp7.so // if PHP 7.3
+ <IfModule php7_module>
+  AddType application/x-httpd-php .php
+ </IfModule>

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

ref.

DirectoryIndexにindex.phpを追加

ファイル名を指定していないアクセスに対して表示するファイルを指定。
以下の指定では、index.html -> index.phpの順で探索する。

<IfModule dir_module>
+    DirectoryIndex index.html index.php
-    DirectoryIndex index.html
</IfModule>

mod_rewriteを有効化

/usr/local/etc/httpd/httpd.confを以下のように修正。

- # LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
+ LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

Virtual hostsを有効化

/usr/local/etc/httpd/httpd.confVirtual hostsのコメントアウトを解除。

# Virtual hosts
- # Include /usr/local/etc/httpd/extra/httpd-vhosts.conf
+ Include /usr/local/etc/httpd/extra/httpd-vhosts.conf

httpd-vhosts.confにVirtual hostsを指定

/usr/local/etc/httpd/extra/httpd-vhosts.confVirtual hostsを設定。

以下は名前ベース(ServerName)の指定例。

名前ベースのVirtual hostsでは、アクセスしたホストとServerNameが一致する設定が適用される。
httpd-vhosts.confに一致する設定がない場合は、httpd.confの設定が適用される。

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot "/var/www/htmlc/example.com"
    ServerName example.com
    ServerAlias www.example.com
    ErrorLog "/var/log/httpd/example.com-error_log"
    CustomLog "/var/log/httpd/example.com-access_log" common
    <Directory "/var/www/html/example.com">
        #
        # Possible values for the Options directive are "None", "All",
        # or any combination of:
        #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
        #
        # Note that "MultiViews" must be named *explicitly* --- "Options All"
        # doesn't give it to you.
        #
        # The Options directive is both complicated and important.  Please see
        # http://httpd.apache.org/docs/2.4/mod/core.html#options
        # for more information.
        #
        Options FollowSymLinks

        #
        # AllowOverride controls what directives may be placed in .htaccess files.
        # It can be "All", "None", or any combination of the keywords:
        #   AllowOverride FileInfo AuthConfig Limit
        #
        # AllowOverride None
        AllowOverride All

        #
        # Controls who can get stuff from this server.
        #
        Require all granted
    </Directory>
</VirtualHost>

Options indexes FollowSymLinksindexesを削除。

ref.

  • https://qiita.com/Y-Kanoh/items/42af3c2f635653c4eaf7

hostsファイルの編集

/private/etc/hostsに以下を追加(ドメインがexmaple.comの場合)。

127.0.0.1 example.com

Apacheのコマンド

brewでインストールした場合は、下記コマンドで実行可能

  • sudo brew services start httpd
  • sudo brew services stop httpd
  • sudo brew services restart httpd