zoukankan      html  css  js  c++  java
  • nginx的安装配置

    nginx的安装配置

    1、下载安装PCRE, 是一个用C语言编写的正则表达式函数库

    2、下载zlib库:zlib-1.2.8.tar.gz cd zlib-1.2.8

    3、下载SSL:openssl-1.0.1c.tar.gz

      yum install -y openssl-devel   【很重要,经常报错无法安装】

    4、下载nginx:nginx-1.2.8.tar.gz

    5、解压安装 :./configure && make && make install

           ./configure --prefix=/usr/local/nginx && make && make install

    ./configure --prefix=/usr/local/nginx-1.8.1 --with-http_ssl_module --with-http_spdy_module--with-http_stub_status_module --with-pcre

    6、启动nginx: /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

    7、设置开机自动启动(shell脚本处理)

    [root@localhost logs]# vi /etc/init.d/nginx
     
    添加以下shell脚本。
     
    #!/bin/bash
    # nginx Startup script for the Nginx HTTP Server
    # it is v.0.0.2 version.
    # chkconfig: - 85 15
    # description: Nginx is a high-performance web and proxy server.
    #              It has a lot of features, but it's not for everyone.
    # processname: nginx
    # pidfile: /var/run/nginx.pid
    # config: /usr/local/nginx/conf/nginx.conf
    nginxd=/usr/local/nginx/sbin/nginx
    nginx_config=/usr/local/nginx/conf/nginx.conf
    nginx_pid=/var/run/nginx.pid
    RETVAL=0
    prog="nginx"
    # Source function library.
    . /etc/rc.d/init.d/functions
    # Source networking configuration.
    . /etc/sysconfig/network
    # Check that networking is up.
    [ ${NETWORKING} = "no" ] && exit 0
    [ -x $nginxd ] || exit 0
    # Start nginx daemons functions.
    start() {
    if [ -e $nginx_pid ];then
       echo "nginx already running...."
       exit 1
    fi
       echo -n $"Starting $prog: "
       daemon $nginxd -c ${nginx_config}
       RETVAL=$?
       echo
       [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
       return $RETVAL
    }
    # Stop nginx daemons functions.
    stop() {
            echo -n $"Stopping $prog: "
            killproc $nginxd
            RETVAL=$?
            echo
            [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
    }
    # reload nginx service functions.
    reload() {
        echo -n $"Reloading $prog: "
        #kill -HUP `cat ${nginx_pid}`
        killproc $nginxd -HUP
        RETVAL=$?
        echo
    }
    # See how we were called.
    case "$1" in
    start)
            start
            ;;
    stop)
            stop
            ;;
    reload)
            reload
            ;;
    restart)
            stop
            start
            ;;
    status)
            status $prog
            RETVAL=$?
            ;;
    *)
            echo $"Usage: $prog {start|stop|restart|reload|status|help}"
            exit 1
    esac
    exit $RETVAL
    保存后,设置权限让所有人可以操作。
    [root@localhost ~]# chmod a+x /etc/init.d/nginx 
    [root@localhost logs]#  /etc/init.d/nginx status
    nginx (pid 2417 2416) is running...
    [root@localhost logs]#  /etc/init.d/nginx stop
    Stopping nginx: [  OK  ]
    [root@localhost logs]#  /etc/init.d/nginx start
    Starting nginx: [  OK  ]
    [root@localhost logs]#  /etc/init.d/nginx status
    nginx (pid 2454 2452) is running...
    [root@localhost ~]# vi /etc/rc.local
    添加:
    /etc/init.d/nginx start
     
    重启电脑就会生效。
    注意:脚本里面关于启动目录的,如果你的安装地址不一样,要进行修改。
     
     
    ===============================================================
     
    负载均衡配置:
    现有两部服务器:
    192.168.137.197     (按照以上操作安装有nginx,作为转发机,虚拟机)
    192.168.137.33     (无安装nginx,运行有测试用的web工程,虚拟机)
    10.10.33.59    (无安装nginx,运行有测试用的web工程,本地电脑地址)
    ,运行有测试用的web工程
    127.0.0.1 web_app
    [root@a conf]# vi /usr/local/nginx/conf/nginx.conf
    配置如下展示:
    worker_processes 8;
    pid /usr/local/nginx/nginx.pid;
    worker_rlimit_nofile 102400;
    events
    {
    use epoll;
    worker_connections 102400;
    }
    http
    {
      include       mime.types;
      default_type  application/octet-stream;
      fastcgi_intercept_errors on;
      charset  utf-8;
      server_names_hash_bucket_size 128;
      client_header_buffer_size 4k;
      large_client_header_buffers 4 32k;
      client_max_body_size 300m;
      sendfile on;
      tcp_nopush     on;

      keepalive_timeout 60;

      tcp_nodelay on;
      client_body_buffer_size  512k;

      proxy_connect_timeout    5;
      proxy_read_timeout       60;
      proxy_send_timeout       5;
      proxy_buffer_size        16k;
      proxy_buffers            4 64k;
      proxy_busy_buffers_size 128k;
      proxy_temp_file_write_size 128k;
      gzip on;
      gzip_min_length  1k;
      gzip_buffers     4 16k;
      gzip_http_version 1.1;
      gzip_comp_level 2;
      gzip_types       text/plain application/x-javascript text/css application/xml;
      gzip_vary on;

    ###2012-12-19 change nginx logs
    log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent"  $request_time $remote_addr';

    upstream web_app {
    server 192.168.137.197:8080 weight=1 max_fails=2 fail_timeout=30s;
    server 192.168.137.33:8080 weight=1 max_fails=2 fail_timeout=30s;
    server 10.10.33.59:8080 weight=1 max_fails=2 fail_timeout=30s;
    }  

    ####chinaapp.sinaapp.com
    server {  
        listen 80; 
        server_name  chinaapp.sinaapp.com;
        index index.jsp index.html index.htm;
        #发布目录/data/www
        root  /data/www;

        location / 
        {  
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        proxy_set_header Host  $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://web_app;
        expires      3d;
        }  
      }  
    }
    设置host:
    [root@a conf]# vi /etc/hosts
    添加hosts:
    127.0.0.1 web_app
     
    启动nginx
    [root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
     
    启动三部服务器的web应用:
    在192.168.137.197执行指令:
    [root@a PROGRAM]# curl "http://web_app/index.jsp"
    测试结果如下:
     
     
     
    常见问题及解决办法:
    1、安装PCRE时提示configure: error: You need a C++ compiler for C++ support.
    原因是没有安装c++编译器,采用下面的命令安装:
    [root@localhost pcre-8.37]# yum install -y gcc gcc-c++
     
    2、启动nginx失败
    [root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    /usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
    原因是64bit的系统,但是默认取了/usr/local/lib里面的包
    检查方法:
    [root@localhost nginx]# cd /usr/local/
    [root@localhost local]# ls /lib64/ |grep pcre
    libpcre.so.0
    libpcre.so.0.0.1
    [root@localhost local]# ls /lib/ |grep pcre
    说明缺失的包在lib64
    设置软连接来解决:
    [root@localhost local]# ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1
    [root@localhost local]# cd nginx
    [root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    3、启动报错:
    报错:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    解决办法:sudo fuser -k 80/tcp
    端口被占用,关闭占用端口
  • 相关阅读:
    Log4php使用指南
    【JQuery】使用JQuery 合并两个 json 对象
    【前端】JS截取字符串常用方法详细整理
    【.Net】net 反射15分钟速成
    【.Net】win10 uwp unix timestamp 时间戳 转 DateTime
    【ASP.NET Core】ASP.NET Core 依赖注入
    【ASP.NET 框架系列】您所经历的,但未必研究的那些技术
    Visual Studio 中设置npm
    【数据库】SQL分组多列统计(GROUP BY后按条件分列统计)
    【数据库】同一字段根据不同条件更新的sql语句的写法
  • 原文地址:https://www.cnblogs.com/chenjw-note/p/5683019.html
Copyright © 2011-2022 走看看