Mac os x下配置nginx + php

一直都没使用过PHP的,最近leader推荐使用他在维护的一个移动端的js框架,在本地合并压缩使用的是php环境处理的,so,只能是搭一个PHP的环境了。一直使用的本地代理服务器都是nginx,虽然PHP和Apache挺般配的,Mac上也自带Apache,但是本地的静态服务一直都是用的nginx,不想在换到Apache上,所以就自己将nginx和PHP配了下对。好了废话不说了,开始正文。

首先打个广告,我leader维护的框架:Murloc(两栖怪),以后会做更详细的介绍。

默认我认为你已经安装好了Homebrew和nginx

OS X 10.9搭载的是PHP54,但是与nginx配置起来比较麻烦,所以我使用Homebrew安装PHP5:

①、安装php-fpm

1、brew tap homebrew/dupes
2、brew tap josegonzalez/homebrew-php
3、brew install --without-apache --with-fpm php55

第3步安装php的时候可能会报下边那样的错:

Error: SHA256 mismatch
Expected: 519ee29e28532782676f3d8e31a808ffbfee383e0279ccc8cbd2b12ed53c2335
Actual: 165907663faf78ae8a056fd4c6b20bdb54f732c7e6103b4b724b277c934ed206
Archive: /Library/Caches/Homebrew/php55-5.5.

解决办法:删除/Library/Catches/Homebrew/php55-5-5.12,打开/usr/local/Library/Taps/josegonzalez/homebrew-php/Formula/abstract-php-version.rb文件,

将:http://www.php.net/get/php-5.5.12.tar.bz2/from/this/mirror 
改为:http://uk1.php.net/get/php-5.5.12.tar.bz2/from/this/mirror

保存后,重新执行第3步。

②、替换系统自带的PHP54

打开~/.bash_profile,写入:

export PATH="/usr/local/bin:/usr/local/sbin:$PATH"

保存之后重启终端,php -v或者php-fpm -v

好了现在php算是安装成功了。

③、配置nginx

打开/usr/local/etc/nginx/nginx.conf,配置nginx,下边是我的配置:

server {
listen 8080;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm index.php;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

nginx的localhost:8080默认指向的是/usr/local/opt/nginx/html目录的, 在该目录下新建一个php文件,例如test.php,

<?php phpinfo();?>

启动nginx服务器,访问http://localhost:8080/test.php,看是否能看到如下图的页面

Mac os x下配置nginx + php

如果看到了 那么就配置成功了。

最后附上window下的php + nginx的配置:猛戳这里

上一篇:在ubuntu上面配置nginx实现反向代理和负载均衡


下一篇:phpcmsv9 阿里云OSS云存储整合教程