翻译:http://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/
最近Ubuntu用着很不爽,首先是输入法很难用,所说搜狗发布了Ubuntu14.04的输入法,但是远远没有Win下的输入法好用。其次是没有qq,在公司喝同事交流很困难,虽说网页qq也可以聊天,但是传个文件就不行了。缺少很多应用,用Web版的用很难用。总之Ubuntu就是不爽。于是把家里尘封的Mac Mini搬到公司爽爽的写程序。
首先我把Mac升级到Mac10.10.1 OS X Yosemite(在App Store里可以免费升级)。然后Xcode也要升级到最新版Version6.1,最后安装(或更新) Xcode Command Line Tools.
安装Xcode Command Line Tools
打开终端,输入以下命令,回车,会弹出一个框,点击安装(或Install)继续。
xcode-select --install
安装完成后,打开Xcode,进入Preferences->Locations,查看Xcode Command Line Tools是否是最新版。
我的是这样的
![Bildschirmfoto-2014-10-20-um-11-27-56](http://www.sundabao.com/wp-content/uploads/2014/12/Bildschirmfoto-2014-10-20-um-11-27-56.png)
确认你用的是Xcode 6.1!然后安装homebrew
Homebrew
Mac下的Homebrew相当于Linux下的apt-get、yum,可以获得最新版的各种安装包。
首先,你要Xquartz
curl http://xquartz-dl.macosforge.org/SL/XQuartz-2.7.7.dmg -o /tmp/XQuartz.dmg
open /tmp/XQuartz.dmg
然后用以下命令安装homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安装完成后,运行以下命令检查是否安装成功
brew doctor
然后更新、升级下brew源
brew update && brew upgrade
PHP-FPM
因为brew默认不包含php-fpm,所以要先添加一个
brew tap homebrew/dupes
brew tap homebrew/php
然后运行以下命令安装php、php-fpm,可能会花较长时间。
brew install --without-apache --with-fpm --with-mysql php55
设置PHP CLI
如果你想在命令行下运行php,你需要更改下bash shell下的环境变量
# If you use Bash
echo 'export PATH="$(brew --prefix homebrew/php/php55)/sbin:$PATH"' >> ~/.bash_profile && . ~/.bash_profile
# If you use ZSH
echo 'export PATH="$(brew --prefix homebrew/php/php55)/sbin:$PATH"' >> ~/.zshrc && . ~/.zshrc
让php自动开启
mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php55/homebrew.mxcl.php55.plist ~/Library/LaunchAgents/
运行php-fpm
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist
确认php-fpm监听9000端口
lsof -Pni4 | grep LISTEN | grep php
输出如下
php-fpm 69659 frdmn 6u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 69660 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 69661 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 69662 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN)
Mysql
运行以下命令安装Mysql
brew install mysql
设置自动重启
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
开启数据库服务
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
安全设置
运行以下命令删除匿名用户,并且禁止root远程登录。
mysql_secure_installation
> Enter current password for root (enter for none):
如果没有设置root密码,直接回车。
> Change the root password? [Y/n]
回车,输入你的root密码。
> Remove anonymous users? [Y/n]
直接回车。
> Disallow root login remotely? [Y/n]
直接回车。
> Remove test database and access to it? [Y/n]
直接回车。
> Reload privilege tables now? [Y/n]
直接回车,刷新权限。
测试连接数据库
mysql -u root -p
输入root密码:
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
退出
/q
Bye
phpMyAdmin
首先需要安装autoconf
brew install autoconf
设置$PHP_AUTOCONF
# If you use Bash
echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.bash_profile && . ~/.bash_profile
# If you use ZSH
echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.zshrc && . ~/.zshrc
安装phpMyAdmin
brew install phpmyadmin
Nginx
安装Nginx
brew install nginx
设置自启
sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
测试Web服务器
启动nginx
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
现在默认监听8080端口,运行以下命令测试
curl -IL http://127.0.0.1:8080
输出:
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 19 Oct 2014 19:07:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 19 Oct 2014 19:01:32 GMT
Connection: keep-alive
ETag: “5444dea7-264”
Accept-Ranges: bytes
停止Nginx服务
sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
配置
创建nginx文件夹及配置文件
mkdir -p /usr/local/etc/nginx/logs
mkdir -p /usr/local/etc/nginx/sites-available
mkdir -p /usr/local/etc/nginx/sites-enabled
mkdir -p /usr/local/etc/nginx/conf.d
mkdir -p /usr/local/etc/nginx/ssl
sudo mkdir -p /var/www
sudo chown :staff /var/www
sudo chmod 775 /var/www
Load PHP-Fpm
curl -L https://gist.github.com/frdmn/7853158/raw/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm
Create default virtual hosts
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default -o /usr/local/etc/nginx/sites-available/default
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default-ssl -o /usr/local/etc/nginx/sites-available/default-ssl
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_phpmyadmin -o /usr/local/etc/nginx/sites-available/phpmyadmin
设置 SSL
创建文件夹存放ssl证书和ssl key
mkdir -p /usr/local/etc/nginx/ssl
生成4096bit key和证书
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=localhost" -keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt
Enable virtual hosts
ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default
ln -sfv /usr/local/etc/nginx/sites-available/default-ssl /usr/local/etc/nginx/sites-enabled/default-ssl
ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin
开启Nginx
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
Test
http://localhost → "Nginx works" page
http://localhost/info → phpinfo()
http://localhost/nope → " Not Found" page
https://localhost:443 → "Nginx works" page (SSL)
https://localhost:443/info → phpinfo() (SSL)
https://localhost:443/nope → "Not Found" page (SSL)
https://localhost:306 → phpMyAdmin (SSL)
配置命令文件
curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.bash_aliases
cat /tmp/.bash_aliases >> ~/.bash_aliases
# If you use Bash
echo "source ~/.bash_aliases" >> ~/.bash_profile
# If you use ZSH
echo "source ~/.bash_aliases" >> ~/.zshrc
Commands
Nginx
nginx.start
nginx.stop
nginx.restart
快速tail日志文件
nginx.logs.access
nginx.logs.default.access
nginx.logs.phpmyadmin.access
nginx.logs.default-ssl.access
nginx.logs.error
nginx.logs.phpmyadmin.error
查看Nginx配置
sudo nginx -t
PHP-FPM
php-fpm.start
php-fpm.stop
php-fpm.restart
查看PHP—FPM配置文件
php-fpm -t
Mysql
mysql.start
mysql.stop
mysql.restart