/ 中存储网

Linux编译安装Nginx和配置定时切割日志

2013-11-12 12:43:01 来源:ITJS.CN

@安装pcre(Nginx的Urlrewrite模块依赖PCRE)

# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.12.tar.gz

# tar zxvf pcre-8.12.tar.gz

# cd pcre-8.12

# ./configure --prefix=/usr/local/webserver/pcre

# make && make install

# cp /usr/local/webserver/pcre/lib/libpcre.a /usr/local/webserver/pcre/libpcre.a

# cp /usr/local/webserver/pcre/lib/libpcre.la /usr/local/webserver/pcre/libpcre.la

# cp /usr/local/webserver/pcre/include/pcre.h /usr/local/webserver/pcre/pcre.h

复制到pcre目录

# mkdir /usr/local/webserver/pcre/.libs

创建.libs文件夹

# cp /usr/local/webserver/pcre/lib/libpcre.a /usr/local/webserver/pcre/.libs/libpcre.a

# cp /usr/local/webserver/pcre/lib/libpcre.la /usr/local/webserver/pcre/.libs/libpcre.la

# cp /usr/local/webserver/pcre/include/pcre.h /usr/local/webserver/pcre/.libs/pcre.h

复制到.lib文件夹

@安装nginx /usr/local/nginx

# wget http://nginx.org/download/nginx-1.0.10.tar.gz

# tar zxvf nginx-1.0.10.tar.gz

# cd nginx-1.0.10

# ./configure --user=nobody --group=nobody --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module

配置时出错, pcre为编译安装,要指定pcre路径

./configure: error: the HTTP rewrite module requires the PCRE library.

You can either disable the module by using --without-http_rewrite_module

option, or install the PCRE library into the system, or build the PCRE library

statically from the source with nginx by using --with-pcre= option.

# ./configure --user=nobody --group=nobody --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/webserver/pcre

# make && make install

报错: Nginx不能很好的识别到系统中的PCRE

make -f objs/Makefile

make[1]: Entering directory `/usr/local/nginx-1.0.10'

cd /usr/local/webserver/pcre

&& if [ -f Makefile ]; then make distclean; fi

&& CC="gcc" CFLAGS="-O2 -fomit-frame-pointer -pipe "

./configure --disable-shared

/bin/sh: line 2: ./configure: No such file or directory

make[1]: *** [/usr/local/webserver/pcre/Makefile] Error 127

make[1]: Leaving directory `/usr/local/nginx-1.0.10'

make: *** [build] Error 2

# ./configure --user=nobody --group=nobody --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/webserver/pcre

# vi objs/Makefile

查找configure --disable-shared,在1089行,删除./configure --disable-shared, 保存

:st nu可显示行号

:1089直接跳到1089行

#make && make install

@创建Nginx日志目录

mkdir -p /var/log/nginx

chmod +w /var/log/nginx

chown -R nobody:nobody /var/log/nginx

@创建Nginx配置文件

#mv /usr/local/webserver/nginx/conf/nginx.conf  nginx.conf-

#vi /usr/local/webserver/nginx/conf/nginx.conf

================================================

复制进以下内容,保存

user  nobody nobody;

worker_processes 8;

error_log  /var/log/nginx/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;

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

index index.html index.htm index.php;

root  /opt/www;

#limit_conn   crawler  20;

location ~.php {

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 /status/ {

stub_status on;

access_log   off;

}

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  /var/log/nginx/access.log  access;

}

}

================================================

@在/usr/local/webserver/nginx/conf/目录中创建fcgi.conf文件

#vi /usr/local/webserver/nginx/conf/fcgi.conf

================================================

复制以下内容,保存

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

include        fastcgi_params;

fastcgi_split_path_info ^(.*.php)(.*)$;

fastcgi_param  PATH_INFO $fastcgi_path_info;

================================================

@启动Nginx

#ulimit -SHn 51200

使用ulimit -a 可以查看当前系统的所有限制值,使用ulimit -n 可以查看当前的最大打开文件数。

新装的linux默认只有1024,当作负载较大的服务器时,很容易遇到error: too many open files。因此,需要将其改大。

使用 ulimit -n 65535 可即时修改,但重启后就无效了。(注ulimit -SHn 65535 等效 ulimit -n 65535,-S指soft,-H指hard)

#/usr/local/webserver/nginx/sbin/nginx     ----->启动

设定web目录

@每天定时切割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="/var/log/nginx/"

logs_bak_path="/var/log/nginx_bak/"

mkdir -p ${logs_bak_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/

cp ${logs_path}access.log ${logs_bak_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log

rm -rf ${logs_path}*.log

kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`

================================================

#crontab -e        ------->设置crontab,每天凌晨00:00切割nginx访问日志

================================================

    输入以下内容

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

================================================