Skip to content

Instantly share code, notes, and snippets.

@gaspanik
Created September 30, 2012 14:47
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaspanik/3806975 to your computer and use it in GitHub Desktop.
Save gaspanik/3806975 to your computer and use it in GitHub Desktop.
Install: AMP on 10.8.x

Apache+PHP+MySQL(Homebrew) on Mountain Lion

Mountain Lionの中に入ってるApacheとPHPを有効化しつつ、Homebrewを使ってMySQLをインストールするまで

※XcodeとCommand Line Toolsぐらいは入れておかないとだめなはず

Mountain Lion 内のApacheを起動

システム環境設定から消えてるので以下のコマンドでApache起動

sudo apachectl start

システム環境設定にスイッチを入れたかったらこれを使えばいいみたい

http://amz.clickontyler.com/WebSharing.zip

バーチャルホスト、PHPの有効化

「/etc/apache2/httpd.conf」いじくってアップデートとかで初期状態に戻るとかってのもなんなので、「/etc/apache2/other」の中にシンボリックリンク作って編集しやすいように

※ここでは適当にホームディレクトリ以下に「Sites」を作って設定ファイルを置いてます。気になる方は別の場所においてくださいな

sudo ln -s ~/Sites/httpd-vhosts.conf /etc/apache2/other

<VirtualHost *:80>

ServerName ホスト名

DocumentRoot "/Users/ユーザ名/Sites/ホスト名"

DirectoryIndex index.html index.php

CustomLog "/Users/ユーザ名/Sites/logs/ホスト名-access_log" combined

ErrorLog "/Users/ユーザ名/Sites/logs/ホスト名-error_log"

</VirtualHost>

まぁ、こんな感じで。

sudo vi /etc/hosts

ホストを追加したら「/etc/hosts」にも書き加える

127.0.0.1 localhost ホスト名(←半角スペース入れて名前を列挙)

ホスト名が「mba.local」だったらこんな感じ

127.0.0.1 localhost mba.local

で、PHPのモジュールを有効に(面倒だったら、直接httpd.confの該当行をコメントアウト)

sudo ln -s ~/Sites/httpd-loadmodules.conf /etc/apache2/other

LoadModule php5_module libexec/apache2/libphp5.so

終わったら、再起動

sudo apachectl restart

PEARのインストール

PEARを有効にしてあげる

sudo /usr/bin/php /usr/lib/php/install-pear-nozlib.phar

[PEAR] Archive_Tar - installed: 1.3.7

[PEAR] Console_Getopt - installed: 1.3.0

[PEAR] Structures_Graph- installed: 1.0.4

[PEAR] XML_Util - installed: 1.2.1

[PEAR] PEAR - installed: 1.9.4

Wrote PEAR system config file at: /private/etc/pear.conf

You may want to add: /usr/lib/php/pear to your php.ini include_path

こんなのが出て終了。あとはパス「/usr/lib/php/pear」を「php.ini」の「include_path」のとこに追加

PHPの初期設定(php.ini)

「/etc/php.ini」を編集。「/usr/lib/php/pear」をinclude_pathに追加したり、MySQLのソケットのパスをかえたりする。特にMySQLのソケットのとこは間違えると、DBに接続できないとかいわれるので注意

sudo cp -a /etc/php.ini.default /etc/php.ini

まずは、デフォルトのやつをコピーして「etc/php.ini」に

sudo chmod 644 /etc/php.ini

パーミッションとか変更してみたりで編集

sudo vi /etc/php.ini

mysql.default_socketのパスが、HomebrewのMySQLとあわないので変えとく

; mysql.default_socket = /var/mysql/mysql.sock

mysql.default_socket = /tmp/mysql.sock

他はお好みに合わせて編集して保存。で、Apacheを再起動

sudo apachectl restart

次はHomebrew使ったMySQLのインストール

Homebrewのインストール

ターミナルから以下のコマンドを実行するだけ(http://mxcl.github.com/homebrew/)

ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)

インストールは「brew install パッケージ名」、情報見るなら「brew info パッケージ名」、アンインストールは「brew uninstall パッケージ名」とか

MySQLのインストール(※MariaDBも一緒)

Homebrew使ってインストール

brew update

念のためアップデートしてから、以下を実行

brew install mysql

インストールが終われば、下のがわらわら出てくるので、基本この流れでコマンドを入れていく

--

Set up databases to run AS YOUR USER ACCOUNT with:

unset TMPDIR mysql_install_db --verbose --user=whoami --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

To set up base tables in another folder, or use a different user to run

mysqld, view the help for mysqld_install_db:

mysql_install_db --help

and view the MySQL documentation:

To run as, for instance, user "mysql", you may need to sudo:

sudo mysql_install_db ...options...

Start mysqld manually with:

mysql.server start

Note: if this fails, you probably forgot to run the first two steps up above

A "/etc/my.cnf" from another install may interfere with a Homebrew-built server starting up correctly.

To connect:

mysql -uroot

To launch on startup:

  • if this is your first install:

mkdir -p ~/Library/LaunchAgents cp /usr/local/Cellar/mysql/5.5.25a/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

  • if this is an upgrade and you already have the homebrew.mxcl.mysql.plist loaded:

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist cp /usr/local/Cellar/mysql/5.5.25a/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

You may also need to edit the plist to use the correct "UserName".

==> Summary /usr/local/Cellar/mysql/5.5.25a: 6382 files, 222M

--

最初はこれ

unset TMPDIR

で、これを打つ

mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

これでシステムテーブルが追加される。またわらわら以下が出るのでメモ

--

Installing MySQL system tables... OK Filling help tables... OK

To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands:

/usr/local/Cellar/mysql/5.5.25a/bin/mysqladmin -u root password 'new-password' /usr/local/Cellar/mysql/5.5.25a/bin/mysqladmin -u root -h マシン名 password 'new-password'

Alternatively you can run:

/usr/local/Cellar/mysql/5.5.25a/bin/mysql_secure_installation

which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

cd /usr/local/Cellar/mysql/5.5.25a ; /usr/local/Cellar/mysql/5.5.25a/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd /usr/local/Cellar/mysql/5.5.25a/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/local/Cellar/mysql/5.5.25a/scripts/mysqlbug script!

--

MySQLの起動と初期設定

とりあえず起動してみる(※してるかもw)

mysql.server start

パスワードを設定しておく。'new-password' の中は書き換える

/usr/local/Cellar/mysql/5.5.25a/bin/mysqladmin -u root password 'new-password'

マシン名のとこは、自分のインストール時に表示されたヤツにかえて実行

/usr/local/Cellar/mysql/5.5.25a/bin/mysqladmin -u root -h マシン名 password 'new-password'

いったん止めて、初期設定ファイルを用意する。

mysql.server stop

HomebrewのCellarディレクトリの中にデフォルトの「my.cnf」がいくつかあるので、それを「/etc/my.cnf」にコピーするとか適当に

sudo cp -a /usr/local/Cellar/mysql/5.5.25a/support-files/my-small.cnf /etc/my.cnf

書き込みのパーミッションがないとか言われるだろうから先に変える

sudo chmod 777 /etc/my.cnf

で、編集

sudo vi /etc/my.cnf

「/etc/my.cnf」にいれておくもの

まぁ、この辺は適当に

[client]

default-character-set = utf8

[mysqld]

max_allowed_packet = 64M character-set-server = utf8

[mysql]

default-character-set = utf8

パーミッションは適当に戻しておく

sudo chmod 644 /etc/my.cnf

で、起動してみる

mysql.server start

動いていたら、ログインとかしてみる

mysql -u root -p

パスワードを聞かれるので入力するとログインできる

mysql> ここにカーソルがでる

で、終了したかったら

mysql> quit

これで

データベース作るなら、mysqladmin使えば簡単

mysqladmin create DB名 -u root -p

とか

心配性のあなたに

これもやっておくといいかも

sudo /usr/local/Cellar/mysql/5.5.25a/bin/mysql_secure_installation

MySQLの自動起動

以下のコマンドを実行すればよい。LaunchAgentsディレクトリがなかったら先に作っておく。それはインストール時にわらわら出るとこに書いてある

cp /usr/local/Cellar/mysql/5.5.25a/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/

コピーしたら、それをロードするように

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

以上でおしまい

どうしてもうまくいかないときは入れ直す

アンインストールの仕方はここに書いてある

http://www.sitepoint.com/forums/showthread.php?859646-Can-t-connect-to-local-MySQL-server-through-socket-tmp-mysql-sock-(2)-(Mysql-Erro

@gaspanik
Copy link
Author

MariaDBもMySQLとおなじ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment