Mac os x下配置nginx php【美洲杯冠军竞猜】
一直都没使用过PHP的,最近leader推荐使用他在维护的一个移动端的js框架,在本地合并压缩使用的是php环境处理的,so,只能是搭一个PHP的环境了。一直使用的本地代理服务器都是nginx,虽然PHP和Apache挺般配的,Mac上也自带Apache,但是本地的静态服务一直都是用的nginx,不想在换到Apache上,所以就自己将nginx和PHP配了下对。好了废话不说了,开始正文。
安装Homebrew
首先打个广告,我leader维护的框架:Murloc(两栖怪),以后会做更详细的介绍。
确保系统已经安装xcode,然后使用一行命令安装依赖管理工具 Homebrew
ruby -e "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/install)"
默认我认为你已经安装好了Homebrew和nginx
之后就可以使用
brew install FORMULA
OS X 10.9搭载的是PHP54,但是与nginx配置起来比较麻烦,所以我使用Homebrew安装PHP5:
来安装所需要的依赖了。
brew(意为酿酒)的命名很有意思,全部都使用了酿酒过程中采用的材料/器具,名词对应以下的概念:
Formula(配方) 程序包定义,本质上是一个rb文件
Keg(桶)程序包的安装路径
Cellar(地窖)所有程序包(桶)的根目录
Tap(水龙头)程序包的源
Bottle (瓶子)编译打包好的程序包
最终编译安装完毕的程序就是一桶酿造好的酒
更详细的信息参考 Homebrew的官方Cookbook
因此使用Homebrew常见的流程是:
①、安装php-fpm
- 增加一个程序源(新增一个水龙头) brew tap homebrew/php
- 更新程序源 brew update
- 安装程序包(按照配方酿酒) brew install git
- 查看配置 brew config 可以看到程序包默认安装在 /usr/local/Cellar 下
(酒桶放在地窖内)
安装PHP5.6(FPM方式)
首先加入Homebrew官方的几个软件源
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/php
1、brew tap homebrew/dupes 2、brew tap josegonzalez/homebrew-php 3、brew install --without-apache --with-fpm php55
PHP如果采用默认配置安装,会编译 mod_php
模块并只运行在Apache环境下,为了使用Nginx,这里需要编译php-fpm并且禁用apache,主要通过参数
--without-snmp --without-apache 来实现。完整的安装指令为
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
第3步安装php的时候可能会报下边那样的错:
由于OSX已经自带了PHP环境,因此需要修改系统路径,优先运行brew安装的版本,在
~/.bashrc 里加入:
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
Error: SHA256 mismatch Expected: 519ee29e28532782676f3d8e31a808ffbfee383e0279ccc8cbd2b12ed53c2335 Actual: 165907663faf78ae8a056fd4c6b20bdb54f732c7e6103b4b724b277c934ed206 Archive: /Library/Caches/Homebrew/php55-5.5.12
如果要安装新的php扩展,可以直接安装而不用每次重新编译php,所有的扩展可以通过
brew search php56
解决办法:删除/Library/Catches/Homebrew/php55-5-5.12,打开/usr/local/Library/Taps/josegonzalez/homebrew-php/Formula/abstract-php-version.rb文件,
看到,下面是我自己所需要的扩展,可以支持 Phalcon框架 :
brew install php56-gearman php56-msgpack php56-memcache php56-memcached
php56-mongo php56-phalcon php56-redis php56-xdebug
将: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
PHP-FPM的加载与启动
安装完毕后可以通过以下指令启动和停止php-fpm
php-fpm -D
killall php-fpm
保存后,重新执行第3步。
同时可以讲php-fpm加入开机启动
ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
②、替换系统自带的PHP54
安装Nginx
brew install nginx
打开~/.bash_profile
,写入:
安装完毕后可以通过
nginx
nginx -s quit
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
启动和关闭,同时也支持重载配置文件等操作
nginx -s reload|reopen|stop|quit
保存之后重启终端,php -v
或者php-fpm -v
nginx安装后默认监听8080端口,可以访问
http://localhost:8080
查看状态。如果要想监听80端口需要root权限,运行
sudo chown root:wheel /usr/local/Cellar/nginx/1.6.2/bin/nginx
sudo chmod u s /usr/local/Cellar/nginx/1.6.2/bin/nginx
好了现在php算是安装成功了。
并使用root权限启动
sudo nginx
③、配置nginx
开机启动
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
打开/usr/local/etc/nginx/nginx.conf,配置nginx,下边是我的配置:
Nginx PHP-FPM配置
Nginx一般都会运行多个域名,因此这里参考了 @fish的方法
,按Ubuntu的文件夹结构来存放Nginx的配置文件
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
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全局配置
vim /usr/local/etc/nginx/nginx.conf
nginx的localhost:8080默认指向的是/usr/local/opt/nginx/html目录的, 在该目录下新建一个php文件,例如test.php,
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/*;
}
本文由美洲杯冠军竞猜发布于计算机教程,转载请注明出处:Mac os x下配置nginx php【美洲杯冠军竞猜】
关键词: