CentOS 安装Nginx

安装所需的软件

  1. OpenSSL
    • sudo yum install openssl-devel
  2. zlib
    • sudo yum install zlib
  3. PCRE
    • sudo yum install pcre-devel
  4. gcc && gcc-c++
    • sudo yum install gcc gcc-c++
  5. cmake
    • sudo yum install cmake

下载Nginx源码文件

  1. site:https://hg.nginx.org/nginx

  2. 将下载的Nginx源码上传到服务器中

    • scp nginx.tar-gz user@centos:~/nginx/
  3. 解压压缩包

    • tar -xzvf nginx.tar.gz
  4. 配置

    ./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
    
  5. 编译文件

    • sudo make
  6. 安装文件

    • sudo make install
  7. 设置Nginx开机自启动,在/etc/init.d/中创建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() {
        [ -xnginx ] || exit 5
        [ -f NGINX_CONF_FILE ] || exit 6
        echo -n"Starting prog: "
        daemonnginx -c NGINX_CONF_FILE
        retval=?
        echo
        [ retval -eq 0 ] && touchlockfile
        return retval
    }
    
    stop() {
        echo -n"Stopping prog: "
        killprocprog -QUIT
        retval=?
        echo
        [retval -eq 0 ] && rm -f lockfile
        returnretval
    }
    
    restart() {
        configtest || return ?
        stop
        start
    }
    
    reload() {
        configtest || return?
        echo -n "Reloadingprog: "
        killproc nginx -HUP
        RETVAL=?
        echo
    }
    
    force_reload() {
        restart
    }
    
    configtest() {
      nginx -t -cNGINX_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 01
            ;;
        restart|configtest)
            1
            ;;
        reload)
            rh_status_q || exit 71
            ;;
        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. 设置nginx启动服务

    • sudo chkconfig --add nginx
    • sudo chkconfig --level 345 nginx on
  9. 启动nginx服务

    • sudo service nginx start
  10. 添加防火墙策略,允许http、https流量

    • sudo firewall-cmd --zone=public --permanent --add-service=http
    • sudo firewall-cmd --zone=public --permanent --add-service=http
    • sudo firewall-cmd --reload
  11. 参考

    • https://www.digitalocean.com/community/tutorials/how-to-compile-nginx-from-source-on-a-centos-6-4-x64-vps

文章来源于互联网,如有雷同请联系站长删除:CentOS7源码安装Nginx

发表评论