LoginSignup
190
189

More than 5 years have passed since last update.

MacでApache+PHPの環境を構築する

Last updated at Posted at 2013-11-02

MAMPを使わず標準でインストールされているApacheとPHPを使い、複数のWEBサイトをローカルで開発できるようにします。

MAMPを使っている場合は、停止させること。

Apacheの設定

複数のWEBサイトをローカルで開発できるようにするには、以下のファイルを設定する必要があります。

/etc/apache2/httpd.conf
/etc/hosts
/etc/apache2/extra/httpd-vhosts.conf

まずは、Apacheの起動です。

$ sudo /usr/sbin/apachectl start

1. /etc/apache2/httpd.confの設定

ApacheでPHPを有効にしつつ、複数サイトを動かすための設定を行います。

$ sudo vi /etc/apache2/httpd.conf

1-1. PHP5モジュールを有効化する

コメントアウトを外してPHPを有効化します。

- #LoadModule php5_module libexec/apache2/libphp5.so
+ LoadModule php5_module libexec/apache2/libphp5.so

1-2. バーチャルホスト設定を有効化する

コメントアウトを外し、後述するバーチャルホストの設定を有効化します。

# Virtual hosts
- #Include /private/etc/apache2/extra/httpd-vhosts.conf
+ Include /private/etc/apache2/extra/httpd-vhosts.conf

2. /etc/hostsの設定

特定のドメイン名は、ネットではなくローカルを参照するようにします。

2-1. アクセスする時の名前を追記

$ sudo vi /etc/hosts

例えば以下のように書くと、sandbox.localhostでアクセスできるようになります。

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0 localhost

127.0.0.1 sandbox.localhost

2-2. /etc/apache2/extra/httpd-vhosts.confの設定

バーチャルホストの設定です。

以下をhttpd-vhosts.confに追記し、サイトのパス(/Users/is0me/Dev/sandbox)を自分のパスに書き換えてください。

先ほど/etc/hostsに追記した名前をServerNameに指定します。

そして、参照先ディレクトリをDocumentRootに指定します。

<VirtualHost *:80>
    DocumentRoot "/Users/is0me/Dev/sandbox"
    ServerName sandbox.localhost
    ErrorLog "/private/var/log/apache2/sandbox-error_log"
    CustomLog "/private/var/log/apache2/sandbox-access_log" common
    DirectoryIndex index.php index.html

    <Directory "/Users/is0me/Dev/sandbox">
        Options Includes ExecCGI FollowSymLinks
        AllowOverride All    
        order deny,allow
        allow from All
    </Directory>
</VirtualHost>

確認

DocumentRootに指定したディレクトリにテストファイルを作ってください。

$ touch /Users/is0me/Dev/sandbox/index.php && echo '<?php phpinfo();' >> /Users/is0me/Dev/sandbox/index.php

Apacheを再起動

$ sudo /usr/sbin/apachectl restart

sandbox.localhostへアクセスして、phpinfoが表示されたら完了です。

完了後にサイトを追加する場合

/etc/hostsとhttpd-vhosts.confに追記しApacheを再起動してください。

190
189
2

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
190
189