Rocky Linux 9.x 从零安装 Nginx 全流程:源码编译 + dnf 安装方案详解
0.前言
本文目标,安装nginx提供http访问git仓库。关于这篇只是再续,还有续集...
以下安装主要的过程都是在root用户下进行,如果是非root用户,请使用sudo获取root权限执行命令。
1.安装编译依赖
安装全部依赖,如下:
sudo dnf install -y \
gcc gcc-c++ make \ # C/C++编译器和构建工具
openssl-devel pcre-devel zlib-devel \ # 分别支持SSL、正则表达式和压缩功能
redhat-rpm-config \ # 提供RHEL系统的安全编译参数(如RELRO、PIE)
wget tar \ # 用于下载和解压Nginx源码
fcgiwrap # 运行Git HTTP后端(git-http-backend)的FastCGI包装器,起到nginx和git的桥梁作用
先查看有没有安装这些依赖,再调整,前面我已经安装升级了openssl和git,所以简化以上命令为:
sudo dnf install -y redhat-rpm-config fcgiwrap
错误原因:Rocky Linux 9 的默认仓库中没有 fcgiwrap 包。这是Rock Linux的软件包策略发生了变化有关, 启用 EPEL (Extra Packages for Enterprise Linux) 仓库。
sudo dnf install epel-release
再运行依赖安装命令
2.下载Nginx安装包
创建源码目录
sudo mkdir -p /usr/local/src/nginx
cd /usr/local/src/nginx
下载源码
sudo wget https://nginx.org/download/nginx-1.24.0.tar.gz
3.解压
当前源码目录解压:
sudo tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
4.创建安装目录
sudo mkdir -p /u01/nginx
sudo chmod -R 755 /u01/nginx # 设置权限
5.配置编译参数
命令解释:
sudo ./configure \
--prefix=/u01/nginx \ # 指定Nginx安装路径
--with-http_ssl_module \ # 启用HTTPS支持(必需,保护Git通信安全)
--with-http_v2_module \ # 启用HTTP/2协议(提升传输效率,减少延迟)
--with-http_gzip_static_module \ # 启用预压缩文件支持(加速静态资源传输)
--with-http_realip_module \ # 启用真实IP获取(用于反向代理环境,获取客户端原始IP)
--with-pcre \ # 启用PCRE正则表达式支持(用于URL重写和匹配,Git路径解析必需)
--with-stream \ # 启用TCP/UDP代理模块(可用于代理Git的SSH协议,如需要)
--with-stream_ssl_module \ # 启用流模块的SSL支持(用于加密TCP/UDP流量)
--with-threads \ # 启用线程池支持(提升并发处理能力,特别适合IO密集型的Git服务)
--with-http_auth_request_module \ # 启用认证请求模块(用于高级认证,如OAuth、LDAP等外部认证服务)
--with-http_flv_module \ # 启用FLV流媒体模块(可选,用于视频播放,Git服务可能不需要,但保留灵活性)
--with-http_mp4_module \ # 启用MP4流媒体模块(可选,用于视频播放,Git服务可能不需要,但保留灵活性)
--with-http_sub_module \ # 启用内容替换模块(可用于自定义Git Web界面,替换页面元素)
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' # 优化编译参数(提升性能和安全性)
运行一下命令:
cd nginx-1.24.0
sudo ./configure --prefix=/u01/nginx --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_realip_module --with-pcre --with-stream --with-stream_ssl_module --with-threads --with-http_auth_request_module --with-http_flv_module --with-http_mp4_module --with-http_sub_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'
6.编译安装并验证和启动
编译:
sudo make
安装
sudo make install
安装的操作就是拷贝到指定路径咯!
验证,全路径测试安装是否成功
/u01/nginx/sbin/nginx -v
启动
全路径启动
/u01/nginx/sbin/nginx
浏览器可以访问到,说明已经启动成功
如果浏览器访问不到,就查看以下80端口是否开放,
firewall-cmd --list-ports // 端口列表
firewall-cmd --query-port=80/tcp // 指定端口查看
开放端口,重启防火墙
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --reload
7.配置环境变量
sudo tee /etc/profile.d/nginx.sh << 'EOF'
export PATH="/u01/nginx/sbin:$PATH"
EOF
source /etc/profile.d/nginx.sh
系统启动,或用户通过ssh登录就会加载脚本。
配置环境变量,有利于手动启动,启动不写全路径。
8. 开机启动
创建文件
touch /etc/systemd/system/nginx.service
写入内容:
[Unit]
Description=The NGINX HTTP Server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/u01/nginx/logs/nginx.pid
ExecStartPre=/u01/nginx/sbin/nginx -t
ExecStart=/u01/nginx/sbin/nginx
ExecReload=/u01/nginx/sbin/nginx -s reload
ExecStop=/u01/nginx/sbin/nginx -s stop
ExecQuit=/u01/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重载systemd管理器配置
systemctl daemon-reload
创建Nginx服务
systemctl enable nginx
启动服务、查看服务状态
# 立即启动Nginx
systemctl start nginx
# 验证服务状态
systemctl status nginx
# 重启启动Nginx
systemctl restart nginx
重启系统验证是否自动启动nginx
9.加强权限提高安全性(适合生产优化)
这一步很重要,是考虑到了系统被攻击等安全性,而加的一个重要步骤。
查询nginx进程情况
ps -ef | grep nginx
两个进程,一个是主进程master process,另一个是工作进程worker process,主进程由root用户运行,而工作进程由,nobody 用户运行。
nobody用户权限极低,专门用来运行无需高权限的服务或进程,这样即便服务有漏洞被攻击,因 nobody 能访问的资源极少,能把对系统的破坏范围 “锁死”。Nginx 用 nobody 运行工作进程,就算进程被入侵,攻击者也拿不到系统高权限(如 root )。那么我们有一个问题:
那么Nginx工作进程(worker process)已经使用nobody用户运行,那还有必要创建Nginx用户去运行工作进程吗?
答案是:有必要。
因为nobody 是共享低权用户,是系统默认创建的用户,nobody用户有可能也运行其他服务和进程,如果nobody被攻破,就会扩大破坏范围,因此,我们使用Nginx专属用户隔离,创建专属隔离。
创建Nginx专属用户:
sudo useradd --system --shell /sbin/nologin --comment "Nginx Web Server" --user-group --no-create-home nginx
创建的nginx用户特点:
- --system 创建系统用户
- --shell /sbin/nologin 禁止登录
- --comment "Nginx Web Server" 添加注释
- --user-group 创建同名组 nginx
- --no-create-home 不创建家目录
可以查一下,创建是否成功
id nginx
下面是优化过程
9.1. 备份nginx.conf配置文件
cp /u01/nginx/conf/nginx.conf /u01/nginx/conf/nginx.conf.bak
9.2.修改nginx.conf配置
配置运行用户,将以下配置加到nginx.conf 首行。
user nginx nginx;
9.3.修改启动服务文件nginx.service
修改
nano /etc/systemd/system/nginx.service
在[Service]中添加一下片段
[Service]
AmbientCapabilities=CAP_NET_BIND_SERVICE
ProtectSystem=full
ProtectHome=true
修改完后,重新加载
systemctl daemon-reload
9.3.调整 Nginx 目录权限
为了以防万一权限混乱,下面对Nginx目录的权限做备份,运行备份命令:
find /u01/nginx -type f -o -type d -print0 | xargs -0 -I {} bash -c 'stat -c "chown %u:%g %n; chmod %a %n" "{}"' > /u01/nginx_perm_backup.sh
将权限写到nginx_perm_backup.sh文件中,可以查看内容
cat /u01/nginx_perm_backup.sh
chown 0:0 /u01/nginx; chmod 755 /u01/nginx
chown 0:0 /u01/nginx/sbin; chmod 755 /u01/nginx/sbin
chown 0:0 /u01/nginx/conf; chmod 755 /u01/nginx/conf
chown 0:0 /u01/nginx/logs; chmod 755 /u01/nginx/logs
chown 0:0 /u01/nginx/html; chmod 755 /u01/nginx/html
chown 65534:0 /u01/nginx/client_body_temp; chmod 700 /u01/nginx/client_body_temp
chown 65534:0 /u01/nginx/proxy_temp; chmod 700 /u01/nginx/proxy_temp
chown 65534:0 /u01/nginx/fastcgi_temp; chmod 700 /u01/nginx/fastcgi_temp
chown 65534:0 /u01/nginx/uwsgi_temp; chmod 700 /u01/nginx/uwsgi_temp
chown 65534:0 /u01/nginx/scgi_temp; chmod 700 /u01/nginx/scgi_temp
恢复权限使用命令
bash /u01/nginx_perm_backup.sh
还有一种备份方式,高级备份
getfacl -R /u01/nginx > /u01/nginx_perm_backup.acl
恢复使用
setfacl --restore=/u01/nginx_perm_backup.acl
保存了以上权限。
下面开始改权限:
核心原则:最小权限!
比如:
主目录 /u01/nginx 保持 root:nginx ownership,权限 750(root 可操作,nginx 组只读 / 执行,其他用户无权限);
配置目录 conf 设为 root:nginx,权限 750(nginx 组能读配置,但不能修改);
日志目录 logs、临时目录 temp、网页目录 html 设为 nginx:nginx,权限 750/755(nginx 用户可读写日志、生成临时文件、提供网页内容)。
我目录 /u01/nginx的目录如下:
# 递归设置Nginx主目录权限给nginx用户组
chown -R root:nginx /u01/nginx
# 设置配置文件目录权限
chmod -R 750 /u01/nginx/conf
chmod 640 /u01/nginx/conf/nginx.conf
# 设置日志目录权限
chown -R nginx:nginx /u01/nginx/logs
chmod -R 750 /u01/nginx/logs
# 设置临时文件目录权限(若有)
chown -R nginx:nginx /u01/nginx/client_body_temp
chmod -R 750 /u01/nginx/client_body_temp
chown -R nginx:nginx /u01/nginx/fastcgi_temp
chmod -R 750 /u01/nginx/fastcgi_temp
chown -R nginx:nginx /u01/nginx/proxy_temp
chmod -R 750 /u01/nginx/proxy_temp
chown -R nginx:nginx /u01/nginx/scgi_temp
chmod -R 750 /u01/nginx/scgi_temp
chown -R nginx:nginx /u01/nginx/uwsgi_temp
chmod -R 750 /u01/nginx/uwsgi_temp
# 设置HTML目录权限(根据实际情况调整)
chown -R nginx:nginx /u01/nginx/html
chmod -R 755 /u01/nginx/html
验证
su -s /bin/bash -c "touch /u01/nginx/logs/test.log" nginx # 测试日志写入
su -s /bin/bash -c "ls /u01/nginx/html" nginx # 测试网页目录访问
// 安装这种方式验证其他目录的nginx用户权限,验证是为了更好的保证正确,不验证也行
9.4.SELinux权限问题
判断系统是否启用了 SELinux(Security-Enhanced Linux ,安全增强型 Linux )
getenforce
- 如果输出 Enforcing,表示 SELinux 处于强制启用状态,会严格依据策略限制访问。
- 如果输出 Permissive,表示 SELinux 已加载策略,但仅记录违规行为,不实际阻止操作 。
- 如果输出 Disabled,表示 SELinux 已被禁用。
# 查看 Nginx 相关的 SELinux 拒绝规则
grep nginx /var/log/audit/audit.log | audit2allow -m nginx_policy
# 生成并加载策略
grep nginx /var/log/audit/audit.log | audit2allow -M nginx_policy
semodule -i nginx_policy.pp
9.5 完成测试
完成以上的配置后,我重启nginx,查看状态
systemctl restart nginx
systemctl status nginx
正常启动,状态正常。
查看进程是不是nginx用户运行了工作进程:
符合预期,主进程由root用户运行,工作进程由nginx用户运行。
完成!
过几天补一篇文章,关于git安装的,之前的git安装我忽略了 openssl-devel包。