1 说明

本文档以吉林考试项目为例,采用编译方式来安装部署nginx

2 目录规划

nginx版本

nginx源码包存放录

nginx安装目录

nginx存放代码目录

nginx日志目录

nginx-1.12.2

nginx-1.12.2

/usr/local/nginx

/home/yunwei/nginx-data

/home/yunwei/nginx-logs

3 下载nginx源码包

创建nginx源码包存放目录

mkdir /usr/local/src/nginx

下载nginx源码包到指定目录

wget http://nginx.org/download/nginx-1.12.2.tar.gz -P /usr/local/src/nginx/

解压nginx源码包

tar xf /usr/local/src/nginx/nginx-1.12.2.tar.gz  -C /usr/local/src/nginx/

4 编译安装nginx

安装依赖包

yum -y install pcre-devel openssl-devel

创建nginx用户

useradd -s /sbin/nologin -M nginx

创建nginx安装目录

mkdir /usr/local/nginx

cd /usr/local/src/nginx/nginx-1.12.2/

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

make && make install

ln -s /usr/local/nginx/sbin/nginx /sbin/nginx

5 创建存放代码和日志目录

mkdir /home/yunwei/nginx-data/jl_exam/jl_exam_poral/ROOT -p

mkdir /home/yunwei/nginx-data/jl_exam/jl_exam_manage_view/ROOT/ -p

mkdir /home/yunwei/nginx-logs/jl_exam/ -p

chmod 755 /home/yunwei/

chown -R nginx.nginx /home/yunwei/nginx-*

6 编辑配置文件

备份配置文件

cp /usr/local/nginx/conf/nginx.conf{,.ori}

创建虚拟主机配置文件存放目录

mkdir /usr/local/nginx/conf/extra/jl_exam

6.1 编辑nginx.conf主配置文件

cat >/usr/local/nginx/conf/nginx.conf <

worker_processes  1;

worker_rlimit_nofile 2048;

events {

    worker_connections  4096;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    tcp_nopush on;

    keepalive_timeout  65;

    fastcgi_intercept_errors on;

    send_timeout 15s;

server_tokens off;

client_max_body_size 50m;    #客户端可上传的最大报文(附件)大小为50MB,默认为1MB

    gzip on;

    gzip_min_length 1k;

    gzip_buffers 4 16k;

    gzip_http_version 1.1;

    gzip_comp_level 2;

    gzip_types text/css text/xml application/javascripts;

gzip_vary on;

client_max_body_size 50m;                                                                      

client_header_timeout 120s;                                                                     

client_body_timeout 120s;                                                                       

client_body_buffer_size 256k;

    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '

                      '\$status \$body_bytes_sent "\$http_referer" '

                      '"\$http_user_agent" "\$http_x_forwarded_for"';

    include extra/jl_exam/jl_exam_poral.conf;

    include extra/jl_exam/jl_exam_manage_view.conf;

}

EOF

6.2 virtual server配置

vim /usr/local/nginx/conf/extra/jl_exam/jl_exam_manage_view.conf

server {                                                                                     

        listen       8081;                                                                       

        server_name  10.0.0.14;

        access_log  /home/yunwei/nginx-logs/jl_exam/jl_exam_manage_view_access.log main;

        error_log  /home/yunwei/nginx-logs/jl_exam/jl_exam_manage_view_error.log;           

        location / {                                                                              

            root   /home/yunwei/nginx-data/jl_exam/jl_exam_manage_view/ROOT;                                                           

        }                                                                                        

        error_page   500 502 503 504  /50x.html;                                                 

        location = /50x.html {                                                                   

            root   html;                                                                          

        }                                                                                        

}

vim /usr/local/nginx/conf/extra/jl_exam/jl_exam_poral.conf

server {

       listen       80;

       server_name  10.0.0.14;

           access_log  /home/yunwei/nginx-logs/jl_exam/jl_exam_poral_access.log main;

       error_log  /home/yunwei/nginx-logs/jl_exam/jl_exam_poral_error.log;

       location / {

           root   /home/yunwei/nginx-data/jl_exam/jl_exam_poral/ROOT;

           index  index.html index.htm;

       }  

       error_page   500 502 503 504  /50x.html;

       location = /50x.html {

           root   html;

       }  

}

7 启动nginx服务

将代码放入相应的站点目录里

nginx    #启动nginx

echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local    #添加到开机自启动

8 日志切割配置

利用系统自带的logrotate进行切割nginx日志文件

创建nginx_jl_exam文件

cat >/etc/logrotate.d/nginx_jl_exam <

/home/yunwei/nginx-logs/jl_exam/*.log{

daily

rotate 10

create

dateext

notifempty

postrotate

        [ -f /usr/local/nginx/logs/nginx.pid ] && kill -USR1 \`cat /usr/local/nginx/logs/nginx.pid\`

endscript

}

EOF

强制执行一次,切割日志,以后每天会定时切割日志

logrotate -f /etc/logrotate.d/nginx_jl_exam