mac 初期設定 part5 - Webサーバの構築 ApacheとPHPのインストール

 

Apacheのインストール

デフォルトでインストールされているapacheの確認をします。

$ apachectl -v

Server version: Apache/2.2.24 (Unix)
Server built: Aug 24 2013 21:10:43

 

次に、HomebrewでApacheの最新版をインストールします。

 

外部 formula の homebrew/dupes を追加

$ brew tap homebrew/dupes

Cloning into '/usr/local/Library/Taps/homebrew-dupes'...

remote: Reusing existing pack: 1083, done.

remote: Counting objects: 9, done.

remote: Compressing objects: 100% (7/7), done.

remote: Total 1092 (delta 3), reused 8 (delta 2)

Receiving objects: 100% (1092/1092), 216.09 KiB | 153.00 KiB/s, done.

Resolving deltas: 100% (559/559), done.

Checking connectivity... done.

Tapped 39 formula 

 

 

外部 formula がインストール可になっているか確認

$ brew search httpd

httpdlibmicrohttpdlighttpd

 

Apache のインストール

$ brew install httpd

==> Downloading http://www.apache.org/dist/httpd/httpd-2.2.26.tar.bz2

######################################################################## 100.0%

==> ./configure --prefix=/usr/local/Cellar/httpd/2.2.26 --mandir=/usr/local/Cell

==> make

==> make install

==> Caveats

To have launchd start httpd at login:

    ln -sfv /usr/local/opt/httpd/*.plist ~/Library/LaunchAgents

Then to load httpd now:

    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.httpd.plist

==> Summary

🍺  /usr/local/Cellar/httpd/2.2.26: 1311 files, 21M, built in 3.4 minutes 

 

新しいApache にパスを通す。

$ echo export PATH='/usr/local/sbin:$PATH' >> ~/.bash_profile

 

設定を有効にするために、.bash_profileを再読み込みします。

$ source .bash_profile

 

改めてapacheの確認。

$ apachectl -v

Server version: Apache/2.2.26 (Unix)

Server built:   Jan 22 2014 20:08:10 

 

無事インストールできました。

 

では、早速起動してみます。

$ sudo apachectl start

 ブラウザで「localhost」にアクセスして動作確認。

It works!

と表示されればOK。 

 

 

PHPのインストール

外部 formula の josegonzalez/homebrew-php を追加

$ brew tap josegonzalez/homebrew-php

Cloning into '/usr/local/Library/Taps/josegonzalez-php'...

remote: Reusing existing pack: 4814, done.

remote: Counting objects: 24, done.

remote: Compressing objects: 100% (16/16), done.

remote: Total 4838 (delta 16), reused 16 (delta 8)

Receiving objects: 100% (4838/4838), 951.47 KiB | 123.00 KiB/s, done.

Resolving deltas: 100% (2761/2761), done.

Checking connectivity... done.

Tapped 285 formula

 

外部 formula がインストール可になっているか確認

$ brew search php

abstract-php   php53-stats      php54-opcache

abstract-php-extension   php53-stemmer      php54-parsekit

chphp   php53-sundown      php54-pcntl

php-build   php53-svm      php54-pdflib

php-code-sniffer   php53-swoole      php54-pdo-dblib

php-cs-fixer   php53-thrift      php54-phalcon

php-install   php53-tidy      php54-proctitle

php-version   php53-timezonedb      php54-propro

php52   php53-tokyotyrant      php54-protobuf

php53   php53-twig      php54-pspell

php53-amqp   php53-uploadprogress      php54-pthreads

php53-apc   php53-uuid      php54-raphf

php53-apcu   php53-varnish      php54-redis

php53-blitz   php53-wbxml      php54-riak

php53-boxwood   php53-xcache      php54-runkit

php53-chdb   php53-xdebug      php54-scrypt

php53-couchbase   php53-xhgui      php54-snappy

php53-crypto   php53-xhp      php54-solr

php53-dbase   php53-xhprof      php54-sphinx

php53-dmtx   php53-xmldiff      php54-spl-types

php53-gearman   php53-yac      php54-ssh2

php53-geoip   php53-yaf      php54-stats

php53-gmagick   php53-yaml      php54-stemmer

php53-graphdat   php53-yar      php54-sundown

php53-htscanner   php53-yaz      php54-svm

php53-http   php53-zenddebugger      php54-swoole

php53-igbinary   php53-zmq      php54-thrift

php53-imagick   php54      php54-tidy

php53-inclued   php54-amqp      php54-timezonedb

php53-intl   php54-apc      php54-tokyotyrant

php53-ioncubeloader   php54-apcu      php54-twig

php53-jsmin   php54-blitz      php54-uploadprogress

php53-judy   php54-boxwood      php54-uuid

php53-leveldb   php54-chdb      php54-varnish

php53-libevent   php54-couchbase      php54-wbxml

php53-libvirt   php54-crypto      php54-xcache

php53-lzf   php54-dbase      php54-xdebug

php53-mailparse   php54-dmtx      php54-xhgui

php53-mcrypt   php54-ev      php54-xhp

php53-memcache   php54-gearman      php54-xhprof

php53-memcached   php54-geoip      php54-xmldiff

php53-midgard2   php54-gmagick      php54-yac

php53-mongo   php54-graphdat      php54-yaf

php53-mosquitto   php54-htscanner      php54-yaml

php53-msgpack   php54-http      php54-yar

php53-mysqlnd_ms   php54-igbinary      php54-yaz

php53-oauth   php54-imagick      php54-zmq

php53-pcntl   php54-inclued      php55

php53-pdflib   php54-intl      php55-amqp

php53-pdo-dblib   php54-ioncubeloader      php55-apcu

php53-phalcon   php54-jsmin      php55-blitz

php53-proctitle   php54-judy      php55-boxwood

php53-propro   php54-leveldb      php55-chdb

php53-protobuf   php54-libevent      php55-couchbase

php53-pspell   php54-libvirt      php55-crypto

php53-pthreads   php54-lzf      php55-dbase

php53-raphf   php54-mailparse      php55-dmtx

php53-redis   php54-mcrypt      php55-ev

php53-riak   php54-memcache      php55-gearman

php53-runkit   php54-memcached      php55-geoip

php53-scrypt   php54-midgard2      php55-gmagick

php53-snappy   php54-mongo      php55-graphdat

php53-solr   php54-mosquitto      php55-htscanner

php53-sphinx   php54-msgpack      php55-http

php53-spl-types   php54-mysqlnd_ms      php55-igbinary

php53-ssh2   php54-oauth      php55-imagick

php55-inclued   php55-propro      php55-varnish

php55-intl   php55-protobuf      php55-wbxml

php55-ioncubeloader   php55-pspell      php55-xcache

php55-jsmin   php55-pthreads      php55-xdebug

php55-judy   php55-raphf      php55-xhgui

php55-leveldb   php55-redis      php55-xhp

php55-libevent   php55-riak      php55-xhprof

php55-libvirt   php55-runkit      php55-xmldiff

php55-lzf   php55-scrypt      php55-yac

php55-mailparse   php55-snappy      php55-yaf

php55-mcrypt   php55-solr      php55-yaml

php55-memcache   php55-sphinx      php55-yar

php55-memcached   php55-spl-types      php55-yaz

php55-midgard2   php55-ssh2      php55-zmq

php55-mongo   php55-stats      phpcpd

php55-mosquitto   php55-stemmer      phpdocumentor

php55-msgpack   php55-sundown      phpdox

php55-mysqlnd_ms   php55-svm      phpenv

php55-oauth   php55-swoole      phplint

php55-opcache   php55-thrift      phploc

php55-parsekit   php55-tidy      phpmd

php55-pcntl   php55-timezonedb      phpmyadmin

php55-pdflib   php55-tokyotyrant      phppgadmin

php55-pdo-dblib   php55-twig      phpsh

php55-phalcon   php55-uploadprogress      phpunit

php55-proctitle   php55-uuid

marcqualie/nginx/php-session-nginx-module 

 

PHP 5.4をMySQLとtidyのオプションをつけてインストールします。

$ brew install php54 --with-mysql --with-tidy

==> Installing dependencies for php54: libpng, freetype, gettext, jpeg, aut

==> Installing php54 dependency: libpng

==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/libpng-1.5.17

######################################################################## 100.0%

==> Pouring libpng-1.5.17.mavericks.bottle.1.tar.gz

🍺  /usr/local/Cellar/libpng/1.5.17: 15 files, 1.0M

==> Installing php54 dependency: freetype

==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/freetype-2.5.

######################################################################## 100.0%

==> Pouring freetype-2.5.2.mavericks.bottle.tar.gz

🍺  /usr/local/Cellar/freetype/2.5.2: 59 files, 2.7M

==> Installing php54 dependency: gettext

==> Downloading http://ftpmirror.gnu.org/gettext/gettext-0.18.3.2.tar.gz

######################################################################## 100.0%

==> ./configure --prefix=/usr/local/Cellar/gettext/0.18.3.2 --with-included-gettex

==> make

==> make install

==> Caveats

This formula is keg-only, so it was not symlinked into /usr/local.

 

OS X provides the BSD gettext library and some software gets confused if both are in the library path.

 

Generally there are no consequences of this for you. If you build your

own software and it requires this formula, you'll need to add to your

build variables:

 

    LDFLAGS:  -L/usr/local/opt/gettext/lib

    CPPFLAGS: -I/usr/local/opt/gettext/include

 

==> Summary

🍺  /usr/local/Cellar/gettext/0.18.3.2: 1835 files, 19M, built in 4.1 minutes

==> Installing php54 dependency: jpeg

==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/jpeg-8d.maver

######################################################################## 100.0%

==> Pouring jpeg-8d.mavericks.bottle.2.tar.gz

🍺  /usr/local/Cellar/jpeg/8d: 18 files, 780K

==> Installing php54 dependency: autoconf

==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/autoconf-2.69

######################################################################## 100.0%

==> Pouring autoconf-2.69.mavericks.bottle.tar.gz

🍺  /usr/local/Cellar/autoconf/2.69: 69 files, 2.0M

==> Installing php54 dependency: automake

==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/automake-1.14

######################################################################## 100.0%

==> Pouring automake-1.14.1.mavericks.bottle.tar.gz

🍺  /usr/local/Cellar/automake/1.14.1: 127 files, 2.5M

==> Installing php54 dependency: libtool

==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/libtool-2.4.2

######################################################################## 100.0%

==> Pouring libtool-2.4.2.mavericks.bottle.2.tar.gz

==> Caveats

In order to prevent conflicts with Apple's own libtool we have prepended a "g"

so, you have instead: glibtool and glibtoolize.

==> Summary

🍺  /usr/local/Cellar/libtool/2.4.2: 66 files, 2.2M

==> Installing php54 dependency: tidy

==> Downloading ftp://mirror.internode.on.net/pub/gentoo/distfiles/tidy-20090325.t

######################################################################## 100.0%

==> sh build/gnuauto/setup.sh

==> ./configure --prefix=/usr/local/Cellar/tidy/20090325 --mandir=/usr/local/Cella

==> make install

🍺  /usr/local/Cellar/tidy/20090325: 10 files, 936K, built in 21 seconds

==> Installing php54 dependency: unixodbc

==> Downloading http://www.unixodbc.org/unixODBC-2.3.2.tar.gz

######################################################################## 100.0%

==> ./configure --prefix=/usr/local/Cellar/unixodbc/2.3.2 --enable-gui=no

==> make install

🍺  /usr/local/Cellar/unixodbc/2.3.2: 31 files, 948K, built in 59 seconds

==> Installing php54 dependency: zlib

==> Downloading http://zlib.net/zlib-1.2.8.tar.gz

######################################################################## 100.0%

==> Patching

patching file configure

==> ./configure --prefix=/usr/local/Cellar/zlib/1.2.8

==> make install

==> Caveats

This formula is keg-only, so it was not symlinked into /usr/local.

 

Mac OS X already provides this software and installing another version in

parallel can cause all kinds of trouble.

 

Generally there are no consequences of this for you. If you build your

own software and it requires this formula, you'll need to add to your

build variables:

 

    LDFLAGS:  -L/usr/local/opt/zlib/lib

    CPPFLAGS: -I/usr/local/opt/zlib/include

 

==> Summary

🍺  /usr/local/Cellar/zlib/1.2.8: 9 files, 384K, built in 5 seconds

==> Installing php54

==> Downloading http://www.php.net/get/php-5.4.24.tar.bz2/from/this/mirror

######################################################################## 100.0%

==> ./configure --prefix=/usr/local/Cellar/php54/5.4.24 --localstatedir=/usr/local

==> make

==> make install

==> /usr/local/Cellar/php54/5.4.24/bin/pear config-set php_ini /usr/local/etc/php/

==> Caveats

To enable PHP in Apache add the following to httpd.conf and restart Apache:

    LoadModule php5_module    /usr/local/opt/php54/libexec/apache2/libphp5.so

 

The php.ini file can be found in:

    /usr/local/etc/php/5.4/php.ini

 

✩✩✩✩ PEAR ✩✩✩✩

 

If PEAR complains about permissions, 'fix' the default PEAR permissions and config:

    chmod -R ug+w /usr/local/Cellar/php54/5.4.24/lib/php

    pear config-set php_ini /usr/local/etc/php/5.4/php.ini

 

✩✩✩✩ Extensions ✩✩✩✩

 

If you are having issues with custom extension compiling, ensure that

you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH:

 

      PATH="/usr/local/bin:$PATH"

 

PHP54 Extensions will always be compiled against this PHP. Please install them

using --without-homebrew-php to enable compiling against system PHP.

 

✩✩✩✩ PHP CLI ✩✩✩✩

 

If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc, 

~/.zshrc, ~/.profile or your shell's equivalent configuration file:

 

      export PATH="$(brew --prefix josegonzalez/php/php54)/bin:$PATH"

==> Summary

🍺  /usr/local/Cellar/php54/5.4.24: 488 files, 37M, built in 4.3 minutes 

 


Apacheの設定

 Fraise を起動して、メニューから[ファイル]>[非表示ファイルを開く]を選択。

/usr/local/etc/apache2/httpd.conf を開きます。

 

#ServerName の変更

#ServerName www.example.com:80
ServerName www.example.com:80

 

# DocumentRoot の変更

# DocumentRoot "/usr/local/Cellar/httpd/2.2.26/share/apache2/htdocs"
DocumentRoot "/Users/username/Sites"

 

# DocumentRoot の設定を追加
# .htaccessでURLのoverwriteを許可とCGI,SSIの許可

<Directory "/Users/username/Sites">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

 

# indexファイルにindex.phpを使えるように修正 

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

 

# php5 のモジュールの追加

LoadModule php5_module    /usr/local/opt/php54/libexec/apache2/libphp5.so

 

# .pl で perl を実行できるように MIME タイプを修正 ( IfModule mime_module 内 )

#AddHandler cgi-script .cgi
AddHandler cgi-script .cgi .pl

 

# .phpphp を実行できるように MIME タイプの追加 ( IfModule mime_module 内 )

AddType application/x-httpd-php .php

 

 以上の編集が終わったら保存します。

 

ターミナルから

$ sudo apachectl restart

Apacheを再起動。

 

ブラウザでlocalhostにアクセスして、/Users/username/Sites/index.html の内容が表示されればOK!

 

info.php ファイルを作成し、

<?php phpinfo(); ?>

と記述して保存。

localhost/info.php にアクセスしてPHP情報が表示されるか確認。

 

info.cgi ファイルを作成し、

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<TABLE BORDER=\"1\">\n";
for $key (sort(keys(%ENV))) {
    print "<TR><TD><TT>$key</TT></TD><TD><TT>$ENV{$key}</TT></TD></TR>\n";
}
print "</TABLE>\n";
exit;

 

と記述して保存。

localhost/info.cgi にアクセスしてperl情報が表示されるか確認。