Snow Leopard 安装 nginx/php fastcgi/mysql

安装 XCode 最新版
安装 MacPorts
sudo port install nginx fcgi spawn-fcgi
sudo port install php5 +fastcgi ….
vim 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><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>

</plist>


sudo cp org.macports.phpfcgi.plist /Library/LaunchDaemons/

sudo launchctl load -w /Library/LaunchDaemons/org.macports.phpfcgi.plist

sudo vim /opt/local/etc/nginx/nginx.conf

user root admin;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include mime.types;
    include phpfcgi.conf;

    default_type application/octet-stream;

    sendfile on;

    keepalive_timeout  0;

    gzip  on;

    include vhosts/*;
}

配置里面的 user root admin; 很重要,不然没法访问 /Users 目录里面的文件。

sudo vim /opt/local/etc/nginx/phpfcgi.conf

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

fastcgi_index  index.php;

fastcgi_param  REDIRECT_STATUS    200;

sudo vim /opt/local/etc/nginx/vhosts/localhost.conf

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  PATH_INFO          $fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

fastcgi_index  index.php;

fastcgi_param  REDIRECT_STATUS    200;

注意文件中的 fastcgi_param PATH_INFO $fastcgi_script_name。如果没有这一行,PHP 无法得到 PATHINFO 信息。

sudo vim /opt/local/etc/nginx/vhosts/localhost.conf

server {
    listen 8080;
    server_name localhost;
    root share/nginx/html;

    location / {
        index index.html index.htm index.php;
    }

    location ~ \.php {
        fastcgi_pass   127.0.0.1:9000;
    }
}

其他虚拟主机参考 localhost.conf,改 server_name 和 root 设定就可以了。

其中 location ~ \.php 后面没有$,否则无法匹配 index.php/xxx/yyy 带 PATHINFO 的脚本调用。

安装 MySQL 就没必要自己编译了,从 mysql.com 下载一个 .dmg 就是。装好后:

sudo cp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf

sudo vim /etc/my.cnf

[mysqld]
...
default-character-set = utf8
...

相关推荐