Nginx 在RedHat5安装过程

By | 2011 年 8 月 9 日

一、Nginx相关资料

主站:http://www.nginx.org/

Nginx中文维基:http://wiki.nginx.org/NginxChs

Nginx中文资料:http://blog.s135.com/index.php

http://www.51nginx.net/

二、创建www用户和组

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
mkdir -p /data0/htdocs/blog
chmod +w /data0/htdocs/blog
chown -R www:www /data0/htdocs/blog
mkdir -p /data0/htdocs/www
chmod +w /data0/htdocs/www
chown -R www:www /data0/htdocs/www

三、安装nginx-1.0.4
  1、安装Nginx所需的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
make && make install

可能需要的库:

x86_64 系统

wget http://blog.s135.com/soft/linux/nginx_php/rpm/x86_64/libjpeg-devel-6b-33.x86_64.rpm
rpm -ivh libjpeg-devel-6b-33.x86_64.rpm
wget http://blog.s135.com/soft/linux/nginx_php/rpm/x86_64/freetype-devel-2.1.9-1.x86_64.rpm
rpm -ivh freetype-devel-2.1.9-1.x86_64.rpm
wget http://blog.s135.com/soft/linux/nginx_php/rpm/x86_64/libpng-devel-1.2.7-1.x86_64.rpm
rpm -ivh libpng-devel-1.2.7-1.x86_64.rpm

  2、安装Nginx

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

  3、创建Nginx日志目录

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

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

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

内容如下:

Xml代码
user www www;
worker_processes 4;

error_log /usr/local/loadbalancer/logs/nginx_error.log crit;

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

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

events {
use epoll;
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

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;

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;

server {
listen 80;
server_name www.xxx.com xxx.com *.xxx.com;

location / {
proxy_pass http://www.xxx.com:8080;
}

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 /usr/local/loadbalancer/logs/xxx_access.log access;
}

server {
listen 80;
server_name www.ffff.com ffff.com *.ffff.com;

location / {
proxy_pass http://www.fff.com:8082;
}

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 /usr/local/loadbalancer/logs/fff_access.log access;
}
}

user www www;
worker_processes 4;

error_log /usr/local/loadbalancer/logs/nginx_error.log crit;

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

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

events {
use epoll;
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

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;

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;

server {
listen 80;
server_name www.xxx.com xxx.com *.xxx.com;

location / {
proxy_pass http://www.xxx.com:8080;
}

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 /usr/local/loadbalancer/logs/xxx_access.log access;
}

server {
listen 80;
server_name www.ffff.com ffff.com *.ffff.com;

location / {
proxy_pass http://www.fff.com:8082;
}

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 /usr/local/loadbalancer/logs/fff_access.log access;
}
}

  ②、在/usr/local/webserver/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/webserver/nginx/sbin/nginx

--------------------------------------------------------------------------------

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

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

  如果屏幕显示以下两行信息,说明配置文件正确:
  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

  2、平滑重启:
  ①、对于Nginx 0.8.x版本,现在平滑重启Nginx配置非常简单,执行以下命令即可:

/usr/local/webserver/nginx/sbin/nginx -s reload

--------------------------------------------------------------------------------

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

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

  输入以下内容:

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

# The Nginx logs path
logs_path="/usr/local/webserver/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/webserver/nginx/nginx.pid`

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

crontab -e

  输入以下内容:

引用
00 00 * * * /bin/bash /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
分享到:

发表回复

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据