当前位置 博文首页 > 文章内容

    CentOS7 安装nginx并负载mysql

    作者: 栏目:未分类 时间:2020-08-05 16:04:04

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    #1.安装依赖项目:PCRE
    链接: https://pan.baidu.com/s/1JA-Tifch8ftM32znQO1csQ 提取码: svgw
    ./configure
    make && make install
    
    #2.安装依赖项目:libtool 
    链接: https://pan.baidu.com/s/18UohgCRggfhlwcAoVOlkvw 提取码: dnd6
    ./configure
    make && make install
    
    #3.安装nginx
    wget http://nginx.org/download/nginx-1.18.0.tar.gz
    tar -xvf nginx-1.18.0.tar.gz
    cd nginx-1.18.0
    ./configure --with-stream
    make && make install
    
    #4.直接启动
    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    /usr/local/nginx/sbin/nginx -s stop (quit)   #停止nginx
    /usr/local/nginx/sbin/nginx -s reload        #重启nginx
    
    #5.将nginx设置为服务并开机启动
    cat > /usr/lib/systemd/system/nginx.service <<EOF
    [Unit]
    Description=nginx - high performance web server
    Documentation=http://nginx.org/en/docs/
    After=network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    EOF
    
    systemctl daemon-reload
    systemctl enable nginx
    systemctl start nginx
    
    定期清理日志
    
    cat > /usr/local/nginx/clean_nginx_logs.sh <<"EOF"
    
    #!/bin/bash
    
    base_path='/usr/local/nginx/logs'
    log_path=$(date -d yesterday +"%Y%m")
    day=$(date -d yesterday +"%Y%m%d")
    
    mkdir -p $base_path/$log_path
    mv $base_path/access.log $base_path/$log_path/access_$day.log
    mv $base_path/error.log $base_path/$log_path/error_$day.log
    
    
    kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
    EOF
    
    chmod +x /usr/local/nginx/clean_nginx_logs.sh
    
    加入crontab 定期执行日志清理
    执行crontab -e,加入一行
    0 0 * * * /bin/bash /usr/local/nginx/clean_nginx_logs.sh
    
    
    默认配置文件
    
    worker_processes auto;
    worker_rlimit_nofile 100000;
    
    events {
        worker_connections 2048;
        multi_accept on;
        use epoll;
    }
    
    stream{
        upstream mysql {
           hash $remote_addr consistent;
           server 192.168.6.117:3306 weight=5 max_fails=3 fail_timeout=30s;
           server 192.168.6.118:3306 weight=5 max_fails=3 fail_timeout=30s;
        }
        
        server {
           listen 23306;
           proxy_connect_timeout 1s;
           proxy_timeout 3s;
           proxy_pass mysql;
        }
    }
    
    
    http {
    include mime.types;
        default_type application/octet-stream;
    
        log_format main '$time_iso8601, status: $status, request: $http_host $request, spent: $request_time, upstreamAaddr: $upstream_addr, clientIp: $remote_addr, agent: $http_user_agent';
        access_log /var/log/nginx/access.log main;
    
        sendfile on;
        proxy_ignore_client_abort on;
    
        #for upload
        client_max_body_size 60M;
        client_body_buffer_size 128k;
    
        client_header_buffer_size 128k;
        large_client_header_buffers 4 128k;
    
        keepalive_timeout 65;
        send_timeout 60;
        fastcgi_buffers 8 128k;
        proxy_connect_timeout 180;
        proxy_send_timeout 180;
        proxy_read_timeout 640;
        gzip on;
    
        server {
            listen       80;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
        }
    
    
    
        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }