一、环境,依赖安装
root@ecs-7398:~# apt-get update #更新软件包列表
root@ecs-7398:~# apt-get upgrade #更新已安装的软件包
root@ecs-7398:~# apt-get install -y make gcc libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev #安装依赖
make: GNU工具集的一部分,用于自动化编译任务。
gcc: GNU编译器集合,用于编译和链接C程序。
libpcre3 和 libpcre3-dev: Perl兼容正则表达式库的运行时库和开发文件。
zlib1g-dev: 压缩算法库的开发文件。
openssl 和 libssl-dev: OpenSSL加密库的运行时库和开发文件。
二、下载 nginx 源码包
Nginx源码包下载
上传源码包到服务器
root@ecs-7398:~# ls
nginx-1.20.2.tar.gz
root@ecs-7398:~# mkdir nginx
root@ecs-7398:~# ls
nginx nginx-1.20.2.tar.gz
root@ecs-7398:~# tar -zxvf nginx-1.20.2.tar.gz -C nginx #解压缩tar包
root@ecs-7398:~# cd nginx/nginx-1.20.2/
root@ecs-7398:~/nginx/nginx-1.20.2# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
三、源码编译安装
root@ecs-7398:~/nginx/nginx-1.20.2# ./configure --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_gzip_static_module --with-http_v2_module --with-http_realip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module
root@ecs-7398:~/nginx/nginx-1.20.2# make
root@ecs-7398:~/nginx/nginx-1.20.2# make install
root@ecs-7398:~/nginx/nginx-1.20.2# cd /usr/local/nginx/
root@ecs-7398:/usr/local/nginx# ls
conf html logs sbin
root@ecs-7398:/usr/local/nginx# ./sbin/nginx #启动Nginx
root@ecs-7398:/usr/local/nginx# ss -pantu | grep nginx #检查是否启动成功
tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=43285,fd=6),("nginx",pid=43284,fd=6))
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload 重新加载配置
–prefix=/usr/local/nginx: 指定 Nginx 安装的根目录。这意味着,编译安装完成后,Nginx 的主要文件和目录(如二进制文件、配置文件、日志文件等)将被放置在 /usr/local/nginx 目录下。
–with-pcre: 表示 Nginx 将使用 PCRE(Perl Compatible Regular Expressions)库,它允许 Nginx 使用正则表达式进行灵活的配置,比如重写规则、访问控制等。
–with-http_ssl_module: 启用 SSL/TLS 支持,允许 Nginx 提供 HTTPS 服务。
–with-http_gzip_static_module: 启用静态文件压缩模块,这允许 Nginx 在发送前对静态文件进行 gzip 压缩,以节省带宽和提高传输速度。
–with-http_v2_module: 启用 HTTP/2 协议支持,HTTP/2 提供了更高效的数据传输机制,如多路复用、头部压缩等。
–with-http_realip_module: 启用真实 IP 模块,它允许 Nginx 处理经过负载均衡器或其他代理传递的真实客户端 IP 地址。
–with-http_sub_module: 启用字符串替换模块,它允许在响应中替换文本。
–with-http_dav_module: 启用 WebDAV 模块,支持基于 Web 的分布式创作和版本控制。
–with-http_flv_module: 启用 FLV(Flash Video)流媒体模块,支持以流的方式传输 FLV 视频文件。
四、创建系统服务
编辑 nginx.service
root@ecs-7398:/usr/local/nginx# vim /lib/systemd/system/nginx.service
root@ecs-7398:/usr/local/nginx# cat /lib/systemd/system/nginx.service
[Unit]
Description=nginx web server
After=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /usr/local/nginx/logs/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s QUIT $(/bin/cat /usr/local/nginx/logs/nginx.pid)"
PrivateTmp=true
[Install]
WantedBy=multi-user.target
root@ecs-7398:~# systemctl daemon-reload
root@ecs-7398:~# systemctl start nginx.service
root@ecs-7398:~# systemctl status nginx.service
root@ecs-7398:~# systemctl enable nginx.service
root@ecs-7398:~# systemctl reload nginx.service