Mac OS X 安装 nginx + php fcgi

使用port来安装

mac port

安装port http://www.macports.org/install.php

nginx

在Mac上使用macport安装和配置Nginx非常方便,首先安装nginx
sudo port install nginx spawn-fcgi
安装完成后启动nginx会说没有nginx.conf文件,到/opt/local/etc/nginx目录下看到以下几个文件:
fastcgi_params.example koi-utf koi-win mime.types.example nginx.conf.example win-utf
直接复制example文件:
sudo mv mime.types.example mime.types
sudo mv nginx.conf.example nginx.conf
启动nginx:
sudo nginx
访问 http://127.0.0.1/ ,就可以看到Nginx的Welcome页面。
如何重启nginx?
# 检测配置文件是否有效
sudo nginx -t
 
# 重启
sudo nginx -s reload
 
# 帮助
sudo nginx -h
默认的nginx配置不是很好用,下面调整一下
cd /opt/local/etc/nginx/
sudo mkdir conf.d
sudo mkdir sites-available
sudo mkdir sites-enabled
文件nginx.conf的配置http部分:
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"';
 
    access_log  logs/access.log  main;
 
    sendfile        on;
    tcp_nopush     on;
 
    keepalive_timeout  10;
    # keepalive_timeout  65;
 
    gzip  on;
 
    include /opt/local/etc/nginx/conf.d/*.conf;
    include /opt/local/etc/nginx/sites-enabled/*;
}
然后站点配置文件放在目录 sites-available 里,在目录 sites-enabled 中做 ln -s ../sites-available/xxx.conf ./ 即可

php5

安装php5,扩展模块根据自己的需要调整

sudo port install php5 +fastcgi fcgi php5-gd php5-mysql php5-sqlite php5-eaccelerator php5-curl php5-iconv
 
#配置文件
cd /opt/local/etc/php5
sudo cp php.ini-development php.ini
将时区修改为:date.timezone = Asia/Chongqing
错误级别修改为:error_reporting = E_ALL & ~E_NOTICE
php-fcgi
cd /Library/LaunchDaemons/
sudo vim org.macports.phpfcgi.plist
文件org.macports.phpfcgi.plist内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" " http://www.apple.com/DTDs/PropertyList-1.0.dtd ">
<plist version="1.0">
<dict>
  <key>Label</key><string>org.macports.phpfcgi</string>
  <key>Debug</key><false />
  <key>OnDemand</key><false />
  <key>RunAtLoad</key><false />
  <key>EnvironmentVariables</key>
  <dict>
    <key>PHP_FCGI_CHILDREN</key><string>2</string>
    <key>PHP_FCGI_MAX_REQUESTS</key><string>5000</string>
  </dict>
  <key>LaunchOnlyOnce</key><true />
  <key>ProgramArguments</key>
  <array>
    <string>/opt/local/bin/spawn-fcgi</string>
    <string>-C 2</string>
    <string>-p 9000</string>
    <string>-f /opt/local/bin/php-cgi</string>
  </array>
</dict>
</plist>
其中:
<key>PHP_FCGI_CHILDREN</key> 表示开启多少个PHP_FCGI_CHILDREN,如果只是开发的话,2个就ok了
-C 2表示spawn-fcgi跑多少个/opt/local/bin/php-cgi进程
运行:
sudo launchctl load -w org.macports.phpfcgi.plist
上述org.macports.phpfcgi.plist等效于直接运行:
sudo /opt/local/bin/spawn-fcgi -C 2 -p 9000 -f /opt/local/bin/php-cgi
如何重启phpfcgi?
# unload
sudo launchctl unload -w org.macports.phpfcgi.plist
 
# /opt/local/bin/php-cgi 进程依然存在就手动kill掉
pkill php-cgi
 
# load
sudo launchctl load -w org.macports.phpfcgi.plist

你可能感兴趣的:(PHP,nginx,mac,OS,X)