1、首先安装nginx
在UBUNTU下只需在终端打个命令
#sudo apt-get install nginx
之后启动 #sudo /etc/init.d/nginx start
然后设置fastcgi_params文件
#sudo gedit /etc/nginx/fastcgi_params
修改如下
#fastcgi_params
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
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_FILENAME $document_root$fastcgi_script_name;
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 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;
# PHP only, required if PHP was built with –enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
接下来在nginx的配置中针对php文件配置其利用FastCGI进程来执行:
#sudo gedit /etc/nginx/sites-available/default
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
location / {
root /var/www/nginx-default;
index index.php index.html index.htm;
}
location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}
location /images {
root /usr/share;
autoindex on;
}
#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 /var/www/nginx-default;
}
# 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$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#deny all;
#}
}
通知Nginx重新载入配置:
#sudo /etc/init.d/nginx reload
2、安装PHP5-cgi和mysql
#sudo apt-get install php-pear php5-cli php5-common php5-xcache php5-cgi php5-mysql php5-gd
#sudo apt-get install mysql-server mysql-client
接下来配置PHP文件
#sudo gedit /etc/php5/cgi/php.ini
打开cgi.fix_pathinfo选项:
cgi.fix_pathinfo=1;
这样php-cgi方能正常使用SCRIPT_FILENAME这个变量。
3、配置PHP进程
利用Lighttpd的spawn-fcgi来控制进程的运行。获得spawn-fcgi的方法如下:
wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2 #获取Lighttpd的源码包
tar -xvjf lighttpd-1.4.18.tar.bz2
cd lighttpd-1.4.18
./configure #编译
make
#sudo cp src/spawn-fcgi /usr/local/bin/spawn-fcgi #取出spawn-fcgi的程序
#sudo chomd 755 /usr/local/bin/spawn-fcgi
下面我们就可以使用 spawn-fcgi 来控制php-cgi的FastCGI进程了
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi
参数含义如下
-f
-a
-p
-s
-C
-P
-u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,Ubuntu下可以使用www-data,其他的根据情况配置,如nobody、apache等
然后我们可以将这行代码加入到/etc/rc.local文件底部,这样系统启动的时候也可以同时启动PHP的FastCGI进程。
注意:ip,端口与nginx服务器中的cgi-pass要对应. -C表示打开几个cgi进程,-u表示用户
为了让php-cgi开机自启动:
打开修改/etc/init.d/nginx文件
sudo gedit /etc/init.d/nginx
添加或修改相应位置:
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON
echo "$NAME."
pkill -9 php-cgi
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON
pkill -9 php-cgi
sleep 1
start-stop-daemon --start --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi
;;
保存后测试一下
sudo /etc/init.d/nginx start
OK,到这里就完全配置好了
我都经过几次失败的旅程才完成的
希望能帮到大家有需要的人
四月
08
发表评论