码迷,mamicode.com
首页 > 其他好文 > 详细

Centos7 编译安装Nginx

时间:2019-10-31 21:57:37      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:inux   编译   return   get   kconfig   关于   zlib   cas   centos   

  1. 升级系统所有软件

    yum -y update
  2. 安装Nginx编译所需要的依赖项

    yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel
  3. 下载Nginx源码

    #方式一:
    wget http://nginx.org/download/nginx-1.9.15.tar.gz
    #方式二:
    git clone git@github.com:nginx/nginx.git
  4. 解压 并 进入解压后的文件夹

    tar -xzf nginx-1.9.15.tar.gz
    cd nginx-1.9.15
  5. 选择安装的模块

    ./configure --user=nginx --group=nginx --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio --with-http_realip_module --without-http_scgi_module --without-http_uwsgi_module --without-http_fastcgi_module
  6. 编译并安装

    make -j 4 && make install
  7. 在init.d文件夹中创建nginx启动文件

    cd /etc/init.d
    
    vim nginx
    
    #添加以下内容
    #!/bin/sh
    #
    # nginx - this script starts and stops the nginx daemin
    #
    # 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.pid
    # user:        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
    
    nginx="/usr/sbin/nginx"
    prog=$(basename $nginx)
    
    NGINX_CONF_FILE="/etc/nginx/nginx.conf"
    
    lockfile=/var/run/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
        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
  8. 将上面写的脚本添加到系统服务中(关于chkconfig请参考:Linux下chkconfig命令详解

chkconfig --add nginx
chkconfig --level 345 nginx on

# 给脚本执行的权限
chmod +x nginx
  1. Linux 防火墙开放80端口

    firewall-cmd --permanent --add-port=80/tcp --zone=public
  2. Nginx 开启关闭命令

    service nginx start/restart/stop

注:本文转自译:Centos7 编译安装Nginx 教程,如需转载请注明出处,https://www.cnblogs.com/zhuchenglin/p/11773815.html。

Centos7 编译安装Nginx

标签:inux   编译   return   get   kconfig   关于   zlib   cas   centos   

原文地址:https://www.cnblogs.com/zhuchenglin/p/11773815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!