Debian11 安装笔记1:编译安装Nginx、Naxsi 和 Njs

### ssh连接远程服务器自动断开解决

修改 debian 的 /etc/ssh/sshd_config

ClientAliveInterval 30 # 每隔多少秒,服务器端向客户端发送心跳

ClientAliveCountMax 6 # 多少次心跳无响应之后,会认为Client已经断开


systemctl reload 重新加载一下配置

systemctl status sshd.service 查看状态

systemctl start sshd.service 开启服务

systemctl restart sshd.service 重启服务

systemctl enable sshd.service 设置开机启动

### 查看 Swap 交换文件

swapon -s

如果返回的信息概要是空的,则表示 Swap 文件不存在。

https://javascript.net.cn/articles/525

dd if=/dev/zero of=/home/swap bs=1024 count=1024000

名字为/swapfile1 大小为bs*count = 1024*1024000=1024M


### 编译环境准备

更新系统安装包列表

apt update

编译安装时,需要自行安装:gcc、pcre、zlib 以及 openssl

apt install gcc make
apt install libpcre3 libpcre3-dev //【正则表达式库】官网http://www.pcre.org/
apt install openssl libssl-dev   //【openssl库】官网https://www.openssl.org/
apt install zlib1g-dev[未安装]


### 编译 nginx

cd /usr/local/src
wget http://nginx.org/download/nginx-1.20.2.tar.gz
tar -zxf nginx-1.20.2.tar.gz
cd nginx-1.20.2

./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module

make && make install

### 添加到全局

ln -s /usr/local/nginx/sbin/nginx /usr/local/bin

### 创建 nginx 用户

useradd -s /sbin/nologin -M nginx

### 设置服务文件

在 /usr/lib/systemd/system 创建文件 nginx.service

[Unit]
Description=nginx service
After=network.target 

[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true 

[Install] 
WantedBy=multi-user.target


systemctl restart nginx


### 设置 debian 防火墙 ufw

sudo ufw status verbose # 查看规则

ufw allow 80/tcp # 添加80端口


更多设置参考:

ufw enable # 启用防火墙
ufw default deny

ufw allow smtp#允许所有的外部IP访问本机的25/tcp (smtp)端口
ufw allow 22/tcp #允许所有的外部IP访问本机的22/tcp (ssh)端口
ufw allow 53 #允许外部访问53端口(tcp/udp)
ufw allow from 192.168.1.100 #允许此IP访问所有的本机端口
ufw deny smtp #禁止外部访问smtp服务
ufw delete allow smtp #删除上面建立的某条规则
ufw status numbered #得到的所有活动规则的顺序和 ID
ufw delete 4 删除编号为 4

参考:https://blog.csdn.net/ghsyw/article/details/122944900


### 增加 njs

git clone https://github.com/nginx/njs.git

进入 nginx 源码目录, /usr/local/src/njs 是 njs 源码目录

./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--add-dynamic-module=/usr/local/src/njs/nginx


执行 make modules,编译完成后在当前 nginx 源码目录的 objs 目录下会有 njs 的两个模块文件。

ngx_http_js_module.so

复制文件到 nginx 安装目录(即 nginx -V 查看的 --prefix 参数)的 modules 目录下。

或者再次执行 make && make install


### 在 nginx.conf 文件添加:

load_module modules/ngx_http_js_module.so;

events {}

http {
  js_path '/usr/local/nginx/conf/njs/';
  js_import 'test.js';
  server {
    listen 3000;
    location / {
      js_header_filter test.header;
      js_content test.summary;
    }
  }
}


添加文件:/usr/local/nginx/conf/njs/test.js

function summary(r) {
 var a, s, h;

 s = "JS summary\n\n<pre>";

 s += "Time: " + new Date().toLocaleString() + "\n";
 s += "Method: " + r.method + "\n";
 s += "HTTP version: " + r.httpVersion + "\n";
 s += "Host: " + r.headersIn.host + "\n";
 s += "Remote Address: " + r.remoteAddress + "\n";
 s += "URI: " + r.uri + "\n";

 s += "Headers:\n";
 for (h in r.headersIn) {
   s += "  " + h + ": " + r.headersIn[h] + "\n";
 }

 s += "Args:\n";
 for (a in r.args) {
   s += "  " + a + ": " + r.args[a] + "\n";
 }

 s += "\n\n</pre>";

 r.return(200, s);
}

function header(r) {
 r.headersOut['content-type'] = 'text/html';
}

export default {summary, header};


重启 nginx,访问 http://localhost:3000


官网: https://nginx.org/en/docs/njs/index.html

例子: https://github.com/nginx/njs-examples/

njs 内置对象: https://nginx.org/en/docs/njs/reference.html


### 编译时增加 naxsi

下载 naxsi

cd /usr/local/src
git clone https://github.com/nbs-system/naxsi.git


安装

./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-file-aio \
--with-http_dav_module \
--add-module=../naxsi/naxsi_src \
--add-dynamic-module=/usr/local/src/njs/nginx

make && make install

cp /usr/local/src/naxsi/naxsi_config/naxsi_core.rules /usr/local/nginx/conf/


在 nginx.conf 引入:

include /usr/local/nginx/conf/naxsi_core.rules;


### 安装 ModSecurity【放弃安装】

这个需要安装 modSecurity 和连接器 ModSecurity-nginx,编译时间很长。暂时放弃。


### 注意:

2022年6月26日 naxsi 不兼容的 nginx版本是 1.22 和 1.23,可以选择 nginx-1.20.2


/usr/sbin/nginx   #主程序

/etc/nginx     #存放配置文件

/usr/share/nginx  #存放静态文件

/var/log/nginx   #存放日志


声明:本站所有文章和图片,如无特殊说明,均为原创发布。商业转载请联系作者获得授权,非商业转载请注明出处。
真诚赞赏,手留余香
赞赏
随机推荐
WP_Query 函数
WordPress 按自定义排序的两种方法
MySQL 删除逗号分隔字段中的某一个值
Linux netstat 命令
JavaScript 代码混淆加密工具 javascript-obfuscator
WordPress 获取当前主题文件夹的路径
Debian11 安装笔记2:编译安装PHP
Node.js 的 URL 的模块