@安裝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
================================================