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

概要

それではmacPCにwebサーバーの構築をしていきましょう。

目次

Apacheのインストール

apacheの確認

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

$ apachectl -v

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

brewの準備

Homebrewでapacheの最新版をインストールする準備をします。

外部formula(Rubyで書かれたビルド手順のスクリプト)の「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

brewでapacheをインストール

brewで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
$ source .bash_profile

apacheの確認

$ apachectl -v

Server version: Apache/2.2.26 (Unix)
Server built:   Jan 22 2014 20:08:10 

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

apacheを起動

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

$ 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
・
・
・
php55-phalcon   php55-uploadprogress      phpunit
php55-proctitle   php55-uuid
marcqualie/nginx/php-session-nginx-module 

PHP5.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.
・
・   
・
==> Summary
🍺  /usr/local/Cellar/php54/5.4.24: 488 files, 37M, built in 4.3 minutes 

Apacheの設定

httpd.confの編集

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を使えるように修正

&t;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

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

AddType application/x-httpd-php .php

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

apache再起動

$ sudo apachectl restart

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

インストールされたPHPの確認

info.phpファイルを作成し、以下の内容を記述し、/Users/username/Sites/に保存。

<?php phpinfo(); ?>

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

インストールされているperlの確認

info.cgiファイルを作成し、以下の内容を記述し、/Users/username/Sites/に保存。

#!/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情報が表示されるか確認。

投稿日:

ページのトップへ戻る