Mac OS X では多くの UNIX ソフトウェアが簡単に導入できます。Apache や PHP, Perl 等はあらためてインストールすることなく利用できます。ローカル環境で Web サーバーを構築するのに必要なことは、Apache の設定と MySQL のインストールだけです。ここでは El Capitan built-in の Apache, PHP を使い MAMP 環境を整えてみたいと思います。
Apache
PHP
最初に Apache の /etc/apache2/httpd.conf
のバックアップを取っておきます。
$ cd /etc/apache2/ $ sudo cp httpd.conf httpd.conf.bak
httpd.conf
の以下の一行をアンコメントします。
$ sudo vi httpd.conf LoadModule php5_module libexec/apache2/libphp5.so
私は CGI モジュールもついでにアンコメントしました。
LoadModule cgi_module modules/mod_cgi.so
Apache をリスタートします。
$ sudo apachectl restart
Mac OS X でのデフォルトの DocumentRoot
は /Library/WebServer/Documents
です。 DocumentRoot
に phpinfo()
ページを作成してみます。
以下の一行の phpinfo.php
を /Library/WebServer/Documents/
に作成します。
$ cd /Library/WebServer/Documents/ $ sudo vi phpinfo.php <?php phpinfo(); ?>
http://localhost/phpinfo.php にアクセスしてみます。
MySQL
私は Homebrew でインストールしました。
$ brew install mysql $ mysql -V mysql Ver 14.14 Distrib 5.7.10, for osx10.11 (x86_64) using EditLine wrapper
~/.bashrc
に以下を加えておきました。
$ vi ~/.bashrc export PATH=/usr/local/mysql/bin:$PATH
サーバーをスタートしてみます。
$ mysql.server start
mysql_secure_installation
を実行しておきます。
$ mysql_secure_installation
パスワードの設定等、質問に答えていきます。
$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.7.10 Homebrew Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
となれば接続成功です。
サーバーをストップするには以下を実行します。
$ mysql.server stop
自動起動するには以下を実行します。
$ mkdir ~/Library/LaunchAgents $ ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents $ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
次のように /var/mysql
に mysql.sock
のシンボリックリンクを作成しておきました。
$ sudo mkdir /var/mysql $ sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
my.cnf
は /usr/local/etc
に配置することに。
$ mysql --help | grep .cnf
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf
$ cp /usr/local/Cellar/mysql/5.7.10/support-files/my-default.cnf /usr/local/etc/my.cnf
MySQL の文字コードは UTF-8 になるように、また、 validate_password_policy
は ‘LOW’ に設定しておきました (最低8文字)。
$ cat /usr/local/etc/my.cnf [mysqld] port = 3306 socket = /tmp/mysql.sock validate_password_policy=LOW collation-server = utf8_unicode_ci init-connect='SET NAMES utf8' character-set-server = utf8 skip-character-set-client-handshake
文字コード:
mysql> show variables like 'char%';
Variable_name | Value |
character_set_client character_set_connection character_set_database character_set_filesystem character_set_results character_set_server character_set_system character_sets_dir |
utf8 utf8 utf8 binary utf8 utf8 utf8 /usr/local/Cellar/mysql/5.7.10/share/mysql/charsets/ |
validate_password:
mysql> SHOW VARIABLES LIKE 'validate_password%';
Variable_name | Value |
validate_password_dictionary_file validate_password_length validate_password_mixed_case_count validate_password_number_count validate_password_policy validate_password_special_char_count |
8 1 1 LOW 1 |
再起動時に Error
再起動時をかけたところ、以下のエラーが出ました。
Starting MySQL
... ERROR! The server quit without updating PID file (/usr/local/var/mysql/xxxx.pid).
/usr/local/var/mysql
の権限を以下のように変更してみたところ、このエラーは回避できました。
$ sudo chown -R _mysql:_mysql /usr/local/var/mysql $ sudo chmod -R o+rwx /usr/local/var/mysql/ $ mysql.server start Starting MySQL SUCCESS!
“Mac OS X El Capitan での Apache + MySQL + PHP の設定” への2件の返信