Chinaunix首页 | 论坛 | 博客
  • 博客访问: 374865
  • 博文数量: 73
  • 博客积分: 2970
  • 博客等级: 少校
  • 技术积分: 1775
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-15 09:34
文章分类

全部博文(73)

文章存档

2011年(69)

2010年(4)

我的朋友

分类: LINUX

2011-04-07 10:23:03

三、安装Nginx 0.8.15
  1、安装Nginx所需的pcre库:
tar zxvf pcre-7.9.tar.gz
cd pcre-7.9/
./configure
make && make install
cd ../


  2、安装Nginx

tar zxvf nginx-0.8.15.tar.gz
cd nginx-0.8.15/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../


  3、创建Nginx日志目录

mkdir -p /data1/logs
chmod +w /data1/logs
chown -R www:www /data1/logs


  4、创建Nginx配置文件
  ①、在/usr/local/nginx/conf/目录中创建nginx.conf文件:

rm -f /usr/local/nginx/conf/nginx.conf
vi /usr/local/nginx/conf/nginx.conf


  输入以下内容:

user  www www;

worker_processes 8;

error_log  /data1/logs/nginx_error.log  crit;

pid        /usr/local/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;

events
{
  use epoll;
  worker_connections 65535;
}

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;
  client_max_body_size 8m;
      
  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  crawler  $binary_remote_addr  10m;

  server
  {
    listen       80;
    server_name  ;
    index index.html index.htm index.php;
    root  /web/www;

    #limit_conn   crawler  20;    
                            
    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;
       

    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  /web/www/access.log  access;
      }

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

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

    log_format  wwwlogs  '$remote_addr - $remote_user [$time_local] "$request" '
               '$status $body_bytes_sent "$http_referer" '
               '"$http_user_agent" $http_x_forwarded_for';
    access_log  /www/html/ wwwlogs;
  }

  server
  {
    listen  80;
    server_name  status.lnmp.com;

    location / {
       stub_status on;
       access_log   off;
    }
  }
}
 
 
  ②、在/usr/local/nginx/conf/目录中创建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;
 
  5、启动Nginx
ulimit -SHn 65535
/usr/local/nginx/sbin/nginx



四、配置开机自动启动Nginx + PHP

vi /root/run.sh


  在末尾增加以下内容:

ulimit -SHn 65535
/usr/local/php/sbin/php-fpm start
/usr/local/nginx/sbin/nginx


五、优化Linux内核参数
vi /etc/sysctl.conf


  在末尾增加以下内容:

# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024  65535
 
  使配置立即生效:
/sbin/sysctl -p



六、在不停止Nginx服务的情况下平滑变更Nginx配置
  1、修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:

/usr/local/nginx/sbin/nginx -t


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


  2、这时,输入以下命令查看Nginx主进程号:

ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'


  屏幕显示的即为Nginx主进程号,例如:
  6302
  这时,执行以下命令即可使修改过的Nginx配置文件生效:

kill -HUP 6302


  或者无需这么麻烦,找到Nginx的Pid文件:

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`



七、编写每天定时切割Nginx日志的脚本
  1、创建脚本/usr/local/nginx/sbin/cut_nginx_log.sh

vi /usr/local/nginx/sbin/cut_nginx_log.sh


  输入以下内容:

#!/bin/bash
# This script run at 00:00

# The Nginx logs path
logs_path="/usr/local/nginx/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")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
 

  2、设置crontab,每天凌晨00:00切割nginx访问日志

crontab -e


  输入以下内容:

00 00 * * * /bin/bash  /usr/local/nginx/sbin/cut_nginx_log.sh

 

八、Nginx的启动

 

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
killall nginx
/root/run.sh

 

九、参考文献

    1、LNMP-Linux下Nginx+MySQL+PHP+phpMyAdmin+eAcelerator一键安装包

       http://blog.licess.cn/lnmp/

    2、Nginx 0.8.x + PHP 5.2.10(FastCGI)搭建胜过Apache十倍的Web服务器

       http://blog.s135.com/nginx_php_v5/
    注:本文主要内容来自以上两篇文章,在这里只是为了学习之用。

 

转自:http://blog.sina.com.cn/s/blog_474cf12b0100g1df.html

阅读(1164) | 评论(0) | 转发(1) |
0

上一篇:Nginx安装笔记(二)

下一篇:DHCP服务全攻略

给主人留下些什么吧!~~