mac os x安装ngigx+php fastcgi+mysql+memcache详细流程
Part 1: MacPorts
Mac上装软件常用的是MacPorts和homebrew,这个软件会很方便地提供软件的安装。装这些前先得装Xcode,Xcode在appstore上有,一个多G,下载安装,完成后在launchpad上回有Xcode图标,要点击才是正式安装,装完后你在终端输入gcc -v,发现无此命令,没这个命令可没法装软件哦,应该是Xcode默认安装没有装完整,打开Xcode,点击左上方Xcode-》Open develop tools-》more develop tools,进入Xcode的网页搜索UNIX,安装command_line_tools_for_xcode_.dmg,完成后再进入终端,输入gcc -v,ok有了,哈哈哈。
我使用的是macports安装,macports到http://www.macports.org/install.php下载dmg文件安装就可以了,安装成功后,在终端输入
port list #可以看所有软件列表
port search xxx #可以搜索是否有该软件
port install xxx #安装
port uninstall xxx #卸载
port clean xxx #删除安装临时文件
Ps:homebrew的安装
首先:
sudo chown -R `whoami` /usr/local
然后可以正式开始安装,我推荐的安装方式是先用 git-osx-installer 装上 git,然后用 git 安装:
cd /usr/local
git init
git remote add origin git://github.com/mxcl/homebrew.git
git pull origin master
装完后命令与port命令差不多 比如 brew search;brew install
Part 2: 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页面。
sudo nginx -t # 检测配置文件是否有效
sudo nginx -s reload # 重启
sudo nginx -h # 帮助
1、让nginx开机自动启动
vi /Library/LaunchDaemons/org.macports.nginx.plist
内容为
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//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.nginx</string>
<key>ProgramArguments</key>
<array>
<string>/opt/local/bin/daemondo</string>
<string>--label=nginx</string>
<string>--start-cmd</string>
<string>/opt/local/sbin/nginx</string>
<string>;</string>
<string>--pid=fileauto</string>
<string>--pidfile</string>
<string>/opt/local/var/run/nginx/nginx.pid</string>
</array>
<key>Debug</key><false/>
<key>Disabled</key><true/>
<key>KeepAlive</key><true/>
</dict>
</plist>
如果文件已经有内容直接退出vi
执行
sudo launchctl load -w /Library/LaunchDaemons/org.macports.nginx.plist
这样nginx开机就回自动启动
2、修改nginx.conf内容
vi /opt/local/etc/nginx/nginx.conf
下面是我的内容
#user nobody;
user www www;
worker_processes 1;
error_log /Users/apple/logs/nginx_errors.log;
events {
worker_connections 4098;
}
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 /Users/apple/logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
include /opt/local/etc/nginx/conf.d/*.conf;
include /opt/local/etc/nginx/sites-enabled/*;
}
然后建立目录
sudo mkdir /opt/local/etc/nginx/sites-enabled
进入cd /opt/local/etc/nginx/sites-enabled
编辑一个站点的conf文件
我建了一个default,内容如下
server {
listen 80; ## listen for ipv4
listen [::]:80 default ipv6only=on; ## listen for ipv6
server_name localhost;
access_log /Users/apple/logs/localhost.access.log;
location / {
root /Users/apple/phpdocs;
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /Users/apple/phpdocs$fastcgi_script_name;
include fastcgi_params;
}
}
保存后在相应的路径上建立log文件和html文件
这个时候先不要放index.php,会出现bad gatway错误,咱们还没装php和fastcgi呢
Part 3: PHP
在终端上输入
安装php5,扩展模块根据自己的需要调整
port install php5 +fastcgi fcgi php5-gd php5-mysql php5-sqlite php5-eaccelerator php5-curl php5-iconv php5-mcrypt
安装完成后,到/opt/local/etc/php5下,cp php.ini-recommended php.ini
然后修改几个项:
error_reporting = E_ALL & ~E_NOTICE
display_errors = On
error_log = /Users/jonathan/logs/php5/error.log
date.timezone = Asia/Shanghai
手动创建/Users/jonathan/logs/php5/error.log日志
我们要再写一个plist文件跑fastcgi
vi /Library/LaunchDaemons/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>
保存后输入
sudo launchctl load -w /Library/LaunchDaemons/org.macports. phpfcgi.plist
这样就自动启动php fastcgi
现在去网站目录放个index.php,看下phpinfo()吧
Part4: 安装MySQL
安装mysql很方便,去mysql网站下个dmg吧,有三个文件要安装,一个是mysql安装文件,一个是mysql自动启动,一个是系统设置里的添加项控制mysql的开关。
装完以后下载phpmyadmin到网站目录,
将phpMyAdmin目录的config.sample.inc.php命名为config.inc.php
打开config.inc.php,做如下修改:
$cfg['blowfish_secret'] = '';//用于Cookie加密,随意的长字符串
$cfg['Servers'][$i]['host'] = '127.0.0.1';//MySQL守护程序做了IP绑定
$cfg['Servers'][$i]['AllowNoPassword'] = false;//可以访问无密码的MySQL
从web访问phpMyAdmin,并修改MySQL密码不为空。
Part4: 安装memcache
装这个本来以为可以用port install php5-memcache,结果port的编译包可能有问题,不能building memcache,只有自己编译了
先装libevent
port install libevent
安装完后去http://pecl.php.net/package/memcache下载最新的版本,下载完成后解压,进入到文件夹里,依次执行以下命令:
./configure
make
make install
#!/bin/bash
DAEMON=/opt/local/bin/memcached
NAME=memcached
DESC=memcached
USER=nobody
HOST=0.0.0.0
PORT=11211
MEM=32
LOG=/Users/apple/logs/memcached/info.log
case "$1" in
start)
echo -n "Startring $DESC: "
$DAEMON -m $MEM -p $PORT -l $HOST -u $USER -d -vv >> $LOG 2>&1
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
killall $NAME
echo "$NAME."
;;
restart)
echo -n "Restarting $DESC: "
killall $NAME
sleep 1
$DAEMON -m $MEM -p $PORT -l $HOST -u $USER -d -vv >> $LOG 2>&1
echo "$NAME."
;;
*)
echo "Usage: $NAME {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
保存后记得赋予可执行权限:chmod +x /etc/init.d/memcached
最后不要忘记创建日志文件:/Users/apple/logs/memcached/info.log
然后让memcached开机启动
写一个plist文件
vi /Library/LaunchDaemons/org.memcache.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.memcache</string>
<key>OnDemand</key><true/>
<key>Username</key>
<string>nobody</string>
<key>ProgramArguments</key>
<array>
<string>/etc/init.d/memcached</string>
<string>start</string>
</array>
<key>Debug</key><false/>
<key>Disabled</key><true/>
<key>RunAtLoad</key><true/>
<key>KeepAlive</key><true/>
</dict>
</plist>
同样保存完后运行
sudo launchctl load -w /Library/LaunchDaemons/org.memcache.plist