今天心血来潮,再折腾一次.转换LAMP为LNMP


安装nginx+php5-cgi+spawn-fcgi

sudo aptitude purge apache2
sudo aptitude install nginx php5-cgi spawn-fcgi
#php5-cgi:安装完成后,默认运行在9000端口

配置nginx虚拟主机

cd /etc/nginx/site-available/default 
sudo cp default yjiang
cd ../site-enabled/
sudo ln -s ../site-available/yjiang ./
sudo vi yjiang
配置文件修改如下:
server {
    listen  80;
    server_name yjiang.tk;            #绑定域名
    root /var/www/cake;                     #网站根目录
    index index.htm index.html index.php;   #默认文件
    #include /var/www/cake/.htaccess        #引用.htaccess
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;  #fastcig_params参数文件地址
    }
    location ~ .*\.(png|jpg|gif|js|css)${
        expires 10d;
    }
    access_log  /var/log/nginx/access_cake.log;
    error_log  /var/log/nginx/error_cake.log;
}

使用spawn-fcgi管理php5-cgi

killall php5-cgi 

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 6 -u www-data -g www-data -f /usr/bin/php5-cgi 
#参数说明: -P 端口  -C 使用的进程数 -u 用户 -g 组     


  • 默认是不支持rewrite的写法,.htaccess文件也需要include进去
  • 实际运行中,spawn-fcgi曾挂掉过多次,而换用php5-cgi以来,已正常运行超过3个月.所以还是推荐使用php5-cgi,而只用spawn-fcgi来管理php5-cgi.

nginx 配置

user nginx;
worker_processes 12;
worker_cpu_affinity 000000000001 000000000010 000000000100 000000001000 000000010000 000000100000 000001000000 000010000000 000100000000 001000000000 010000000000 100000000000;
pid /var/run/nginx.pid;
worker_rlimit_nofile 1024000;

events {
    use epoll;
    worker_connections 1024000;
    # multi_accept on;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    charset utf-8;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    server_names_hash_bucket_size 128;
    types_hash_max_size 2048;
    large_client_header_buffers 4 4k;
    client_max_body_size 8m;
    # server_tokens off;
    # server_name_in_redirect off;

    #access_log /var/log/nginx/access.log;
    #error_log /var/log/nginx/error.log;
    access_log off;
    error_log off;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";       #对IE6不进行gzip
    gzip_vary on;               #和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩
    gzip_proxied any;
    gzip_comp_level 4;          #gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
    gzip_buffers 16 8k;          #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存
    gzip_http_version 1.1;      #对HTTP/1.1协议的请求才会进行gzip压缩(注意,当通过nginx反向代理,会是1.0 gzip将失效)
    gzip_types text/plain text/css application/x-javascript application/json text/javascript image/png image/gif image/jpg;


    ##
    # Fastcgi Settings
    ##
    fastcgi_cache_path /etc/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;
    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;
    fastcgi_cache TEST;
    fastcgi_cache_valid 200 302 1h;
    fastcgi_cache_valid 301 1d;
    fastcgi_cache_valid any 1m;

    #include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}