安装nginx和mysql

sudo apt-get install nginx  php5-cli php5-cgi php5-memcache php5-mcrypt php5-gd php5-curl mysql-server mysql-client php5-mysql lighttpd rcconf   

sudo service lighttpd stop                停止lighttpd

sudo rcconf                               去掉lighttpd开机自启动

nginx的配置:

设置nginx系统全局配置

sudo vim /etc/nginx/nginx.conf                     

nginx.conf写入如下配置:

user www-data;                           

worker_processes  2;                       #设置占用CPU进程数

error_log  /var/log/nginx/error.log;       

pid        /var/run/nginx.pid;

#diy1

worker_rlimit_nofile    51200;             #设置访问nginx最大连接数   

events {

    use epoll;                             #设置访问模式

    worker_connections  51200;             #设置访问最大连接数

    # multi_accept on;                

}

http {

    include       /etc/nginx/mime.types;

 

    access_log    /var/log/nginx/access.log;

    sendfile        on;

    tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

    tcp_nodelay        on;

 

#设置fastcgi相关参数:连接,发送,读取时间以及缓存大小等

    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;

 

#设置文件压缩相关参数

    gzip  on;

    gzip_min_length 1k;

    gzip_buffers 4 16k;

    gzip_http_version 1.1;

    gzip_comp_level 2;

    gzip_types text/plain application/x-javascript text/css application/xml;

    gzip_vary on;

    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    include /etc/nginx/conf.d/*.conf;

    include /etc/nginx/sites-enabled/*;

}

 

单个web网站配置

sudo vim /etc/nginx/sites-available/default

default中写入如下配置:

server {

    listen  2080;                 #设置网站访问端口号

    server_name  a.a;             #设置访问网站域名或者ip

    access_log  /var/log/nginx/localhost.access.log;

    location / {

        root   /var/www/nginx-default;           #设置网站根目录路径

        index  index.php index.html index.htm;   #设置首页文件类型

    }

    #启用fastcgi并设置相关参数

    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 fastcgi_params;

                fastcgi_param DOCUMENT_ROOT "网站根目录路径";   #设置网站文档目录路径

    }

         #关闭一些多余文件日志

        location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$

        {

             root   /var/www/nginx-default;                  

             access_log off;

         }

}

 

/etc/nginx/sites-enable/目录下设置单个web网站配置文件的软连接

sudo ln -s 配置文件名称 /etc/nginx/sites-available/配置文件名称

 

负载均衡控制服务器上的nginx设置

 

在负载均衡控制服务器上/etc/nginx/sites-available/目录下新建nginxctrl文件

 

nginxctrl写入如下配置:

 

#设置负载机器IP以及负载权重

upstream bk{

           ip_hash;

    server 192.168.2.201:2080 weight=10;

    server 192.168.2.202:2080 weight=10;

}

#设置需要负载的网站ip或者域名

server {

    listen 80;

    server_name a.a;

    location /{

        proxy_pass http://bk;

        proxy_set_header        Host $host;

        proxy_set_header        X-Real-IP $remote_addr;

        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

            

    }

}

 

 

/etc/nginx/sites-enable/目录下设置nginxctrl软连接

sudo ln -s nginxctrl /etc/nginx/sites-available/nginxctrl

 

 

启动fastcgi php:

sudo spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -g www-data -f  /usr/bin/php-cgi

 

设置php-cgi开机自启动:

sudo vim /etc/init.d/nginx

找到在相应位置插入如下语句:

 

start)

 spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -g www-data -f /usr/bin/php-cgi

 echo "$NAME."

stop)

        echo -n "Stopping $DESC: "

        pkill -9 php-cgi

        echo "$NAME."

 

重新启动nginx:

sudo service nginx stop

sudo service nginx start

 

关闭php-cgi日志

sudo vim /etc/php5/cgi/php.ini

找到下面一行

display_errors = On

更改为

display_errors = Off

保存重启php-cgi

pkill -9 php-cgi

sudo  spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -g www-data -f /usr/bin/php-cgi