2013-11-12 13:01:01
来 源
itjs.cn
Nginx
本文介绍Nginx服务器的编译安装与配置,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。

Nginx :0.8.15

1、安装Nginx所需的pcre库:

#cd /usr/local/src/tarbag

#tar zxvf pcre-7.9.tar.gz -C ../software

#cd ../software/pcre-7.9/

#./configure

#make && make install

#cd ../../tarbag

#cd /usr/local/src/tarbag

#tar zxvf pcre-7.9.tar.gz -C ../software

#cd ../software/pcre-7.9/

#./configure

#make && make install

#cd ../../tarbag

2、安装Nginx

#tar zxvf nginx-0.8.15.tar.gz -C ../software

#cd ../software/nginx-0.8.15/

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

#make && make install

#cd ../../tarbag

#tar zxvf nginx-0.8.15.tar.gz -C ../software

#cd ../software/nginx-0.8.15/

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

#make && make install

#cd ../../tarbag 3、创建Nginx日志目录

mkdir -p /opt/www/nginx/logs

chmod +w /opt/www/nginx/logs

chown -R nobody:nobody /opt/www/nginx/logs

mkdir -p /opt/www/nginx/logs

chmod +w /opt/www/nginx/logs

chown -R nobody:nobody /opt/www/nginx/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 nobody nobody;

worker_processes 8;

error_log /opt/www/nginx/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;

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;

log_format access '$remote_addr - $remote_user [$time_local] '

"$request" $status $bytes_sent '

'"$http_referer" "$http_user_agent" '

"$gzip_ratio"';

log_format wwwlogs '$remote_addr - $remote_user [$time_local] '

'"$request" $status $bytes_sent '

'"$http_referer" "$http_user_agent" '

'"$http_range" "$sent_http_content_range"';

include vhosts.conf

}

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

vi /usr/local/nginx/conf/nginx.conf

user  nobody nobody;

worker_processes 8;

error_log  /opt/www/nginx/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;

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;

log_format  access    '$remote_addr - $remote_user [$time_local] '

"$request" $status $bytes_sent '

'"$http_referer" "$http_user_agent" '

"$gzip_ratio"';

log_format  wwwlogs  '$remote_addr - $remote_user [$time_local] '

'"$request" $status $bytes_sent '

'"$http_referer" "$http_user_agent" '

'"$http_range" "$sent_http_content_range"';

include         vhosts.conf

}

在/usr/local/nginx/conf/目录中创建vhosts.conf文件:

vi /usr/local/nginx/conf/vhosts.conf

#limit_zone crawler $binary_remote_addr 10m;

server

{

listen 80;

server_name www.hsf.com;

index index.html index.htm index.php;

root /www/blog;

#limit_conn crawler 20;

access_log /opt/www/nginx/logs/access.log access;

}

server

{

listen 80;

server_name www.hsf.com;

index index.html index.htm index.php;

root /www/www;

access_log /www/nginx/wwwlogs.log wwwlogs;

}

server

{

listen 80;

server_name status.www.hsf.com;

location /

{

stub_status on;

access_log off;

}

}

vi /usr/local/nginx/conf/vhosts.conf

#limit_zone  crawler  $binary_remote_addr  10m;

server

{

listen       80;

server_name  www.hsf.com;

index index.html index.htm index.php;

root  /www/blog;

#limit_conn   crawler  20;

access_log  /opt/www/nginx/logs/access.log  access;

}

server

{

listen       80;

server_name  www.hsf.com;

index index.html index.htm index.php;

root  /www/www;

access_log  /www/nginx/wwwlogs.log  wwwlogs;

}

server

{

listen  80;

server_name  status.www.hsf.com;

location /

{

stub_status on;

access_log   off;

}

}

5、启动Nginx

ulimit -SHn 65535

/usr/local/nginx/sbin/nginx

ulimit -SHn 65535

/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

# 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

/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

  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}'

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/nginx.pid`

kill -HUP `cat /usr/local/nginx/nginx.pid`   七、编写每天定时切割Nginx日志的脚本

  1、创建脚本/usr/local/scripts/cut_nginx_log.sh

vi /usr/local/scripts/cut_nginx_log.sh

vi /usr/local/scripts/cut_nginx_log.sh

输入以下内容:

#!/bin/bash

# This script run at 00:00

# The Nginx logs path

logs_path="/www/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/nginx.pid`

#!/bin/bash

# This script run at 00:00

# The Nginx logs path

logs_path="/www/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/nginx.pid`

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

crontab -e

输入以下内容:

00 00 * * * /bin/bash /usr/local/scripts/cut_nginx_log.sh

00 00 * * * /bin/bash  /usr/local/scripts/cut_nginx_log.sh

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。