Debian 11 编译安装Nginx

Linux · 2023-03-26
Debian 11 编译安装Nginx

1.安装所需要的依赖软件包

apt install build-essential libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev -y

2.下载Nginx源码
可以前往nginx官网下载最新的stable稳定版本

这里以安装1.23.4版本为例

wget https://nginx.org/download/nginx-1.23.4.tar.gz

解压

tar -xzvf nginx-1.23.4.tar.gz

进入源代码目录

cd nginx-1.23.4

3. 配置和编译
接下来就是make环节了,编译时候的参数可以参考官方Nginx文档:http://nginx.org/en/docs/configure.html

我自己编译Nginx时候,选择的参数一般是:

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--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_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module

没有报错信息就可以编译和安装了:

make -j$(nproc) && make install

等待编译安装完成

4.创建 Nginx 运行使用的用户 nginx:

/usr/sbin/groupadd nginx
/usr/sbin/useradd -g nginx nginx

5.创建systemctl守护,管理Nginx:

vim /etc/systemd/system/nginx.service

写入以下内容:

[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

:wq 保存退出

6.复制/usr/local/nginx/sbin/nginx到/usr/bin/下(比较简单粗暴):

cp /usr/local/nginx/sbin/nginx /usr/bin/nginx

执行nginx -v查看是否有输出

/usr/local/nginx:为Nginx编译安装的地址。
/usr/local/nginx/nginx.conf:Nginx默认配置文件。
我们使用systemctl对Nginx进行管理:
启动Nginx服务

systemctl start nginx

查看nginx运行状态

systemctl status nginx

至此,我们已成功编译安装nginx

Nginx 编译安装
Theme Jasmine by Kent Liao