1.homebrew
homebrew 是mac环境下yum apt-get的类似物,在mac下相对也比Linux下的安装工具更易管理。
官网:brew.sh
安装:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
详细信息以及brew的基本操作见官网。
2.安装nginx
安装好homebrew以后,使用brew安装:
brew install nginx
默认被安装到的目录为:
/usr/local/Cellar/nginx/
配置文件的目录为:
/usr/local/etc/nginx/
该目录下需要关注的配置文件:
nginx.conffastcgi.conf fastcgi_params
nginx安装完成,可通过nginx -h查看说明。其中 reopen是重启日志,reload用于修改配置等操作之后的重载
3.取消Apache
由于mac默认安装了Apache,80端口是被占用了的,需要取消Apache的开机启动:
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist
重新需要启动Apache的话 unload改为load即可。
4.安装php
mac预装了php,但是没有安装一些扩展,本人也有强迫症一定要自己装。。
使用brew tap安装brew的第三方程序包,选择josegonalez:
brew tap homebrew/dupesbrew tap josegonzalez/homebrew-php
然后搜索php的版本(选择了最近的php56,php55以后内嵌了php-fpm):
brew search php
然后查看安装选项:
brew options php56
安装php(内嵌php-fpm,无扩展):
brew install php56
安装扩展的话就按需来装吧,当然有些会报错,需要提前装其他的模块如curl等,暂时还没有装。安装特定扩展:
brew install php56 --with-xxx
安装完成可以检验版本:
php -v php-fpm -v
很奇怪的是php-fpm -t ,用于test php-fpm的运行,但是这句话默认的查找的路径是:
/private/etc/php-fpm.conf
但是实际上的.conf文件在
/usr/local/etc/php/5.6/php-fpm.conf
所以会报错,于是两个解决办法(1)将文件添加到该目录下(并不推荐,还有默认的目录错误也如此操作)(2)使用参数:
php-fpm --fpm-config /usr/local/etc/php/5.6/php-fpm.conf --prefix /usr/local/var -t(这也是homebrew.mxcl.php56..plist的开机启动的方法)
但是要启动php-fpm并不用这么麻烦,只需要:
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist(.plist文件默认在/usr/local/Cellar/php56/5.6.7/下,将其复制到前面的目录下成为开机启动)
如果出现
ERROR:unable to bind listening socket for address ' 127.0.0.1:9000'
则关闭php-fpm进程,并重启:
killall php-fpm
launchstl .......
检查自己的localhost:9000端口是否在监听:
lsof -i:9000
另外,因为重装php,终端调用的时候系统还是用以前的php解析,需要在~/.bashrc (没有则创建)中修改
export PATH="$(brew --prefix php56)/bin:$PATH"
[html]
[code]
source ./.profile
5. 使nginx支持php
打开nginx的配置文件
vim /usr/local/etc/nginx/nginx.conf
user nobody; //可以根据以后需要自行设置
在
http{
server{
location /{
}
...
location ~ \.php${
}
}
...
}
中,默认的根目录和默认的起始页在location/下,为
root html;
index index.html index.htm
实际详细的根目录为
/usr/local/Cellar/nginx/1.6.2/html
且由于是从外部继承缺省值,所以应该将root 和index的定义都放到外层的server下,并且root使用绝对路径。然后添上index.php。
http{
server{
root /usr/local/Cellar/nginx/1.6.2/html;
index index.html index htm index.php;
location\{
...
}
}
}
缺省时location ~\.php{}是注释掉的,需要去掉注释,并且修改里面的内容。初始:
location ~\.php${
proxy_pass http:127.0.0.1;
}
location ~\.php${
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
其中proxy_pass 以后根据需要配置,第一个函数可都注释掉(初始也是注释掉的)。第二个函数(去掉注释),root改为绝对路径
root /usr/local/Cellar/nginx/1.6.2/html;
关于fastcgi有两种改法:
1fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /usr/local/etc/nginx/fastcgi_params;
2include /usr/local/etc/nginx/fastcgi.conf
因为有人会吧$document_root变成硬编码,为了规范用法所以新加了fastcgi.conf文件
diff /usr/local/etc/nginx/fastcgi.conf /usr/local/etc/nginx/fastcgi_params
结果:
2d1
< fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
$document_root中就是root的路径,所以root中最好写绝对路径。
在定义的root路径下新建一个index.php:
vim /usr/local/Cellar/nginx/1.6.2/html/index.php
编辑为:
<html>
<body>
<?php
phpinfo();
?>
</body>
<html>
如果更改了默认的路径可能会报403的错误,这是因为启动nginx的用户和文件夹的所属用户不是同一个,这个可以按需求更改。
确认php-fpm工作(若未工作请往上翻),重启nginx
lsof -i:9000
nginx -s reload
在浏览器输入localhost:8080/index.php
ps:如果修改了php.ini的信息,在PHP5.3之后,不仅需要重启web服务器,还需要先重启php-fpm
6. 安装MySQL
brew install mysql
这时候运行MySQL会报错,需要再做以下设置:
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
现在就可以运行了,启动MySQL
mysql.server start
如果没有unset TMPDIR ,mysql start会报can't connect to local mysql server through socket '/tmp/mysql.sock'的错误
接着进入安全的初始化设置,运行:
/usr/local/Cellar/mysql/5.6.24/bin/mysql_secure_installation
逐个选择就可以了。
登陆mysql:
mysql -u root -p
7. 设置开机启动
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/nginx/1.6.2/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
cp /usr/local/Cellar/php56/5.6.7/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
cp /usr/local/Cellar/mysql/5.6.24/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
文章涉及的版本号根据自身情况而定