LoginSignup
18
29

More than 5 years have passed since last update.

Apacheのhttpd.confにPHPを設定する方法

Last updated at Posted at 2018-12-27

PHPモジュールの設定はhttpd.confに書かなくていいというのを見て便利と思ったのでやってみた。

  • 環境
    • macOS Mojave バージョン10.14.3
    • Apache 2.4.37 (Unix)
    • PHP 7.3.1 (cli)

PHPの情報を確認する

$ which php
/usr/local/bin/php

$ php -v
PHP 7.3.1 (cli) (built: Jan 10 2019 13:15:37) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.1, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.1, Copyright (c) 1999-2018, by Zend Technologies

$ php --ini | grep php.ini
Configuration File (php.ini) Path: /usr/local/etc/php/7.3
Loaded Configuration File:         /usr/local/etc/php/7.3/php.ini

PHP用の設定ファイルを作る

Includeで指定するディレクトリにPHP用の設定ファイルを作る。
今回の場合は、 /usr/local/etc/httpd/extraphp71.conf を作る.

PHP用の設定ファイルを作る
$ vi /usr/local/etc/httpd/extra/php73.conf

# httpd.confにをincludeする設定ファイル
#
# PHPの場所 : /usr/local/bin/php
# 対象バージョン : PHP 7.3.1 (cli) (built: Jan 10 2019 13:15:37) ( NTS )
#
# これ↓をhttpd.confに追記する
#Include /usr/local/etc/httpd/extra/php73.conf

#
# AddType allows you to add to or override the MIME configuration
# file specified in TypesConfig for specific file types.
#
AddType application/x-httpd-php .php

#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
LoadModule php7_module "/usr/local/Cellar/php/7.3.1/lib/httpd/modules/libphp7.so"

#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    DirectoryIndex index.php
</IfModule>

#
# php.iniの場所
#
PHPIniDir "/usr/local/etc/php/7.3/php.ini"


# 保存して確認する。
$ ls -l /usr/local/etc/httpd/extra/ | grep php
-rw-r--r--  1 mana  apache   1224 12 21 00:26 php71.conf #<<< バージョン違いもあると便利
-rw-r--r--  1 mana  apache   1065  3  3 18:50 php73.conf

httpd.confに作った設定ファイルを設定する

httpd.confの一番最後
# PHPの設定
# Include /usr/local/etc/httpd/extra/php71.conf
Include /usr/local/etc/httpd/extra/php73.conf

見てみる

# 起動する
$ apachectl start

見てみる・・・できた!便利だ。
スクリーンショット 2019-03-03 19.26.00.png

18
29
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
18
29