国产麒麟服务器实战:从零编译定制版 Nginx(适配银河麒麟 V10)

411 阅读4分钟

🎯 背景与目标

在国产化浪潮下,越来越多企业选择使用 银河麒麟服务器操作系统(Kylin Linux Advanced Server V10) 作为底层平台。但在实际部署中,我们常遇到一个问题:

系统自带或默认安装的 Nginx 功能不全 —— 没有 SSL、没有状态监控、路径不规范,启动还要加 -c -p 参数,非常不便!

本文将手把手教你:

✅ 在银河麒麟 V10 上,从源码重新编译安装 Nginx,并定制以下参数:

--prefix=/nginx/nginx
--sbin-path=/nginx/sbin/nginx
--conf-path=/nginx/conf/nginx.conf
--error-log-path=/nginx/logs/error.log
--http-log-path=/nginx/logs/access.log
--pid-path=/nginx/logs/nginx.pid
--with-http_stub_status_module   # 状态监控
--with-http_ssl_module           # HTTPS支持
--with-http_gzip_static_module   # 静态压缩
--with-pcre                      # 正则支持

最终实现:

# 一键启动,无需指定配置文件路径!
/nginx/sbin/nginx

🧭 第一步:确认你的麒麟系统类型

麒麟服务器有两个主流版本:

系统名称基础发行版包管理器识别命令
中标麒麟(NeoKylin)RHEL/CentOSyum / dnfcat /etc/os-release → 含 NeoKylin
银河麒麟(Kylin V10)Ubuntu/Debianapt/yumcat /etc/os-release → 含 Kylin Linux

📌 请执行以下命令确认:

cat /etc/os-release

如果你看到:

NAME="Kylin Linux Advanced Server"
VERSION="V10 (Sword)"
ID=kylin
ID_LIKE=debian

→ 恭喜,你使用的是 银河麒麟 V10


🛠️ 第二步:安装编译依赖

在银河麒麟上,使用 apt 安装开发工具和依赖库:

sudo apt update
sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev

💡 build-essential 包含 gcc、make 等工具链,是 Ubuntu/Debian 系开发标配。


📦 第三步:下载并解压 Nginx 源码

⚠️ 重要:不要在安装目录(如 /nginx/nginx)内操作!否则会报错:cp: 'xxx' and 'xxx' are the same file

推荐在 /tmp 目录下操作:

cd /tmp
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0

✅ 推荐使用稳定版(Stable version),目前最新为 1.24.x。主线版(Mainline)功能新但可能不稳定。


⚙️ 第四步:配置编译参数

执行 ./configure,传入你想要的所有路径和模块:

./configure \
--prefix=/nginx/nginx \
--sbin-path=/nginx/sbin/nginx \
--conf-path=/nginx/conf/nginx.conf \
--error-log-path=/nginx/logs/error.log \
--http-log-path=/nginx/logs/access.log \
--pid-path=/nginx/logs/nginx.pid \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-pcre

✅ 成功后,你会看到类似输出:

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library
  ...

❗ 如果报错 SSL modules require the OpenSSL library,请执行:

sudo apt install -y libssl-dev

然后重新执行 ./configure ...


🔨 第五步:编译 & 安装

make
sudo make install

⚠️ 此操作会覆盖 /nginx/sbin/nginx 二进制文件,但不会覆盖你原有的配置文件(如 nginx.conf),除非你手动删除或强制覆盖。


✅ 第六步:验证与启动

1. 查看编译参数

/nginx/sbin/nginx -V

输出中应包含你配置的所有 --with-xxx--xxx-path 参数。

2. 测试配置文件

/nginx/sbin/nginx -t

3. 启动服务

# 停止旧进程(如有)
/nginx/sbin/nginx -s stop

# 启动新 Nginx
/nginx/sbin/nginx

🎉 恭喜!现在你可以直接使用:

/nginx/sbin/nginx

而无需在像以前需要加 -c-p 参数!


🚀 进阶:配置 systemd 服务(推荐生产环境)

创建服务文件:

sudo vim /lib/systemd/system/nginx.service

粘贴以下内容:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/nginx/logs/nginx.pid
ExecStartPre=/nginx/sbin/nginx -t
ExecStart=/nginx/sbin/nginx
ExecReload=/nginx/sbin/nginx -s reload
ExecStop=/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

启用并启动:

sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx

现在你可以使用:

sudo systemctl restart nginx
sudo systemctl reload nginx

优雅管理服务!


🧩 常见问题 FAQ

❓ Q2:报错 cp: 'conf/koi-win' and '/nginx/conf/koi-win' are the same file

因为你在安装目录内编译了源码。解决方法:换到 /tmp 或其他目录重新编译。

❓ Q3:如何备份原有配置?

sudo cp /nginx/sbin/nginx /nginx/sbin/nginx.bak
sudo cp -r /nginx/conf /nginx/conf.bak

📌 总结

通过本文,你已掌握:

  • 如何识别麒麟系统类型;
  • 如何在银河麒麟上正确安装编译依赖;
  • 如何定制化编译 Nginx,集成常用模块;
  • 如何避免路径冲突导致的安装失败;
  • 如何配置 systemd 实现服务化管理。

这套方法不仅适用于 Nginx,也适用于其他需要源码编译的软件(如 PHP、OpenResty、Tengine 等),是国产化环境运维的必备技能!


📚 参考资料