Mac下已经为我们集成了php5.4、apache2。所以在Mac下开发PHP是一种不错的选择;
安装环境
osx10.9.4
xcode Version 5.1.1 (5B1008)
如果您已经装好以上环境就可以跟着本文安装PHP开发环境了
老规矩,本文安装使用的Homebrew进行Mac软件包管理器来安装,方便以后升级和扩展.
这里我们不使用mac给我们提供的php和apache,我们来全新安装环境,当然我们也不要去删除自带的php和apache,以免出现系统不稳定(xcode要用到)。
首先,安装一下编辑器,这里推荐安装sublime3,方便等下修改配置文件
下载地址:SublimeOS X版本(10.7 or later is required)
安装完毕之后做一个链接,以后使用sublime直接可以使用subl
sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /bin/subl
第二步,安装Homebrew
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"
安装过程有点漫长,由于国内网络复杂原因,建议安装homebrew的时候使用v p n
更新下brew
brew update
安装Nginx1.6.0
brew install nginx
mkdir -p ~/Library/LaunchAgents
#添加到开机启动
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
#下面这两句必须,否则开机自启动不会成功
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
sudo chown -R $USER /usr/local/var/log/nginx/
#等全部安装完环境在进行配置
安装php5.5.14
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
#以下命令必须执行,否则会出现找不到openssl的错误
xcode-select --install
brew install --without-apache --with-fpm --with-mysql php55
#替换path
vi ~/.bash_profile
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
#测试一下版本
php -v or php-fpm -v
#安装完毕设置php-fpm自启动
ln -sfv /usr/local/opt/php55/*.plist ~/Library/LaunchAgents
安装mysql5.6.19
brew install mysql
#启动mysql
mysql.server start
#设置root密码
mysqladmin -u root password
#mysql开机自启动
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
配置Nginx (开发环境配置,非生产环境)
#备份原始配置
mv /usr/local/etc/nginx/nginx.conf /usr/local/etc/nginx/nginx.conf.bak
#配置文件
vi /usr/local/etc/nginx/nginx.conf
#写入以下内容,user要改成你的当前登录账户
user zhuzhenyu staff;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
events {
worker_connections 1024;
}
http {
#列目录,方便调试
autoindex on;
include mime.types;
sendfile on;
#keepalive_timeout 65;
# gzip on;
# gzip_disable "MSIE [1-6]\.(?!.*SV1)";
include vhost/*.conf;
}
#创建include_php.conf
vi /usr/local/etc/nginx/include_php.conf
#写入以下内容
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param SERVER_NAME $host;
fastcgi_intercept_errors on;
#创建虚拟主机配置目录
mkdir /usr/local/etc/nginx/vhost
#这里做演示创建一个虚拟主机:blog.com
subl /usr/local/etc/nginx/vhost/blog.com.conf
#写入以下内容
server {
listen 80;
server_name blog.com;
root /Users/zhuzhenyu/workspace/blog.com;
index index.php index.html index.htm;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
location ~ \.php$ {
include include_php.conf;
}
}
#以后只要创建虚拟主机在/usr/local/etc/nginx/vhost/xxx.conf即可
#创建web目录
mkdir -p ~/workspace/blog.com
subl ~/workspace/blog.com/index.php
#写入以下内容
<?php
phpinfo();
#操作nginx,(如果修改过nginx配置文件,切记一定要重启)
nginx -t #检测nginx配置文件语法
nginx start #启动
nginx -s reload #平滑重启
nginx restart #重启
配置php
subl /usr/local/etc/php/5.5/conf.d/test_load.ini
#写入如下内容
short_open_tag = On
display_errors = On
display_startup_errors = On
upload_max_filesize = 256M
date.timezone = "Asia/Shanghai"
error_reporting = E_ALL
#保存即可
#为了测试,我们修改下本机的host
sudo vi /etc/hosts
#添加一条记录
127.0.0.1 blog.com
这时候,我们在浏览器输入http://blog.com会出现如下结果