Debian 12环境里部署nginx步骤记录

前言

浅记录一下 在Debian 12环境里安装nginx的过程,这个过程并没有特别顺利,有遇到各种报错,这些报错,我也会记录进来;方便自己后续查看以及供需要的小伙伴参考吧~~ 

主要参考资料:https://blog.youkuaiyun.com/qq_71371157/article/details/141453071

准备工作

准备linux下nginx 的包:可以从其官网下载:https://nginx.org/en/download.html

我这次使用的是 1.26.2的版本:

下载下来:并通过如finallshell、winscp等ssh工具将其传到linux服务器上。(也可以通过wget命令直接下载到linux服务器上)

部署步骤

1、安装依赖:

大多数的linux安装过程,都需要预先安装依赖,而依赖的安装有时候也是需要根据环境具体情况具体分析。 这里大家可以参考网上的资料 。

Debian 的依赖安装是需要使用apt 命令的 :

sudo apt install gcc pcre-devel zlib-devel openssl openssl-devel

然而 却报错了:无法定位软件包 pcre-devel 、zlib-devel、openssl-devel

这里报错了,我就暂时没管了。继续下一步:

2、解压

使tar -zxvf nginx-1.26.2.tar.gz 命令解压,形成与当前压缩文件同名的文件夹:

然后cd 进入解压后的目录:

3、配置nginx

如图所示,configure文件是具备可执行权限才对:

配置nginx命令: ./configure --prefix=/usr/local/nginx  

--prefix= 指向 安装目录  当然还有其他的配置选项,没有特殊需求就用上述命令即可。

错误与处理办法

在这里我就遇到了问题: 诸如:缺少c 编译器 checking for C compiler ... not found

@debian12:/opt/nginx/nginx-1.26.2$ ./configure --prefix=/usr/local/nginx
checking for OS
 + Linux 6.1.0-20-amd64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

于是用命令:

sudo apt install gcc

结果还是有报错。如图:

提示:E: 无法下载 http://security.debian.org/debian-security/pool/updates/main/l/linux/linux-libc-dev_6.1.90-1_amd64.deb  404  Not Found [IP: 146.75.114.132 80]
E: 有几个软件包无法下载,要不运行 apt-get update 或者加上 --fix-missing 的选项再试试?

网上有说 先用 sudo apt-get install build-essential 安装;结果还是错:

然后我就  sudo apt-get update升级了系统,然后再安装:

于是我又用:sudo apt install gcc --fix-missing  去安装gcc的依赖

结果还是报错:缺少PCRE库

./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=<path> option.

这个缺少PCRE库的情况 通过 :sudo apt-get install libpcre3 libpcre3-dev 来安装PCRE库:

结果还是报错了:提示requires the zlib library.

./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=<path> option.

直接用sudo apt-get install zlib就是是不行的,如图:E:无法定位软件包 zlib

缺少 zlib库的处理办法: sudo apt-get install zlib1g-dev

安装好 zlib的库:zlib1g-dev 之后重新配置:

此时就可以配置成功,如下图,本次操作,nginx的安装路径为/usr/local/nginx

4、make

配置完毕就可以进行下一步:make

5、安装

安装的时候需要注意权限:sudo make install 否则可能会因为权限不足无法创建对应的目录:

6、配置服务

sudo nano /usr/lib/systemd/system/nginx.service

内容如下:

Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

进入nano界面后 ctrl+S就可以直接保存

7、设置开机自启动以及启动服务

systemctl enable nginx.service #(设置开机自启动)

systemctl start nginx.service #(启动nginx服务)

systemctl status nginx.service #(查看服务当前状态)

总结

配置nginx的过程难易以及耗时长短,要根据具体情况决定。过程仅供参考,毕竟应该没有人跟我一样动不动就报错吧。。。。。。反正不行就针对错误找资料。。。。

<think>好的,用户想在Debian系统上为FastAPI应用配置Nginx,包括反向代理和优化建议。首先,我需要回顾一下之前提供的引用内容。引用[1]提到了Nginx的常用命令和配置文件位置,而引用[2]则讨论了FastAPI与Nginx、Gunicorn的部署组合,说明Nginx作为反向代理和负载均衡器的作用。 用户的需求明确,需要具体的配置步骤和优化方法。首先,我应该考虑如何将FastAPI应用通过Gunicorn运行,然后配置Nginx作为反向代理。需要确保用户已经安装了必要的组件,比如Python虚拟环境、Gunicorn和Nginx本身。 接下来,步骤可能包括:安装Nginx,创建FastAPI应用的服务文件以便使用systemd管理,编写Nginx服务器块配置,设置反向代理到Gunicorn的本地端口。此外,优化部分可能涉及调整worker进程数、超时设置、启用gzip压缩,以及SSL配置等。 需要检查用户是否可能遇到常见问题,比如权限问题,或者防火墙设置导致无法访问。另外,可能还需要提到如何测试配置文件的正确性,使用nginx -t命令,并重新加载配置。 还要确保示例配置中的端口和路径与Debian系统的标准路径一致,比如Nginx的站点配置文件通常在/etc/nginx/sites-available/,然后符号链接到sites-enabled。可能需要提醒用户替换示例中的域名和路径为实际使用的值。 关于优化,除了基本的反向代理设置,可能还需要考虑静态文件处理、负载均衡(如果有多个Gunicorn实例)、以及使用HTTP/2等现代协议。同时,SSL部分可以使用Let's Encrypt的Certbot工具自动配置,这也是Debian上的常见做法。 最后,应该生成相关问题,帮助用户进一步了解相关主题,比如如何用Certbot获取SSL证书,或者如何优化Gunicorn的配置,以及监控Nginx性能的方法。</think>以下是 Debian 系统上为 FastAPI 配置 Nginx 的完整指南,包含反向代理设置和优化建议: ### 一、基础配置步骤 1. **安装 Nginx** ```bash sudo apt update && sudo apt install nginx ``` 2. **创建 FastAPI 服务文件** 创建 `/etc/systemd/system/fastapi.service`: ```ini [Unit] Description=FastAPI Application After=network.target [Service] User=www-data Group=www-data WorkingDirectory=/path/to/your/app ExecStart=/path/to/gunicorn -k uvicorn.workers.UvicornWorker -w 4 -b 127.0.0.1:8000 main:app [Install] WantedBy=multi-user.target ``` 3. **配置 Nginx 反向代理** 创建 `/etc/nginx/sites-available/fastapi`: ```nginx server { listen 80; server_name yourdomain.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 保持长连接优化 proxy_http_version 1.1; proxy_set_header Connection ""; } # 静态文件处理优化 location /static { alias /path/to/static/files; expires 30d; access_log off; } } ``` ### 二、关键优化设置 1. **Worker 进程优化** ```nginx worker_processes auto; # 自动匹配CPU核心数 events { worker_connections 1024; # 每个worker的最大连接数 multi_accept on; # 同时接受多个新连接 } ``` 2. **缓冲与超时设置** ```nginx proxy_buffering on; proxy_buffer_size 4k; proxy_buffers 8 16k; proxy_busy_buffers_size 24k; proxy_temp_file_write_size 32k; proxy_connect_timeout 75s; proxy_send_timeout 7200s; proxy_read_timeout 7200s; ``` 3. **Gzip 压缩配置** ```nginx gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; gzip_proxied any; gzip_comp_level 6; gzip_min_length 1024; ``` ### 三、SSL 安全配置 建议使用 Certbot 获取免费证书: ```bash sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d yourdomain.com ``` ### 四、完整配置验证流程 1. 检查 Nginx 配置: ```bash sudo nginx -t ``` 2. 重载配置: ```bash sudo systemctl reload nginx sudo systemctl restart fastapi ``` ### 五、监控与维护建议 1. 启用状态监控: ```nginx location /nginx_status { stub_status; allow 127.0.0.1; deny all; } ``` 2. 日志轮转配置: ```bash sudo nano /etc/logrotate.d/nginx ``` [^1][^2]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值