美文网首页
2018-05-16

2018-05-16

作者: 勤能补拙_2aa2 | 来源:发表于2018-05-16 17:20 被阅读0次

    #安装依赖库

    
    yum install -y pcre-devel zlib-devel
    
    

    #进入到安装目录

    
    cd /usr/local/src/
    
    

    #远程下载安装源码

    
    wget http://nginx.org/download/nginx-1.9.9.tar.gz
    
    

    #解压

    
    tar zxvf nginx-1.9.9.tar.gz
    
    

    #进入安装目录

    
    cd nginx-1.9.9
    
    

    #配置安装信息

    
    ./configure \
    
    --user=www \
    
    --group=www \
    
    --prefix=/usr/local/nginx
    
    

    #编译并安装

    
    make && make install
    
    

    #启动nginx测试

    
    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    
    

    #编辑启动脚本,将下面的代码粘贴进去

    
    vi /etc/init.d/nginx
    
    
    
    #!/bin/sh
    
    #
    
    # 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
    
    # config:      /etc/sysconfig/nginx
    
    # pidfile:    /var/run/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/local/nginx/sbin/nginx"
    
    prog=$(basename $nginx)
    
    sysconfig="/etc/sysconfig/$prog"
    
    lockfile="/var/lock/subsys/nginx"
    
    pidfile="/usr/local/nginx/logs/nginx.pid"
    
    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
    
    [ -f $sysconfig ] && . $sysconfig
    
    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 -p $pidfile $prog
    
        retval=$?
    
        echo
    
        [ $retval -eq 0 ] && rm -f $lockfile
    
        return $retval
    
    }
    
    restart() {
    
        configtest_q || return 6
    
        stop
    
        start
    
    }
    
    reload() {
    
        configtest_q || return 6
    
        echo -n $"Reloading $prog: "
    
        killproc -p $pidfile $prog -HUP
    
        echo
    
    }
    
    configtest() {
    
        $nginx -t -c $NGINX_CONF_FILE
    
    }
    
    configtest_q() {
    
        $nginx -t -q -c $NGINX_CONF_FILE
    
    }
    
    rh_status() {
    
        status $prog
    
    }
    
    rh_status_q() {
    
        rh_status >/dev/null 2>&1
    
    }
    
    # Upgrade the binary with no downtime.
    
    upgrade() {
    
        local oldbin_pidfile="${pidfile}.oldbin"
    
        configtest_q || return 6
    
        echo -n $"Upgrading $prog: "
    
        killproc -p $pidfile $prog -USR2
    
        retval=$?
    
        sleep 1
    
        if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]];  then
    
            killproc -p $oldbin_pidfile $prog -QUIT
    
            success $"$prog online upgrade"
    
            echo
    
            return 0
    
        else
    
            failure $"$prog online upgrade"
    
            echo
    
            return 1
    
        fi
    
    }
    
    # Tell nginx to reopen logs
    
    reopen_logs() {
    
        configtest_q || return 6
    
        echo -n $"Reopening $prog logs: "
    
        killproc -p $pidfile $prog -USR1
    
        retval=$?
    
        echo
    
        return $retval
    
    }
    
    case "$1" in
    
        start)
    
            rh_status_q && exit 0
    
            $1
    
            ;;
    
        stop)
    
            rh_status_q || exit 0
    
            $1
    
            ;;
    
        restart|configtest|reopen_logs)
    
            $1
    
            ;;
    
        force-reload|upgrade)
    
            rh_status_q || exit 7
    
            upgrade
    
            ;;
    
        reload)
    
            rh_status_q || exit 7
    
            $1
    
            ;;
    
        status|status_q)
    
            rh_$1
    
            ;;
    
        condrestart|try-restart)
    
            rh_status_q || exit 7
    
            restart
    
            ;;
    
        *)
    
            echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
    
            exit 2
    
    esac
    
    

    #注意权限

    
    chmod +x /etc/init.d/nginx
    
    

    #启动测试

    
    service nginx restart
    
    

    #设置开机启动

    
    chkconfig nginx on
    
    

    #开放防火墙,配置80端口访问

    
    iptables -I INPUT -p tcp --dport 80 -j ACCEPT
    
    

    #保存配置文件,让其生效

    
    service iptables save
    
    

    #无法解析php文件

    #在/usr/local/nginx/html/ 建立phpinfo.php文件,访问发现php文件无法解析

    #修改nginx.conf文件

    
    vi /usr/local/nginx/conf/nginx.conf
    
    

    #加入

    
    location ~ \.php$ {
    
            fastcgi_pass  127.0.0.1:9000;
    
            fastcgi_index  index.php;
    
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
            include        fastcgi_params;
    
    }
    
    

    #重启nginx

    
    service nginx restart
    
    

    相关文章

      网友评论

          本文标题:2018-05-16

          本文链接:https://www.haomeiwen.com/subject/njcmdftx.html