最近有点闲钱买Mac了,便果断换了个rmbp当工作本,折腾了小半天,终于把
Mac下PHP的开发环境搭好了,用的是nginx+php-fpm,特地记录一下如何在Mac(版本10.11.3)下从零开始安装PHP开发环境
Mac下有必要重新搭建PHP开发环境么
有必要
Mac自带的PHP不支持PHP扩展
Mac自带apache不能优雅的install/remove
Mac自带的PHP版本往往不是最新/最流行的
安装Homebrew
homebrew是什么
homebrew是OS X 不可或缺的套件管理器,类似于ubuntu下apt-get,Red hat的yum,官网:http://brew.sh/index_zh-cn.html
homebrew安装
安装非常简单,打开终端粘贴以下代码即可:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安装依赖
brew install FORMULA
homebrew常用命令
搜索软件:brew search 软件名,如brew search php56
安装软件:brew install 软件名,如brew install php56
卸载软件:brew remove 软件名,如brew remove php56
重新安装: brew reinstall 软件名,如brew reinstall PHP56
homebrew一般流程
增加一个程序源
brew tap homebrew/php
更新程序源
brew update
安装程序包
brew install php56
查看配置
brew config
可以看到程序包默认安装在/usr/local/Cellar下
安装PHP5.6
这里使用的是php-fpm的安装方式,详细的安装说明可以见:homebrew-php
加入官方源
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/php
安装PHP
默认安装会运行在apache下,而我们要使它运行在php-fpm下,所以要增加参数--with-fpm --without-apache
,查看所有安装参数可以输入brew option php56
进行查看,参考安装指令:
brew install php56 \
--without-snmp \
--without-apache \
--with-debug \
--with-fpm \
--with-intl \
--with-homebrew-curl \
--with-homebrew-libxslt \
--with-homebrew-openssl \
--with-imap \
--with-mysql \
--with-tidy
安装完后有一大堆的提示说明,首先由于Mac自带PHP,所以要修改系统路径,优先允许brew安装的版本
echo 'export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"' >> ~/.bashrc
###安装PHP扩展
php扩展可以用brew search php56
查看,参考安装指令:
brew install php56-gearman php56-msgpack php56-memcache php56-memcached php56-mongo php56-redis php-zookeeper php56-xdebug
测试php
php -v
如果出现扩展启动错误,可以使用下面命令重新安装
brew reinstall php56-gearman --build-from-source
附:测试Mac自带PHP的命令是:/usr/bin/php -v
运行php-fpm
测试php-fpm
php-fpm -v
附:测试Mac自带的php-fpm命令:/usr/bin/php-fpm -v
运行php-fpm
sudo php-fpm -D
关闭php-fpm
sudo killall php-fpm
查看php-fpm运行状态
lsof -Pni4 | grep LISTEN | grep php
php-fpm开机启动
ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
可能出现的问题一
ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory
ERROR: failed to load configuration file '/private/etc/php-fpm.conf'
ERROR: FPM initialization failed
解决方法
sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
可能出现的问题二
ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)
ERROR: failed to post process the configuration
ERROR: FPM initialization failed
解决方法
sudo vi /private/etc/php-fpm.conf
#找到`error_log`项,在下面添加:
error_log = /usr/local/var/log/php-fpm.log
pid = /usr/local/var/run/php-fpm.pid
安装nginx
基础安装
安装命令:
brew install nginx
启动关闭命令:
#测试配置是否有语法错误
nginx -t
#打开 nginx
sudo nginx
#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit
开机启动:
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
nginx监听80端口:
sudo chown root:wheel /usr/local/Cellar/nginx/1.6.0_1/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.6.0_1/bin/nginx
配置文件
按ubuntu文件格式来储存配置文件,方便配置多个域名:
mkdir -p /usr/local/var/logs/nginx
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
编辑Nginx全局配置
vim /usr/local/etc/nginx/nginx.conf
#添加以下内容
worker_processes 1;
error_log /usr/local/var/logs/nginx/error.log debug;
pid /usr/local/var/run/nginx.pid;
events {
worker_connections 256;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_x_forwarded_for" $host $request_time $upstream_response_time $scheme '
'$cookie_evalogin';
access_log /usr/local/var/logs/access.log main;
sendfile on;
keepalive_timeout 65;
port_in_redirect off;
include /usr/local/etc/nginx/sites-enabled/*;
}
设置nginx php-fpm配置文件
vim /usr/local/etc/nginx/conf.d/php-fpm
#输入以下配置
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
include /usr/local/etc/nginx/fastcgi.conf;
}
在/usr/local/etc/nginx/sites-enabled
目录下,一个文件对应一个域名的配置,我们设置web服务器目录是/var/www
sudo mkdir -p /var/www
sudo chown :staff /var/www
sudo chmod 775 /var/www
vi /var/www/info.php
vi /var/www/index.html
vi /var/www/403.html
vi /var/www/404.html
创建默认虚拟主机default
vim /usr/local/etc/nginx/sites-available/default
#输入以下配置
server {
listen 80;
server_name localhost;
root /var/www/;
access_log /usr/local/var/logs/nginx/default.access.log main;
location / {
index index.html index.htm index.php;
autoindex on;
include /usr/local/etc/nginx/conf.d/php-fpm;
}
location = /info {
allow 127.0.0.1;
deny all;
rewrite (.*) /.info.php;
}
error_page 404 /404.html;
error_page 403 /403.html;
}
创建ssl默认虚拟主机default-ssl
vim /usr/local/etc/nginx/sites-available/default-ssl
#输入以下配置
server {
listen 443;
server_name localhost;
root /var/www/;
access_log /usr/local/var/logs/nginx/default-ssl.access.log main;
ssl on;
ssl_certificate ssl/localhost.crt;
ssl_certificate_key ssl/localhost.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
include /usr/local/etc/nginx/conf.d/php-fpm;
}
location = /info {
allow 127.0.0.1;
deny all;
rewrite (.*) /.info.php;
}
error_page 404 /404.html;
error_page 403 /403.html;
}
这样启动了php-fpm和Nginx后,就可以通过http://localhost
来运访问php程序了,也可以使用https://localhost
来访问ssl内容
安装mysql
mysql基本安装
安装命令:
brew install mysql
安装完成之后开启MySQL安全机制:
/usr/local/opt/mysql/bin/mysql_secure_installation
MySQL开机启动:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
基本命令:
mysql.server start
mysql.server stop
mysql -u root -p
安装phpmyadmin
安装命令:
brew install phpmyadmin
创建phpmyadmin虚拟主机:
vim /usr/local/etc/nginx/sites-available/phpmyadmin
#输入以下配置
server {
listen 306;
server_name localhost;
root /usr/local/share/phpmyadmin;
error_log /usr/local/var/logs/nginx/phpmyadmin.error.log;
access_log /usr/local/var/logs/nginx/phpmyadmin.access.log main;
ssl on;
ssl_certificate ssl/phpmyadmin.crt;
ssl_certificate_key ssl/phpmyadmin.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
index index.html index.htm index.php;
include /usr/local/etc/nginx/conf.d/php-fpm;
}
}
设置ssl
mkdir -p /usr/local/etc/nginx/ssl
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
创建虚拟主机软连接,开启虚拟主机
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
这样便可以用下面链接访问了:
http://localhost/ -> index.html
http://localhost/info -> info.php via phpinfo();
http://localhost/404 -> 404.html
https://localhost/ -> index.html(SSL)
https://localhost/info -> info.php via phpinfo();(SSL)
https://localhost/404 -> 404.html(SSL)
https://localhost:306 -> phpmyadmin(SSL)
安装memcached
安装命令:
brew install memcached
启动/停止指令:
memcached -d
killall memcached
加入开机启动:
ln -sfv /usr/local/opt/memcached/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist
安装redis
安装命令:
brew install redis
Redis默认配置文件不允许以Deamon方式运行,因此需要先修改配置文件
vim /usr/local/etc/redis.conf
将daemonize修改为yes,然后载入配置文件即可实现后台进程启动
redis-server /usr/local/etc/redis.conf
加入开机启动
ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
附:所有开机启动的命令可以用brew info
查看
设置快捷启动
使用alias命令,创建服务快捷启动:
vim ~/.bash_aliases
#输入以下内容
alias nginx.start='launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
alias nginx.stop='launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
alias nginx.restart='nginx.stop && nginx.start'
alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
alias php-fpm.restart='php-fpm.stop && php-fpm.start'
alias mysql.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.restart='mysql.stop && mysql.start'
alias redis.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
alias redis.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
alias redis.restart='redis.stop && redis.start'
alias memcached.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
alias memcached.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
alias memcached.restart='memcached.stop && memcached.start'
让快捷命令生效
echo "[[ -f ~/.bash_aliases ]] && . ~/.bash_aliases" >> ~/.bash_profile
source ~/.bash_profile