Logo
在debian上编译nginx

在debian上编译nginx

April 11, 2025
1 min read
大纲

在debian上安装nginx

debian默认软件库的nginx没有fancy-index模块, fancy-index是一个html文件服务器, 下面就开始手动给nginx添加fancy-index模块。

1. 安装编译环境

shell
sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev
# 包含pcre(正则库), zlib(压缩库), ssl(https)

2. 下载并解压nginx源码

官网查看最新版本(当前20250111为1.26.3) https://nginx.org/en/download.html

shell
wget https://nginx.org/download/nginx-1.26.3.tar.gz
tar -xf nginx-1.26.3.tar.gz

3. 下载并解压nginx-fancyindex模块

shell
cd nginx-1.26.3
wget https://github.com/aperezdc/ngx-fancyindex/releases/download/v0.5.2/ngx-fancyindex-0.5.2.tar.xz
tar -xf ngx-fancyindex-0.5.2.tar.xz

4. 配置需要编译的模块

shell
# NGINX_ROOT_PATH = nginx的安装目录, 需要替换成你想要安装的目录
mkdir -p /var/cache/nginx
./configure --add-module=./ngx-fancyindex-0.5.2 \
--prefix=${NGINX_ROOT_PATH} \
--user=jimlee \
--group=jimlee \
--sbin-path=${NGINX_ROOT_PATH}/sbin/nginx \
--conf-path=${NGINX_ROOT_PATH}/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-http_v3_module

5. 编译

shell
sudo make && sudo make install

6. 创建systemctl服务

systemd
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
 
[Service]
Type=forking
ExecStart=${NGINX_ROOT_PATH}/sbin/nginx
ExecReload=${NGINX_ROOT_PATH}/sbin/nginx -s reload
ExecStop=${NGINX_ROOT_PATH}/sbin/nginx -s quit
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

7. 通过systemctl管理nginx

shell
systemctl start nginx
systemctl status nginx

8. 修改setcap让普通用户可以使用1024以下端口

shell
setcap cap_net_bind_service=+eip ${NGINX_ROOT_PATH}/sbin/nginx