1. 首页
  2. Web服务器

Debian12 上编译安装 Nginx 1.27.2

一、安装前准备

1.源码下载地址

# 源码包地址
http://nginx.org/download/

# 本次安装使用的源码包是 1.27.2
http://nginx.org/download/nginx-1.27.2.tar.gz

2.配置选项说明

http://nginx.org/en/docs/configure.html

3.关于 openssl,安装时如果带有 --with-openssl 配置选项,则需要指定 openssl 源码包地址,因此需要先下载 openssl  源码包

# openssl  源码包
https://openssl-library.org/source/old/index.html

# 本次安装使用的 openssl 版本 1.1.1
https://github.com/openssl/openssl/releases/download/OpenSSL_1_1_1w/openssl-1.1.1w.tar.gz

4.创建 nginx 的用户和用户组

# 创建用户组
groupadd www

# 创建用户,并将用户添加到用户组(如果后续需要安装PHP,则PHP可以和 nginx 共用同一个用户和用户组)
useradd -g www www

二、更新系统

apt update
apt upgrade -y

三、安装依赖项,编译 Nginx 需要一些编译工具和库。

apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libxml2 libxslt-dev

四、下载 Nginx 及 openssl 源码,并解压源码

# nginx 源码
wget http://nginx.org/download/nginx-1.27.2.tar.gz

tar -xzvf nginx-1.27.2.tar.gz

# openssl 源码
wget https://github.com/openssl/openssl/releases/download/OpenSSL_1_1_1w/openssl-1.1.1w.tar.gz
tar -xzvf openssl-1.1.1w.tar.gz

# 如果因网络原因下载失败,可以先下载下来,然后上传到服务器

五、进入到解压的 nginx 目录,并配置编译选项

# 进入到解压的 nginx 目录
cd nginx-1.27.2/

# 你可以根据需要启用或禁用模块,最后一个配置项,需要将 解压后的openssl源码目录 替换为 真实目录,如:--with-openssl=/root/openssl-1.1.1w
./configure --prefix=/usr/local/nginx --user=www --group=www --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-http_addition_module --with-http_sub_module --with-http_mp4_module --with-http_stub_status_module --with-stream_ssl_module --with-stream_realip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_realip_module --with-http_dav_module  --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_slice_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre  --with-pcre-jit --with-stream=dynamic --with-stream_ssl_preread_module --with-openssl=解压后的openssl源码目录

六、编译和安装

# -j$(nproc) 选项会根据 CPU 核心数进行并行编译,加速编译过程。
make -j$(nproc) && make install

七、验证是否安装成功

# 命令行运行,如果显示出 nginx 版本,则表示安装成功
/usr/local/nginx/sbin/nginx -v

八、配置 Nginx

1.安装完成后,配置文件通常位于 /usr/local/nginx/conf/目录下,根据需要编辑该目录下的文件。本文章底部附件提供了针对 1.27.2 的配置文件,可将原conf目录删除,用附件里的conf目录替换!

# 删除原配置文件
cd /usr/local/nginx/
rm -rf conf

# 在当前目录下,使用rz命令上传 conf.zip 文件
rz 

# 上传成功后使用 unzip 命令 解压缩配置目录
unzip conf.zip

# 主要配置文件及目录说明
conf  配置文件目录
├─example                        示例文件目录,包含代理配置,网站配置等
├─rewrite                        路由重写配置目录,如wordpress,thinkphp,laravel
├─vhost                          网站配置目录,新增站点配置文件均放到该目录
├─enable-php.conf                PHP类型网站配置文件
├─enable-php-pathinfo.conf       PHP类型网站配置文件
├─nginx.conf                     主配置文件,控制 Nginx 的全局设置和核心功能。
├─mime.types                     定义 MIME 类型与文件扩展名的映射关系。
├─fastcgi.conf                   配置与 FastCGI 服务器(例如 PHP-FPM)的通信。
├─fastcgi_params                 配置 Nginx 与 FastCGI 服务器之间通信的参数
├─scgi_params                    SCGI 协议的参数配置文件。
├─uwsgi_params                   uWSGI 协议的参数配置文件。
├─koi-utf                        字符集转换映射文件
├─koi-win                        字符集转换映射文件
├─win-utf                        用于 Windows 系统的字符集转换文件。
├─......                         其它

2.创建 nginx 日志文件目录,及网站文件目录,并将目录权限给到 www 用户和 www 用户组

# 配置文件里,将 /server 目录作为日志和网站文件根目录
mkdir /server /server/log /server/www /server/www/default

# 复制nginx默认页面
cp -r /usr/local/nginx/html/* /server/www/default/

# 更改目录所属用户和用户组
chown -R www:www /server/log
chown -R www:www /server/www

3.配置系统服务,实现开机自动启动

1.创建一个新的 systemd 服务文件 /etc/systemd/system/nginx.service。
2.将以下内容复制到该文件中:
[Unit]
Description=The NGINX HTTP and reverse proxy 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 -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false

[Install]
WantedBy=multi-user.target

3.重新加载 systemd,以便它知道新的服务:
systemctl daemon-reload

4.启动 Nginx 服务,并设置为开机启动:
systemctl start nginx
systemctl enable nginx

4.对于Nginx服务,常用的systemctl命令

# 启动Nginx服务:
systemctl start nginx

# 停止Nginx服务:
systemctl stop nginx

# 重新加载Nginx配置文件:
systemctl reload nginx

# 重启Nginx服务:
systemctl restart nginx

# 查看Nginx服务状态:
systemctl status nginx

九、配置网站

1.以添加 dev.com 为示例,将 conf/example 目录下的 dev.com 文件复制到  conf/vhost 目录下

cp /usr/local/nginx/conf/example/dev.com.conf /usr/local/nginx/conf/vhost/dev.com.conf

2.创建目录 /server/www/dev.com

mkdir /server/www/dev.com

3.创建文件,并在 文件写入 hello world

touch /server/www/dev.com/index.html
vim /server/www/dev.com/index.html

4.重新加载nginx配置文件

nginx -s reload

5.做好 dev.com 的域名解析,即可成功访问啦

十、编译参数说明参考

--prefix=/usr/local/nginx:指定Nginx安装目录为/usr/local/nginx,即Nginx的安装路径。
--user=www:指定运行Nginx的用户为www,用于限制Nginx进程的权限。
--group=www:指定运行Nginx的用户组为www,用于限制Nginx进程的权限。
--with-select_module:启用select模块,用于支持select事件模型。
--with-poll_module:启用poll模块,用于支持poll事件模型。
--with-threads:启用线程支持,用于支持多线程。
--with-file-aio:启用文件异步I/O,用于支持文件异步读写。
--with-http_ssl_module:启用HTTP SSL模块,用于支持HTTPS协议。
--with-http_v2_module:启用HTTP/2模块,用于支持HTTP/2协议。
--with-http_v3_module:启用HTTP/3模块,用于支持HTTP/3协议。
--with-http_addition_module:启用HTTP添加模块,用于添加HTTP响应内容。
--with-http_sub_module:启用HTTP替换模块,用于替换HTTP响应内容。
--with-http_mp4_module:启用HTTP MP4模块,用于处理MP4视频文件。
--with-http_stub_status_module:启用HTTP状态模块,用于获取Nginx运行状态。
--with-stream_ssl_module:启用Stream SSL模块,用于支持SSL/TLS协议。
--with-stream_realip_module:启用Stream RealIP模块,用于设置真实客户端IP。
--with-http_gzip_static_module:启用HTTP Gzip静态模块,用于对静态文件进行Gzip压缩。
--with-http_auth_request_module:启用HTTP认证请求模块,用于处理认证请求。
--with-http_secure_link_module:启用HTTP安全链接模块,用于生成安全链接。
--with-http_random_index_module:启用HTTP随机索引模块,用于设置随机索引文件。
--with-http_realip_module:启用HTTP RealIP模块,用于设置真实客户端IP。
--with-http_dav_module:启用HTTP WebDAV模块,用于支持WebDAV协议。
--with-http_degradation_module:启用HTTP降级模块,用于处理降级请求。
--with-http_flv_module:启用HTTP FLV模块,用于处理FLV视频文件。
--with-http_gunzip_module:启用HTTP Gzip解压模块,用于解压Gzip压缩的响应。
--with-http_slice_module:启用HTTP切片模块,用于支持HTTP切片下载。
--with-http_xslt_module=dynamic:启用HTTP XSLT模块,用于支持XSLT转换。
--with-mail=dynamic:启用Mail模块,用于支持邮件服务。
--with-mail_ssl_module:启用Mail SSL模块
--with-pcre:指定PCRE库的路径,用于支持正则表达式。
--with-pcre-jit:启用PCRE的Just-In-Time编译,用于提高正则表达式匹配性能。
--with-stream=dynamic:启用Stream模块,用于支持TCP和UDP代理。
--with-stream_ssl_preread_module:启用Stream SSL预读模块,用于提前读取SSL握手信息。
--with-openssl=解压后的openssl源码目录:指定OpenSSL库的路径,用于支持SSL加密。

TOP