#Linux下通过源码安装Nginx

####1. 安装nginx必须的工具

  • 编译工具

    yum -y install gcc gcc-c++ autoconf automake
    yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
  • 说明
    • zlib : nginx 提供gzip模块,需要zlib库支持
    • openssl:nginx提供ssl功能
    • pcre :支持地址重写rewrite功能

####2.下载nginx并解压

  • Nginx官网下载稳定版本的nginx安装包
  • 解压文件

    tar -zxvf nginx-1.10.3.tar.gz
    cd nginx-1.10.3

####3.创建nginx用户和组


    groupadd -r nginx   
    useradd -s /sbin/nologin -g nginx -r nginx

####4.创建临时目录


    mkdir -pv /var/tmp/nginx/client

####5.执行configure,编译,启动

  • ./configure –help 查看安装模块
  • 执行configure

    ./configure \
    --prefix=/usr \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --pid-path=/var/run/nginx/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --user=nginx \
    --group=nginx \
    --with-http_ssl_module \
    --with-http_flv_module \
    --with-http_gzip_static_module \
    --http-log-path=/var/log/nginx/access.log \
    --http-client-body-temp-path=/var/tmp/nginx/client \
    --http-proxy-temp-path=/var/tmp/nginx/proxy \
    --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
    --with-http_stub_status_module
  • make && make install
  • 启动查看nginx

    /usr/sbin/nginx -c /etc/nginx/nginx.conf
    ps -ef | grep nginx
    netstat -tulnp | grep :80

####6.添加到服务中

  • vim /etc/init.d/nginx 写入如下信息

    #!/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/sbin/nginx
    nginx_config=/etc/nginx/nginx.conf
    nginx_pid=/var/run/nginx/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/nginx.lock
       return $RETVAL
    }
    # Stop nginx daemons functions.
    stop() {
            echo -n $"Stopping $prog: "
            killproc $nginxd
            RETVAL=$?
            echo
            [ $RETVAL = 0 ] && rm -f /var/lock/nginx.lock /var/run/nginx/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
  • 执行下面的命令

    chmod a+x /etc/init.d/nginx 
    chkconfig --add nginx
    chkconfig --list nginx   
  • 验证服务
    • service nginx restart
    • service nginx stop
    • service nginx start