nginx 安装、配置、负载均衡、反向代理<一>

来源:互联网 发布:js改变浏览器窗口大小 编辑:程序博客网 时间:2024/06/03 19:42

nginx 安装

本文主讲nginx的安装
nginx的负载均衡,反向代理,gzip压缩配置等,详见:nginx 安装、配置、负载均衡、反向代理<二>

准备工作: 一台能联公网的CentOS 7.x服务器

    # root 登录(因为要使用80端口,一般情况下需要root权限)    cd ~    # 新建nginx用户和nginx组    groupadd -r nginx && useradd -r -g nginx -s /bin/false -M nginx    # yum安装nginx必须的依赖库(已安装的会自动监测是否为最新并升级到最新版本)    yum -y install openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed    # 官网下载Nginx1.9.0的tar包,然后解压到服务器上    wget -c http://nginx.org/download/nginx-1.9.0.tar.gz    tar -zxf nginx-1.9.0.tar.gz && cd nginx-1.9.0    # 下载pcre的tar包并解压,以便支持Nginx的Rewrite功能    wget -c https://o3cex9zsl.qnssl.com/libs/nginx/pcre-8.36.tar.gz && tar -zxf pcre-8.36.tar.gz    # 下载zlib的tar包并解压,以便支持Nginx的Gzip压缩功能    wget -c https://o3cex9zsl.qnssl.com/libs/nginx/zlib-1.2.8.tar.gz && tar -zxf zlib-1.2.8.tar.gz    # 新建Nginx1.9.0安装时所需要的目录    cd /var/tmp/ && mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}    mkdir -p /var/run/nginx && cd ~/nginx-1.9.0    # 至此,准备工作基本完成

配置安装参数

    # 执行 ./configure 命令安装,下面是完整的路径配置和安装,__注意不能有换行__,具体配置含义见文末    ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_spdy_module --with-http_dav_module --with-http_flv_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_stub_status_module --with-http_sub_module --with-http_random_index_module --with-http_degradation_module --with-http_secure_link_module --with-http_gzip_static_module --with-http_perl_module --with-pcre=pcre-8.36 --with-zlib=zlib-1.2.8 --with-debug --with-file-aio --with-mail --with-mail_ssl_module --http-client-body-temp-path=/var/tmp/nginx/client_body --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-stream --with-ld-opt="-Wl,-E"

编译和安装

    # 执行make编译,make install 安装(可能需要时间稍微长点儿,几分钟到十几分钟,请耐心等待)    make && make install    # 测试安装是否成功,能显示版本号就说明没问题    nginx -v

配置nginx的操作命令和开机自动重启

    # 进入root目录    cd ~    # 编辑nginx文件    vi nginx    # 文件内容如下,粘贴时注意核对下有没有漏掉,然后保存退出    # 添加到systemctl初始化目录中    mv ~/nginx /etc/init.d/nginx && chmod +x /etc/init.d/nginx    # 删除不必要的文件夹    rm -rf nginx-1.9.0*    # 添加开机启动    chkconfig --add nginx    chkconfig nginx on

nginx命令配置文件的内容

    #! /bin/bash    #    # nginx - this script starts and stops the nginx daemon    #    # chkconfig:   - 85 15    # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \    #               proxy and IMAP/POP3 proxy server    #    # processname: nginx    # config:      /etc/nginx/nginx.conf    # pidfile:     /var/run/nginx/nginx.pid    # Source function library.    . /etc/rc.d/init.d/functions    # Source networking configuration.    . /etc/sysconfig/network    # Check that networking is up.    [ "$NETWORKING" = "no" ] && exit 0    nginx="/usr/sbin/nginx"    prog=$(basename $nginx)    NGINX_CONF_FILE="/etc/nginx/nginx.conf"    [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx    lockfile=/var/lock/nginx.lock    start() {        [ -x $nginx ] || exit 5        [ -f $NGINX_CONF_FILE ] || exit 6        echo -n $"Starting $prog: "        daemon $nginx -c $NGINX_CONF_FILE        retval=$?        echo        [ $retval -eq 0 ] && touch $lockfile        return $retval    }    stop() {        echo -n $"Stopping $prog: "        killproc $prog -QUIT        retval=$?        echo        [ $retval -eq 0 ] && rm -f $lockfile        return $retval    }    restart() {        configtest || return $?        stop        sleep 1        start    }    reload() {        configtest || return $?        echo -n $"Reloading $prog: "        killproc $nginx -HUP        RETVAL=$?        echo    }    force_reload() {        restart    }    configtest() {    $nginx -t -c $NGINX_CONF_FILE    }    rh_status() {        status $prog    }    rh_status_q() {        rh_status >/dev/null 2>&1    }    case "$1" in        start)            rh_status_q && exit 0            $1            ;;        stop)            rh_status_q || exit 0            $1            ;;        restart|configtest)            $1            ;;        reload)            rh_status_q || exit 7            $1            ;;        force-reload)            force_reload            ;;        status)            rh_status            ;;        condrestart|try-restart)            rh_status_q || exit 0                ;;        *)            echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"            exit 2    esac

测试安装和配置结果

    # nginx 版本    nginx -v    # 启动Nginx服务    systemctl start nginx    # 停止nginx服务    systemctl stop nginx    # 重启Nginx服务    systemctl restart nginx    # 服务的状态(可以查看当前nginx服务是否正常,以及报错原因)    systemctl status nginx    # 检查nginx.conf配置的合法性(一般用于编辑完nginx.conf文件后,看看编辑是否有问题)    nginx -t

以上,安装工作完成 =================================================

附:nginx的安装参数详述

配置项 说明 ./configure –prefix=/usr/share/nginx Nginx安装目录 –sbin-path=/usr/sbin/nginx Nginx的sbin目录 –conf-path=/etc/nginx/nginx.conf Nginx的配置文件 –error-log-path=/var/log/nginx/error.log Nginx的错误日志 –http-log-path=/var/log/nginx/access.log Nginx的访问日志 –pid-path=/var/run/nginx/nginx.pid Nginx的进程ID –lock-path=/var/lock/nginx.lock –user=nginx Nginx所属用户 –group=nginx Nginx所属用户组 –with-http_ssl_module Nginx的ssl模块 –with-http_spdy_module[Nginx的Google spdy模块 –with-http_dav_module –with-http_flv_module –with-http_realip_module –with-http_addition_module –with-http_xslt_module –with-http_stub_status_module –with-http_sub_module –with-http_random_index_module –with-http_degradation_module –with-http_secure_link_module –with-http_gzip_static_module Nginx的gzip压缩模块 –with-http_perl_module –with-pcre=pcre-8.36 pcre的安装目录 –with-zlib=zlib-1.2.8 pcre的安装目录 –with-debug 允许DEBUG –with-file-aio –with-mail –with-mail_ssl_module –http-client-body-temp-path=/var/tmp/nginx/client_body –http-proxy-temp-path=/var/tmp/nginx/proxy –http-fastcgi-temp-path=/var/tmp/nginx/fastcgi –http-uwsgi-temp-path=/var/tmp/nginx/uwsgi –http-scgi-temp-path=/var/tmp/nginx/scgi –with-stream Nginx1.9.0特有的stream模块 –with-ld-opt=”-Wl,-E” gcc的编译优化

+ 参考资料:blog地址https://typecodes.com/web/centos7compilenginx.html