Chinaunix首页 | 论坛 | 博客
  • 博客访问: 403068
  • 博文数量: 124
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 872
  • 用 户 组: 普通用户
  • 注册时间: 2018-03-29 14:38
个人简介

默默的一块石头

文章分类

全部博文(124)

文章存档

2022年(26)

2021年(10)

2020年(28)

2019年(60)

我的朋友

分类: LINUX

2021-11-11 11:41:18

手动编译安装nginx
0.前期准备
1.下载nginx源码包并解压
2.编译
3. 其他命令
4. 官方启动脚本内容,用于开机自启(以下内容未测试)
0.前期准备
sudo apt-get update
#安装依赖:gcc、g++依赖库
sudo apt-get install build-essential libtool
#安装 pcre依赖库()
sudo apt-get install libpcre3 libpcre3-dev
#安装 zlib依赖库()
sudo apt-get install zlib1g-dev
#安装ssl依赖库
sudo apt-get install openssl

1.下载nginx源码包并解压
wget
tar xvzf nginx-1.15.8.tar.gz
cd nginx-1.15.8/

2.编译
切换到解压后的nginx目录中执行:

sudo ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock

要激活 debug 日志,Nginx 在构建时需要配置为支持 debug:
./configure --with-debug

sudo make
sudo make install

参数说明:
编译选项官方提供的有:
–prefix=path 定义一个目录来保存你的nginx的提供功能的文件夹,就这好比我们安装软件的时候软件存放的目录,如果我们在编译的不指定安装位置,那么默认的位置/usr/local/nginx 目录
–sbin-path=path 设置nginx执行脚本的位置,这里如果设置在path变量里面,就可以在bash环境下,任意使用nginx命令,默认位置prefix/sbin/nginx 注意这里的prefix是
在配置文件里面配置的路径
–conf-path=path 配置nginx配置文件的路径,如果不指定这个选项,那么配置文件的默认路径就会是 prefix/conf/nginx.conf
–pid-path =path 配置nginx.pid file的路径,一般来说,进程在运行的时候的时候有一个进程id,这个id会保存在pid file里面,默认的pid file的放置位置是prefix/logs/nginx.pid
–error-log-path=path 设置错误日志的存放路径,如果不指定,就默认 prefix/logs/error.log
–http-log-path= path 设置http访问日志的路径,如果不指定,就默认 prefix/logs/access.log
–user=name 设置默认启动进程的用户,如果不指定,就默认 nobody
–group=name 设置这个用户所在的用户组,如果不指定,依然是nobody
这些是我们常用的编译选项,其他的可以均保持默认,如需特殊指定,可上nginx官网查阅
下面是一些不常用的选项
–with-http_ssl_module -开启HTTP SSL模块,使NGINX可以支持HTTPS请求。需要安装了OPENSSL
–with-http_flv_module
–with-http_stub_status_module - 启用 “server status” 页(可有可无)
–without-http_gzip_module - 禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。
–without-http_ssi_module - 禁用 ngx_http_ssi_module
–without-http_referer_module - 禁用 ngx_http_referer_module
–without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。
–without-http_proxy_module - 禁用 ngx_http_proxy_module
–without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module
–without-http_memcached_module - 禁用 ngx_http_memcached_module
–without-http_browser_module - 禁用 ngx_http_browser_module
–http-proxy-temp-path=PATH - Set path to the http proxy temporary files
–http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files
–without-http - 禁用 HTTP server(用作代理或反向代理)
–with-mail - 启用 IMAP4/POP3/SMTP 代理模块
–with-mail_ssl_module - 启用 ngx_mail_ssl_module
–with-openssl=DIR - Set path to OpenSSL library sources

可能出现的错误:
错误1:./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre= option.
解决办法:sudo apt-get install libpcre3 libpcre3-dev

错误2:./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib= option.
解决办法:下载zlib库,地址:/

wget /zlib-1.2.11.tar.gz
tar xvzf zlib-1.2.11.tar.gz
cd zlib-1.2.11/
./configure
make
#检查,要全部为yes
make check
sudo make install

错误3:SSL modules require the OpenSSL library
解决办法:sudo apt-get install openssl libssl-dev

3. 其他命令
#查找安装路径
whereis nginx
#启动
sudo nginx
#查看Nginx进程
ps -ef|grep nginx
#Nginx常见命令
sudo nginx -s stop
sudo nginx -s quit
sudo nginx -s reload

nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

注:-s都是采用向 Nginx 发送信号的方式

4. 官方启动脚本内容,用于开机自启(以下内容未测试)
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/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      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
    fi
}

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


————————————————
版权声明:本文为CSDN博主「风轻云断」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/A156348933/article/details/85335089

背景

Nginx(Tengine) 代理 Bitbucket 的 tcp 端口时报错:unknown directive "stream", 记录下~

解决

  1. 编译时,通过添加 --with-stream 参数来安装 stream 模块。
  2. 需要手工加载 ngx_stream_module.so 模块。

参考


阅读(1488) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~