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


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


3、创建Nginx日志目录

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
}

/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;    
}  
}

5、启动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

使配置立即生效:

/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

  或者无需这么麻烦,找到NginxPid文件:

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

  输入以下内容:

#!/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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值