HomebrewやPHPを簡単に再インストールするためのシェルスクリプト


MacのHomebrewは便利なんだけど、使っているうちに環境が汚れてくるのか、だんだんパッケージのインストールがうまくいかないことが起きてくる。

また、家と会社とモバイルの3つの環境で、バージョンが合わなくてアップデートもうまくいかないことがある。

そういうときは、諦めてアッサリとイチからHomebrewを入れ直した方が後腐れない。だけど、いちいちコマンドを打つのも面倒なのでシェルスクリプト化した。

ついでにhomebrew-phpのインストールもシェルスクリプト化。
いまどき、brew bundleを使うのが正しいのかもしれないけど、シェルスクリプトにしとくと、メッセージを表示したり複数パターン作れたり、いろいろ便利なので。

**まず、homebrewの（比較的）確実なアンインストール。**

```uninstall_homebrew.sh

#!/bin/sh

echo "cd `brew --prefix`"
cd `brew --prefix`

echo "rm -rf Cellar"
rm -rf Cellar

echo "brew prune"
brew prune

echo "rm -rf Library .git .gitignore bin/brew README.md share/man/man1/brew"
rm -rf Library .git .gitignore bin/brew README.md share/man/man1/brew

echo "rm -rf ~/Library/Caches/Homebrew"
rm -rf ~/Library/Caches/Homebrew

echo "rm -rf ~/Library/Logs/Homebrew"
rm -rf ~/Library/Logs/Homebrew

echo "rm -rf /Library/Caches/Homebrew"
rm -rf /Library/Caches/Homebrew

echo "rm -rf /usr/local/Cellar /usr/local/.git"
rm -rf /usr/local/Cellar /usr/local/.git

```

**次にhomebrewの再インストール**

```install_homebrew.sh

#!/bin/sh
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
brew update
brew doctor
brew -v

```

**MySQLのインストール**

```install_mysql.sh

#!/bin/sh
echo "brew install pkg-config"
brew install pkg-config

echo "mv /usr/local/var/mysql /usr/local/var/mysql.old"
mv /usr/local/var/mysql /usr/local/var/mysql.old

echo "brew install mysql"
brew install mysql

echo "unset TMPDIR"
unset TMPDIR

echo "mkdir /usr/local/var"
mkdir /usr/local/var

echo "mkdir /usr/local/var/mysql"
mkdir /usr/local/var/mysql

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

echo "sudo cp /usr/local/opt/mysql/my-new.cnf /etc/my.cnf"
sudo cp /usr/local/opt/mysql/my-new.cnf /etc/my.cnf

echo "$ mysql.server start"
mysql.server start

echo "$ mysql.server stop"
mysql.server stop

```

**全文検索mroonga（MySQL+Groonga）のインストール（お好みで）**
（実は最近使ってないけど）

```install_mroonga.sh

#!/bin/sh

echo "rm -fr /usr/local/share/doc/mroonga"
rm -fr /usr/local/share/doc/mroonga

echo "mysql.server start"
mysql.server start

echo "brew install https://raw.github.com/mroonga/homebrew/master/mroonga.rb --use-homebrew-mysql"
brew install https://raw.github.com/mroonga/homebrew/master/mroonga.rb --use-homebrew-mysql

echo "mysql.server stop"
mysql.server stop

echo "mysql.server start"
mysql.server start

echo "mysql -u root -e'SHOW PLUGINS;'"
mysql -u root -e'SHOW PLUGINS;'

```

**memcachedのインストール**
（実は今は最初の3行だけでいいのかも。世の中の変化について行けてない…）


```install_memcached.sh

#!/bin/sh

echo "brew install memcached"
brew install memcached

echo "mkdir -p ~/Library/LaunchAgents"
mkdir -p ~/Library/LaunchAgents

echo "cp /usr/local/Cellar/memcached/1.4.20//homebrew.mxcl.memcached.plist ~/Library/LaunchAgents/"
cp /usr/local/Cellar/memcached/1.4.20//homebrew.mxcl.memcached.plist ~/Library/LaunchAgents/

echo "launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist

```


**PHP5.3のインストール**
peclでmemcacheとapcもインストール。
pecl memcachedのほうが必要な人は書き換えてください。

```install_php53.sh

#!/bin/sh

echo "rm ~/.pearrc"
rm ~/.pearrc

echo "brew tap homebrew/dupes"
brew tap homebrew/dupes

echo "brew tap homebrew/homebrew-php"
brew tap homebrew/homebrew-php

#echo "brew tap josegonzalez/homebrew-php"
#brew tap josegonzalez/homebrew-php

echo "brew tap homebrew/versions"
brew tap homebrew/versions

echo "brew install libtool zlib jpeg libpng freetype gettext icu4c mcrypt autoconf"
brew install libtool zlib jpeg libpng freetype gettext icu4c mcrypt autoconf

echo "brew install php53-intl"
brew install php53-intl

echo "brew install php53 --with-pgsql --with-intl --with-thread-safety"
brew install php53 --with-pgsql --with-intl --with-thread-safety

touch $(brew --prefix php53)/lib/php/.lock && chmod 0644 $(brew --prefix php53)/lib/php/.lock

echo "pear upgrade"
pear upgrade

echo "pecl install memcache"
pecl install memcache

echo "pecl install apc"
pecl install apc

echo "ln -sf `brew list php53 | grep libphp` /usr/local/lib/libphp5.so"
ln -sf `brew list php53 | grep libphp` /usr/local/lib/libphp5.so

```

**PHP5.5のインストール**
こちらはopcacheとapcuをインストールしてます。
（まだ、ほとんど使ってないけど）

```install_php55.sh

#!/bin/sh

echo "rm ~/.pearrc"
rm ~/.pearrc

echo "brew tap homebrew/dupes"
brew tap homebrew/dupes

echo "brew tap homebrew/homebrew-php"
brew tap homebrew/homebrew-php

echo "brew tap homebrew/versions"
brew tap homebrew/versions

echo "brew install libtool zlib jpeg libpng freetype gettext icu4c mcrypt autoconf"
brew install libtool zlib jpeg libpng freetype gettext icu4c mcrypt autoconf

echo "brew install php55-intl"
brew install php55-intl

echo "brew install php55 --with-pgsql --with-intl --with-thread-safety"
brew install php55 --with-pgsql --with-intl --with-thread-safety --with-phpdbg

touch $(brew --prefix php55)/lib/php/.lock && chmod 0644 $(brew --prefix php55)/lib/php/.lock

echo "brew unlink php53"
brew unlink php53

echo "brew link php55"
brew link php55

echo "pear upgrade"
pear upgrade

echo "pecl install memcache"
pecl install memcache

echo "brew install php55-opcache"
brew install php55-opcache

echo "brew install php55-apcu"
brew install php55-apcu

echo "ln -sf `brew list php55 | grep libphp` /usr/local/lib/libphp5.so"
ln -sf `brew list php55 | grep libphp` /usr/local/lib/libphp5.so

```

上記のPHP5.3とPHP5.5を切り替えるには、httpd.confのmod_phpの読み込みを以下の様に書いておく

```httpd.conf

LoadModule php5_module     /usr/local/lib/libphp5.so

```

**PHP5.3とPHP5.5の切り替え**
（Githubで5.3と5.4の切り替え用として紹介されてたのを拾った）

```php-switch.sh

#!/bin/bash
# 
# php switch for homebrew
#
VERSION_FILE="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/php-switch.version"
 
if [[ ! -f $VERSION_FILE ]]; then
    echo "5.5" > $VERSION_FILE
fi
 
version=`cat $VERSION_FILE`
 
if [[ $version == 5.3 ]]; then
    ln -sf `brew list php55 | grep libphp` /usr/local/lib/libphp5.so
    echo "Switched to 5.5"
    echo `brew info php55 | head -1`
    echo "5.5" > $VERSION_FILE
    brew unlink php53
    brew link php55
     
else
    ln -sf `brew list php53 | grep libphp` /usr/local/lib/libphp5.so
    echo "Switched to 5.3"
    echo `brew info php53 | head -1`
    echo "5.3" > $VERSION_FILE
    brew unlink php55
    brew link php53
fi
 
sudo apachectl restart

```

以上。個人的メモなので、無保証です。