利用Nginx替代apache实现高性能的Web环境 ,在freebsd下的安装更是方便,
一 . 安装必备软件
MySQL+PHP+Pcre
cd /usr/ports/database/mysql50-server && make install clean
cd /usr/lang/php5/ && make install clean 选择对cgi mysql等的支持
cd /usr/devel/pcre && make install clean
用ports安装 /usr/ports/www/nginx, make install clean
二、弄了一个fastcgi的脚本,来自lighttpd
1) cd /usr/ports/www/lighttpd
2) make
3) cp /usr/ports/www/lighttpd/work/lighttpd-1.4.18/src/spawn-cgi /usr/bin
4) make clean
三、修改配置文件:
1,/usr/local/etc/nginx/nginx.conf:
- user www www;
- worker_processes 10;
- error_log /usr/local/etc/nginx/logs/nginx_error.log
- events
- {
- use kqueue;
- worker_connections 51200;
- }
- http
- {
- include conf/mime.types;
- default_type application/octet-stream;
- charset gb2312;
- server_names_hash_bucket_size 128;
- keepalive_timeout 60;
- tcp_nodelay on;
- gzip on;
- gzip_min_length 1k;
- gzip_buffers 4 8k;
- gzip_http_version 1.1;
- gzip_types text/plain application/x-javascript. text/css text/html application/xml;
- server
- {
- listen 80;
- server_name www.jianblog.com;
- index index.html index.htm index.php;
- root /usr/local/www/data/;
- location ~ .*\.php?$
- {
- include fcgi.conf;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- }
- log_format access '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" $http_x_forwarded_for';
- access_log /usr/local/nginx/logs/access.log access;
- }
- }
- 2 ,先将php.ini的配置中
- cgi.fix_pathinfo=1 这样php-cgi方能正常使用SCRIPT_FILENAME这个变量。
- 3,编辑fcgi.conf文件,加入
- fastcgi_param GATEWAY_INTERFACE CGI/1.1;
- fastcgi_param SERVER_SOFTWARE nginx;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- # PHP only, required if PHP was built with --enable-force-cgi-redirect
- #fastcgi_param REDIRECT_STATUS 200;
四,启动
1, 启动fcgi
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -f /usr/local/bin/php-cgi
参数说明:
-f 指定调用FastCGI的进程的执行程序位置,根据系统上所装的PHP的情况具体设置
-a 绑定到地址addr
-p 绑定到端口port
-s 绑定到unix socket的路径path
-C 指定产生的FastCGI的进程数,默认为5(仅用于PHP)
-P 指定产生的进程的PID文件路径
-u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,Ubuntu下可以使用www-data,其他的根据情况配置,如nobody、apache等
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -f /usr/local/bin/php-cgi