1,安装nginx,执行以下命令,很快完成,不过目前apg-get方式安装默认是0.5.33的版本
sudo apt-get install nginx
配置文件默认安装位置:
[quote]conf: /etc/nginx/nginx.conf
bin:/usr/sbin/nginx
vhost: /etc/nginx/sites-enable/default
cgi-params: /etc/nginx/fastcgi-params[/quote]
建一个虚拟Server
server {
listen 80;
server_name www.23day.com;
access_log /var/log/nginx/home.ucenter.access.log;
location / {
root /var/www/23day.com;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/23day.com$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
2,安装php-cgi模块
执行sudo apt-get install php5-cgi
配置文件默认安装位置:
php-cgi: /usr/bin/php-cgi
php5-cgi: /usr/bin/php5-cgi
cgi config: /usr/bin/cgi/php.ini [/quote]
修改php.ini文件的cgi.fix_pathinfo数据为1,默认为0 cgi.fix_pathinfo=1; 这样php-cgi方能正常使用SCRIPT_FILENAME这个变量.
3,安装spawn-fcgi spawn-fcgi是lighttpd的一个用来控制php-cgi的工具.
如果系统没有安装GCC编译环境,刚需要在安装lighttpd之前要安装build-essential工具包,执行以下命令
sudo apt-get install build-essential
wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz
tar -xvf lighttpd-1.4.19.tar.gz
cd lighttpd-1.4.19/
sudo apt-get install libpcre3-dev
./configure –without-zlib –without-bzip2
make
sudo cp src/spawn-fcgi /usr/local/bin/spawn-fcgi
这样cgi控制器就安装完成.
4.启动测试系统.启动fast_cgi:
spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi
注意:ip,端口与nginx服务器中的cgi-pass要对应. -C表示打开几个cgi进程
启动nginx
sudo /etc/init.d/nginx start
好了,如果没有出错信息,则说明配置成功了,现在写个phpinfo测试下吧!
最后,附上我的/etc/nginx/sites-enable/default的配置文件,此配置文件启用了rewrite功能
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
location / {
root /var/www/nginx-default;
index index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
#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;
fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /\.ht {
#deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#listen 8000;
#listen somename:8080;
#server_name somename alias another.alias;
#location / {
#root html;
#index index.html index.htm;
#}
#}
# HTTPS server
#
#server {
#listen 443;
#server_name localhost;
#ssl on;
#ssl_certificate cert.pem;
#ssl_certificate_key cert.key;
#ssl_session_timeout 5m;
#ssl_protocols SSLv2 SSLv3 TLSv1;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;
#location / {
#root html;
#index index.html index.htm;
#}
#}
nginx.conf优化
user www www;
#Nginx每个进程耗费10M~12M内存,这里只开启一个Nginx进程,节省内存。
worker_processes 1;
error_log /data1/logs/nginx_error.log crit;
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
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 128k;
#对网页文件、CSS、JS、XML等启动gzip压缩,减少数据传输量,提高访问速度。
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
server
{
listen 80;
server_name blog.s135.com www.s135.com s135.com *.s135.com;
index index.html index.htm index.php;
root /data0/htdocs/blog;
#limit_conn crawler 20;
#针对Bo-Blog系统的Rewrite静态化
rewrite ^/post/([0-9]+).htm$ /read.php?$1 last;
rewrite ^/post/([0-9]+)_([0-9]+).htm$ /read.php?$1&page=$2 last;
rewrite ^/post/([0-9]+)_([0-9]+)_([0-9]+).htm$ /read.php?$1&page=$2&part=$3 last;
rewrite ^/index_([0-9]+)_([0-9]+).htm$ /index.php?mode=$1&page=$2 last;
rewrite ^/star_([0-9]+)_([0-9]+).htm$ /star.php?mode=$1&page=$2 last;
rewrite ^/category_([0-9]+).htm$ /index.php?go=category_$1 last;
rewrite ^/category_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=category_$1&mode=$2&page=$3 last;
rewrite ^/archive_([0-9]+)_([0-9]+).htm$ /index.php?go=archive&cm=$1&cy=$2 last;
rewrite ^/archive_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=archive&cm=$1&cy=$2&mode=$3&page=$4 last;
rewrite ^/showday_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=showday_$1-$2-$3 last;
rewrite ^/showday_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).htm$ /index.php?go=showday_$1-$2-$3&mode=$4&page=$5 last;
location ~ .*\.(php|php5)?$
{
#将Nginx与FastCGI的通信方式由TCP改为Unix Socket。TCP在高并发访问下比Unix Socket稳定,但Unix Socket速度要比TCP快。
fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ /read.php
{
#将Nginx与FastCGI的通信方式由TCP改为Unix Socket。TCP在高并发访问下比Unix Socket稳定,但Unix Socket速度要比TCP快。
fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
#博客的图片较多,更改较少,将它们在浏览器本地缓存15天,可以提高下次打开我博客的页面加载速度。
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 15d;
}
#博客会加载很多JavaScript、CSS,将它们在浏览器本地缓存1天,访问者在看完一篇文章或一页后,再看另一篇文件或另一页的内容,无需从服务器再次下载相同的JavaScript、CSS,提高了页面显示速度。
location ~ .*\.(js|css)?$
{
expires 1d;
}
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 /data1/logs/access.log access;
}
}
PHP 5.2.6(FastCGI)的配置优化
[eaccelerator]
zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="1"
eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.keys = "disk_only"
eaccelerator.sessions = "disk_only"
eaccelerator.content = "disk_only"