Xinu

Nginx--Linxu下编译安装Nginx
文档官网文档地址 http://nginx.org/en/docs/configure.html获取资源 wget...
扫描右侧二维码阅读全文
03
2019/06

Nginx--Linxu下编译安装Nginx

logo

文档

官网文档地址 http://nginx.org/en/docs/configure.html

获取资源


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

安装

常用编译选项

文件和权限

--prefix=path

定义 nginx 文件的安装路径。configure 的其他选项如果使用相对路径,那么以此路径为基础路径。(except for paths
to libraries sources)。nginx.conf 文件中的相对路径也以此为基础路径。默认
--prefix=/usr/local/nginx

--sbin-path=path

设置 nginx 二进制程序的路径名,这个名字只在安装期间使用。 默认 --sbin-path=prefix/sbin/nginx

--conf-path=path

设置 nginx.conf 的路径。nginx 可在启动时手动以 -c file 参数指定其他配置文件。默认
--conf-path=prefix/conf/nginx.conf

--pid-path=path

设置 nginx.pid 文件的路径。安装nginx之后,可在 nginx.conf 文件中使用 pid 指令修改该路径。默认
--pid-path=prefix/logs/nginx.pid

--error-log-path=path

设置 nginx 错误日志的路径。安装nginx之后,可在 nginx.conf 文件中使用 error_log 指令修改该路径。默认
--error-log-path=prefix/logs/error.log

--http-log-path=path

设置 nginx 访问日志的路径。安装nginx之后,可在 nginx.conf 文件中使用 access_log 指令修改该路径。默认
--http-log-path=prefix/logs/access.log

--user=name

设置启动 worker 进程时所使用的非特权用户名。安装nginx之后,可在 nginx.conf 文件中使用 user
指令修改用户名。默认 --user=nobody

--group=name

设置启动 worker 进程时所使用的非特权用户组名。安装nginx之后,可在 nginx.conf 文件中使用 group
指令修改用户组名。默认 --group=nobody

事件循环

--with-select_module
--without-select_module

是否编译 select 模块。使用 select 模块可使 nginx 工作于 select() 模式。 如果 nginx
不支持其他更合适的模块,如 kqueue, epoll 或者 /dev/poll,该模块被自动编译。

--with-poll_module
--without-poll_module

是否编译 poll 模块。使用 poll 模块可使 nginx 工作于 poll() 模式。 如果 nginx 不支持其他更合适的模块,如
kqueue, epoll 或者 /dev/poll,该模块被自动编译。

可选模块

--without-http_gzip_module

不编译 gzip 压缩模块。压缩模块用于压缩 HTTP 响应报文。该模块的编译和运行依赖 zlib 库。

--without-http_rewrite_module

不编译 rewrite 模块。rewrite 模块用于重定向 HTTP 请求,也可以改写 HTTP 请求的 URI。该模块的编译和运行依赖
PCRE 库。

--without-http_proxy_module

不编译代理模块

--with-http_ssl_module

编译 ssl 模块。ssl 模块使 nginx 支持 HTTPs 协议。该模块默认不编译。该模块的编译和运行依赖 OpenSSL 库。

--with-pcre=path

设置PCRE库源的路径。库分发(版本4.4-8.43)需要从PCRE站点下载并提取。其余由nginx的./configuremake完成。对于location指令中的正则表达式支持和ngx_http_rewrite_module模块,需要使用库。

--with-pcre-jit

编译 PCRE 库时,加入 “just-in-time compilation” 支持 (1.1.12, the pcre_jit directive)

--with-zlib=path

设置 zlib 库的源码路径。首先需要下载和解压 zlib 库。 要求 zlib 库的版本范围为 1.1.3 -1.2.8,设置之后,其余的就交给 ./configure 和 make 命令。gzip 压缩模块依赖 zlib 库。

编译控制

--with-cc-opt=parameters

为 CFLAGS 变量设置额外的参数。比如 FreeBSD 下使用 PCRE 库,必须指定 --with-cc-opt="-I
/usr/local/include"。 比如 希望增加 select() 支持的文件数,可指定:--with-cc-opt="-D
FD_SETSIZE=2048"

--with-ld-opt=parameters

设置链接时的额外参数。比如,FreeBSD 使用 PCRE 库时,必须指定 --with-ld-opt="-L
/usr/local/lib"。

开始安装

tar -zxvf nginx-1.16.0.tar.gz
cd nginx-1.16.0
mkdir -p /server/nginx
./configure --prefix=/server/nginx \
--user=www \
--group=www \
--with-http_ssl_module \
--with-pcre=/server/source/pcre-8.41

make
make install

编写启动脚本

vim /etc/init.d/nginxd
#--------以下为脚本内容--------

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   3 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /server/nginx/conf/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /server/nginx/logs/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="/server/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/server/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/server/nginx/logs/nginx.lock
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    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

启动服务

service nginxd start

# 停止服务
service nginxd stop
# 重启
service nginxd restart
# 检测配置文件语法
service nginxd configtest

添加开机自启

chkconfig --add nginxd

注意
脚本中的# chkconfig: 3 85 15这一行注释不要删掉,chkconfig通过此参数来设置启动项
启动服务设置可以参看 这里

Last modification:June 3rd, 2019 at 11:02 pm

Leave a Comment