认真是我们
参与这个社会的方式

Debian编译安装Nginx

因为想要使用Nginx进行端口转发,于是需要安装–with-stream模块,之前只使用过lnmp一键脚本,又不想在服务器上怼那么多东西,于是手动安装。同时参考了:

https://raw.githubusercontent.com/helloxz/nginx-cdn/master/nginx.sh

https://www.xiaoz.me/archives/10578

1)进行编译Nginx与OpenSSL,以及你所需要的Nginx第三方模组。

注:这里作者手动编译OpenSSL,是希望安装Openssl-1.1.1版本来支持TLS 1.3,如果不需要支持TLS 1.3则可以不进行编译安装Openssl, 直接通过apt-get install openssl安装

先更新一下

apt-get update

安装依赖

apt-get install build-essential libpcre3 libpcre3-dev zlib1g-dev git -y

注:如果不编译安装Openssl就要加上apt-get install openssl

新增一个编译用的目录,及cd至刚刚新增好的目录位置。

mkdir ~/nginx_build && cd ~/nginx_build

下载新版本的Nginx网页伺服器。

**在nginx: download页面中,你应该可以看到Stable和Mainline两种的Nginx版本,一般来说Mainline版本的新功能会比Stable版本来得多一些,但在稳定性方面是Stable版本会比Mainline版本还要来得高。

Stable版本

wget -c https://nginx.org/download/nginx-1.16.1.tar.gz && tar zxf nginx-1.16.1.tar.gz && rm nginx-1.16.1.tar.gz

注:如果版本号更新了手动改一下链接。

下载支持TLS 1.3协定的OpenSSL版本。

wget -c https://www.openssl.org/source/openssl-1.1.1d.tar.gz && tar zxf openssl-1.1.1d.tar.gz && rm openssl-1.1.1d.tar.gz

版本在OpenSSL-Downloads页面查看

下载需要的第三方模组,如果没有则跳过

**Nginx网站上有列出第三方模块的资源,你可以至NGINX 3rd Party Modules页面来参考有哪些是你所需要的模组。

假设安装ngx_brotli模块

git clone https://github.com/google/ngx_brotli.git
pushd ngx_brotli
git submodule update --init
popd

然后在编译参数里加上

--add-module =/root/nginx_build/ngx_brotli \

注:这里没实际操作

cd至你已解压缩好的Nginx目录位置,以可以设定需要编译的参数。

cd nginx-1.16.1

**因为笔者没有指定prefix路径,所以Nginx等下在安装时,会在预设的/usr/local/nginx/目录,假如你要更改,也是在下面configure参数中进行更改。

下面是我为了进行端口转发所调整的编译配置,同时参考了另外一篇教程里面的参数。

./configure \
--with-stream \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-pcre \
--with-pcre-jit \
--with-openssl=/root/nginx_build/openssl-1.1.1d \
--with-http_sub_module

注意:这里面的 \ 意味着换行,最后一个就不要加了鸭,不然又换行了,还得再回车一下。

 

**Nginx的Log档,预设都会存放在/usr/local/nginx/logs目录,如果需要更改到/var/log/nginx,则在参数中加上

--http-log-path = / var / log / nginx / access.log \
--error-log-path = / var / log / nginx / error.log \

编译及安装

make
make install

测试有没有错误。

/usr/local/nginx/sbin/nginx -t

如果回显以下信息,则代表编译成功完成

nginx: the configuration file / usr / local / nginx / conf / nginx.conf syntax is ok
nginx: configuration file / usr / local / nginx / conf / nginx.conf test is successful

也可以查看Nginx版本和详细资料

/usr/local/nginx/sbin/nginx -V

启动一下试试

/usr/local/nginx/sbin/nginx

2)从GitHub下载shell script,以可以方便管理Nginx网页伺服器及能让Nginx在伺服器开机时能自动执行。

下载管理脚本

编译及安装好Nginx之后,接下来为了可以方便管理Nginx网页伺服器,所以需要建立一个shell script,你可以直接从GitHub下载nginx-startup-script-for-debian-ubuntu.sh至你的『/etc/init.d』目录,并将名称更改为『nginx』。

wget -O /etc/init.d/nginx https://raw.githubusercontent.com/KJieGitHub/Nginx/master/nginx-script/nginx-startup/nginx-startup-script-for-debian-ubuntu.sh

贴一下脚本

#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
  . /etc/default/nginx
fi

set -e

. /lib/lsb/init-functions

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
        --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
        --exec $DAEMON || true
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --pidfile \
        /usr/local/nginx/logs/$NAME.pid --exec $DAEMON || true
    sleep 1
    start-stop-daemon --start --quiet --pidfile \
        /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  reload)
    echo -n "Reloading $DESC configuration: "
    start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
        --exec $DAEMON || true
    echo "$NAME."
    ;;
  status)
    status_of_proc -p /usr/local/nginx/logs/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
    ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
    exit 1
    ;;
esac

exit 0

给予脚本执行权限

chmod +x /etc/init.d/nginx

重载systemd

systemctl daemon-reload

测试指令是否能正常执行

systemctl start nginx

注:在有一台机器上安装时,发现系统内没有systemctl命令,亦无法apt-get安装,我的姿势有问题?如果没办法使用systemctl,那么就手动管理就好,还是要下载管理脚本并赋予权限,然后管理命令就变成了

/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart

管理命令

启动Nginx

sudo systemctl start nginx

停止Nginx

sudo systemctl stop nginx

重启Nginx

sudo systemctl restart nginx

重新载入Nginx的config档

sudo systemctl reload nginx

设置开机启动

update-rc.d -f nginx defaults

3)重启一波试试

 

 

 

 

没有故事 也没有酒

点也没用点也没用