ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

从零到一:Nginx 1.28.1 源码编译与Systemd服务配置全攻略

从零到一:Nginx 1.28.1 源码编译与Systemd服务配置全攻略 Nginx的源码编译1.下载软件[rootNginx ~]# wget https://nginx.org/download/nginx-1.28.1.tar.gz2.解压[rootNginx ~]# tar zxf nginx-1.28.1.tar.gz[rootNginx ~]# cd nginx-1.28.1/[rootNginx nginx-1.28.1]# lsauto CHANGES.ru conf contrib htmlmanSECURITY.md CHANGES CODE_OF_CONDUCT.md configure CONTRIBUTING.md LICENSE README.md srcauto/编译辅助脚本./configure执行时会调用这里面脚本检测系统环境、生成 Makefile一般不用手动改。conf/源码自带的 nginx 示例配置文件。注意编译安装完成后真正使用的配置文件在安装目录默认/usr/local/nginx/conf这里只是源码里的模板。contrib/附加工具提供 vim 语法高亮脚本、nginx 脚本示例等。html/默认网页页面模板欢迎页、50x 错误页面编译后复制到安装目录 html。man/nginx 帮助手册。src/最重要nginx 全部 C 语言源代码3.检测环境#安装依赖性[rootNginx ~]# dnf install gcc openssl-devel.x86_64 pcre2-devel.x86_64 zlib-devel -y[rootNginx nginx-1.28.1]# ./configure --prefix/usr/local/nginx --usernginx --groupnginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module–prefix/usr/local/nginx安装根目录后续所有程序、配置、日志都放在这个目录。卸载直接删这个文件夹即可。–usernginx --groupnginxnginx 工作进程以 nginx 用户运行必须提前创建 nginx 用户组和用户不能用 root 身份跑业务进程安全规范。–with-http_ssl_module开启 HTTPS ssl 模块实现 443 端口加密访问。–with-http_v2_module开启 HTTP/2 协议网页加载性能优化HTTPS 下生效。–with-http_realip_module获取真实客户端 IPNginx 做反向代理时拿到原始用户 IP而不是代理服务器 IP。–with-http_stub_status_module状态监控模块可以访问/status页面查看连接数、请求数等运行指标。–with-http_gzip_static_module预压缩 gzip 模块直接返回已经压缩好的.gz文件比运行时 gzip 效率更高。–with-pcre编译内置 pcre 正则库rewrite、location 正则匹配。–with-stream四层 TCP/UDP 反向代理模块做端口转发、数据库代理、tcp 负载均衡。–with-stream_ssl_module四层 stream 的 SSL 解密TCP 流量的 SSL 终止。–with-stream_realip_module四层代理下获取真实客户端 IP。4.编译[rootNginx nginx-1.28.1]# make [rootNginx nginx-1.28.1]# make install5.nginx启动#设定环境变量 [rootNginx sbin]# vim ~/.bash_profile export PATH$PATH:/usr/local/nginx/sbin [rootNginx sbin]# source ~/.bash_profile [rootNginx logs]# useradd -s /sbin/nologin -M nginx [rootNginx logs]# nginx [rootNginx logs]# ps aux | grep nginx root 44012 0.0 0.1 14688 2356 ? Ss 17:01 0:00 nginx: master process nginx nginx 44013 0.0 0.2 14888 3892 ? S 17:01 0:00 nginx: worker process root 44015 0.0 0.1 6636 2176 pts/0 S 17:01 0:00 grep --colorauto nginx #测试 [rootNginx logs]# echo timinglee /usr/local/nginx/html/index.html [rootNginx logs]# curl 172.25.254.100 timinglee6.编写启动文件[rootNginx ~]# vim /lib/systemd/system/nginx.service[Unit]DescriptionThe NGINX HTTP and reverse proxy serverAftersyslog.target network-online.target remote-fs.target nss-lookup.targetWantsnetwork-online.target[Service]TypeforkingExecStartPre/usr/local/nginx/sbin/nginx-tExecStart/usr/local/nginx/sbin/nginxExecReload/usr/local/nginx/sbin/nginx-sreloadExecStop/bin/kill-sQUIT$MAINPIDPrivateTmptrue[Install]WantedBymulti-user.target[rootNginx ~]# systemctl daemon-reload#验证[rootNginx ~]# systemctl status nginx.service○ nginx.service - The NGINX HTTP and reverse proxy server Loaded: loaded(/usr/lib/systemd/system/nginx.service;disabled;preset: disabled)Active: inactive(dead)[rootNginx ~]# systemctl enable --now nginx[rootNginx ~]# ps aux | grep nginxroot18390.00.1146882356? Ss 09:530:00 nginx: master process /usr/local/nginx/sbin/nginx nginx18400.00.2148883828? S 09:530:00 nginx: worker process[rootNginx ~]# systemctl status nginx.service
返回列表