CentOS+Nginx+PHP+Mysql环境配置(四)

用户 流年岁月 2014-8-21 5461

[启动php-cgi进程,监听127.0.0.1的9000端口]
# ulimit -SHn 51200
# /usr/local/webserver/php/sbin/php-fpm start

[安装Nginx所需的pcre库]
# tar zxvf pcre-7.7.tar.gz
# cd pcre-7.7/
# ./configure --enable-utf8 --enable-unicode-properties
# make && make install
# cd ../

[安装Nginx 0.7.19]
# tar zxvf nginx-0.7.19.tar.gz
# cd nginx-0.7.19/
# ./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-openssl = /home /usb/ webserver-linux/webserver/centos_lib/openssl-1.0.0d
# make
# make install
# cd ../

[创建Nginx日志目录]
# mkdir -p /logs
# chmod +w /logs
# chown -R www:www /logs

[创建nginx.conf配置文件]

    # rm -f /usr/local/webserver/nginx/conf/nginx.conf
    # vi /usr/local/webserver/nginx/conf/nginx.conf
    输入以下内容(请注意以下内容中"↑"标志后的内容不能出现在实际文件中):

    user www www;

    worker_processes 8;
    ↑ Nginx每个进程耗费10M~12M内存

    error_log /logs/nginx_error.log warn;

    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;

    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 crash $binary_remote_addr 10m;
    ↑ 定义一个叫“crash”的记录区,总容量为 10M,以变量 $binary_remote_addr 作为会话的判断基准(即一个地址一个会话),当区的大小为 1M 的时候,大约可以记录 32000 个会话信息(一个会话占用 32 bytes)

    server
    {
    listen       80;
    server_name 222.17.177.205;
    index index.html index.htm index.php;
    root /wwwroot;

    ########

    #下面的配置是为了支持qeephp的路由重写功能

    if (-d $request_filename){

                    rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

                    }

                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;

                }
    #######

    #limit_conn   crash 5;
    ↑ *此处已被#注释掉了,即不起作用*定义整个网站的限制。此处为在"crash"记录区中,以变量 $binary_remote_addr 作为会话的判断基准(即一个地址一个会话),限制网站全局目录,一个会话只能进行5个连接(即一个IP只能发起5个连接,多过5个,一律503错误)

    location ~ .*\.(php|php5)?$
    {
    #fastcgi_pass unix:/tmp/php-cgi.sock;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fcgi.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
    expires      1h;
    }

    location /resource/ {
    limit_conn   crash 2;
    ↑ 定义resource目录的限制。此处为在"crash"记录区中,以变量 $binary_remote_addr 作为会话的判断基准(即一个地址一个会话),限制resource目录,一个会话只能进行2个连接(即一个IP只能发起2个连接,多过2个,一律503错 误)
    }

    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 /logs/access.log access;
    sendfile on;
    tcp_nopush on;
    client_max_body_size 50m;
    ↑ 网站程序中允许上传的最大size,这里设置成50M,这里只是nginx的限制,PHP本身限制2M
    }
    }

[创建fcgi.conf配置文件]

    # vi /usr/local/webserver/nginx/conf/fcgi.conf
    输入以下内容:

    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]
# ulimit -SHn 51200
# /usr/local/webserver/nginx/sbin/nginx

[在不停止Nginx服务的情况下平滑变更Nginx配置]

    修改/usr/local/webserver/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
    # /usr/local/webserver/nginx/sbin/nginx -t
    如果测试ok successfully,则可以使用下面命令重启Nginx
    (第1种)# pkill nginx
    # /usr/local/webserver/nginx/conf/nginx.conf
    (第2种)# kill -HUP `cat /usr/local/webserver/nginx/nginx.pid`
    # /usr/local/webserver/nginx/conf/nginx.conf
    (第3种)# ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
    # kill -HUP 数字
    ↑ 此数字来自于上一条命令执行后屏幕输出的数字,即Nginx的pid进程号
    # /usr/local/webserver/nginx/conf/nginx.conf

    如果屏幕显示以下两行信息,说明配置文件正确:
    the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
    the configuration file /usr/local/webserver/nginx/conf/nginx.conf was tested successfully

[配置开机自动启动Nginx + PHP + MySQL]
# vi /etc/rc.local
在末尾增加以下内容:
/bin/sh /usr/local/webserver/mysql/bin/mysqld_safe --defaults-file=/usr/local/webserver/mysql/my.cnf &
ulimit -SHn 51200
/usr/local/webserver/php/sbin/php-fpm start
/usr/local/webserver/nginx/sbin/nginx

[优化Linux内核参数]

    # vi /etc/sysctl.conf
    在文件末尾增加以下内容:

    net.ipv4.tcp_fin_timeout = 30
    net.ipv4.tcp_keepalive_time = 300
    net.ipv4.tcp_syncookies = 1
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.ip_local_port_range = 5000    65000

    # /sbin/sysctl -p
    ↑ 使配置立即生效

[编写每天定时切割Nginx日志的脚本]
# vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
↑ 创建切割脚本

输入以下内容:
#!/bin/bash
# This script run at 00:00

# The Nginx logs path
logs_path="../logs/"

mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/nginx_access_$(date -d "yesterday" +"%Y%m%d").log
mv ${logs_path}error.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/nginx_error_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/webserver/nginx/logs/nginx.pid`

#chmod 755 cut_nginx_log.sh

#./cut_nginx_log.sh   //运行完之后在logs目录下就会出现切割文件及存放目录

[设置切割日志的计划任务]

    # crontab -e
    ↑ 编辑计划任务列表

    输入以下内容:
    00 00 * * * /bin/bash /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
    ↑ 每天凌晨00:00切割nginx访问日志

http://bbs.zblogcn.com/static/image/smiley/default/smile.gif流年岁月http://bbs.zblogcn.com/static/image/smiley/default/sad.gif

最新回复 (0)
全部楼主
    • ZBlogger技术交流中心
      2
          
返回