银河麒麟服务器系统编译安装新版本(指定版本)nginx
目录
前言
一、nginx安装
(一)环境准备
1.系统环境
2.禁用防火墙、安装环境依赖
(二)编译安装
1.官网下载nginx源码包,选择自己需要的版本下载
2.编译安装nginx
(1)解压nginx源码包
(2)预编译
(3)编译安装
二、使用systemd管理nginx服务
1.编写service文件
2.使用systemctl命令管理nginx服务
三、验证nginx是否安装成功
总结
前言
随着国产化技术的快速发展,银河麒麟服务器操作系统凭借其高安全性和稳定性,逐渐成为企业级应用的重要选择。作为核心的Web服务引擎,Nginx在高并发、负载均衡等场景中表现卓越,掌握其编译安装方法有助于深度适配系统环境,提升服务性能。本文将以简洁的步骤,介绍如何在银河麒麟系统中从源码编译安装Nginx,为开发者们提供一份轻量级实践指南。
一、nginx安装
(一)环境准备
1.系统环境
[root@localhost nginx-1.26.3]# cat /etc/.kyinfo
[dist]
name=Kylin-Server
milestone=V10
arch=x86_64
beta=False
time=2024-05-08 10:33:19
dist_id=Kylin-Server-V10-SP3-2403-Release-20240426-x86_64-2024-05-08 10:33:19
[servicekey]
key=0415040
[os]
to=
term=2025-09-01
2.禁用防火墙、安装环境依赖
# 禁止防火墙自启动并立刻关闭防火墙
systemctl disable firewalld.service --now
# 开放端口80、443不关闭防火墙也可以
# 安装所需软件
yum -y install gcc make pcre-devel openssl-devel
# gcc:GNU 编译器集合(C/C++ 等语言的编译器)
# make:自动化构建工具,根据 Makefile 文件自动执行编译、链接等流程,简化源码安装步骤
# pcre-devel:Perl 兼容正则表达式库的开发包,提供 Nginx 的 rewrite 模块所需的正则解析支持(如 URL 重写规则)
# openssl-devel:OpenSSL 加密库的开发包,为 Nginx 提供 HTTPS(SSL/TLS)支持,实现数据加密传输
[root@localhost ~]# systemctl disable firewalld --now
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# yum -y install gcc make pcre-devel openssl-devel
Last metadata expiration check: 1:05:38 ago on 2025年04月17日 星期四 08时59分25 秒.
Package gcc-7.3.0-2020033101.58.p01.ky10.x86_64 is already installed.
Package make-1:4.3-1.ky10.x86_64 is already installed.
Package pcre-devel-8.44-2.p02.ky10.x86_64 is already installed.
Package openssl-devel-1:1.1.1f-31.p23.ky10.x86_64 is already installed.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Upgrading:
openssl x86_64 1:1.1.1f-31.p29.ky10 ks10-adv-updates 449 k
openssl-devel x86_64 1:1.1.1f-31.p29.ky10 ks10-adv-updates 1.8 M
openssl-help noarch 1:1.1.1f-31.p29.ky10 ks10-adv-updates 3.2 M
openssl-libs x86_64 1:1.1.1f-31.p29.ky10 ks10-adv-updates 1.4 M
openssl-perl x86_64 1:1.1.1f-31.p29.ky10 ks10-adv-updates 17 k
Transaction Summary
================================================================================
Upgrade 5 Packages
Total download size: 6.8 M
Downloading Packages:
(1/5): openssl-1.1.1f-31.p29.ky10.x86_64.rpm 1.6 MB/s | 449 kB 00:00
(2/5): openssl-devel-1.1.1f-31.p29.ky10.x86_64. 3.3 MB/s | 1.8 MB 00:00
(3/5): openssl-help-1.1.1f-31.p29.ky10.noarch.r 5.3 MB/s | 3.2 MB 00:00
(4/5): openssl-perl-1.1.1f-31.p29.ky10.x86_64.r 82 kB/s | 17 kB 00:00
(5/5): openssl-libs-1.1.1f-31.p29.ky10.x86_64.r 2.7 MB/s | 1.4 MB 00:00
--------------------------------------------------------------------------------
Total 8.5 MB/s | 6.8 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Upgrading : openssl-libs-1:1.1.1f-31.p29.ky10.x86_64 1/10
Running scriptlet: openssl-libs-1:1.1.1f-31.p29.ky10.x86_64 1/10
Upgrading : openssl-help-1:1.1.1f-31.p29.ky10.noarch 2/10
Upgrading : openssl-1:1.1.1f-31.p29.ky10.x86_64 3/10
Upgrading : openssl-perl-1:1.1.1f-31.p29.ky10.x86_64 4/10
Upgrading : openssl-devel-1:1.1.1f-31.p29.ky10.x86_64 5/10
Cleanup : openssl-devel-1:1.1.1f-31.p23.ky10.x86_64 6/10
Cleanup : openssl-perl-1:1.1.1f-31.p23.ky10.x86_64 7/10
Cleanup : openssl-1:1.1.1f-31.p23.ky10.x86_64 8/10
Cleanup : openssl-help-1:1.1.1f-31.p23.ky10.noarch 9/10
Cleanup : openssl-libs-1:1.1.1f-31.p23.ky10.x86_64 10/10
Running scriptlet: openssl-libs-1:1.1.1f-31.p23.ky10.x86_64 10/10
Verifying : openssl-1:1.1.1f-31.p29.ky10.x86_64 1/10
Verifying : openssl-1:1.1.1f-31.p23.ky10.x86_64 2/10
Verifying : openssl-devel-1:1.1.1f-31.p29.ky10.x86_64 3/10
Verifying : openssl-devel-1:1.1.1f-31.p23.ky10.x86_64 4/10
Verifying : openssl-help-1:1.1.1f-31.p29.ky10.noarch 5/10
Verifying : openssl-help-1:1.1.1f-31.p23.ky10.noarch 6/10
Verifying : openssl-libs-1:1.1.1f-31.p29.ky10.x86_64 7/10
Verifying : openssl-libs-1:1.1.1f-31.p23.ky10.x86_64 8/10
Verifying : openssl-perl-1:1.1.1f-31.p29.ky10.x86_64 9/10
Verifying : openssl-perl-1:1.1.1f-31.p23.ky10.x86_64 10/10
Upgraded:
openssl-1:1.1.1f-31.p29.ky10.x86_64
openssl-devel-1:1.1.1f-31.p29.ky10.x86_64
openssl-help-1:1.1.1f-31.p29.ky10.noarch
openssl-libs-1:1.1.1f-31.p29.ky10.x86_64
openssl-perl-1:1.1.1f-31.p29.ky10.x86_64
Complete!
(二)编译安装
1.官网下载nginx源码包,选择自己需要的版本下载
nginx官网:nginx
[root@localhost ~]# wget https://nginx.org/download/nginx-1.26.3.tar.gz
--2025-04-17 10:07:50-- https://nginx.org/download/nginx-1.26.3.tar.gz
正在解析主机 nginx.org (nginx.org)... 2a05:d014:5c0:2600::6, 2a05:d014:5c0:2601: :6, 52.58.199.22, ...
正在连接 nginx.org (nginx.org)|2a05:d014:5c0:2600::6|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1260179 (1.2M) [application/octet-stream]
正在保存至: “nginx-1.26.3.tar.gz”
nginx-1.26.3.tar.gz 100%[===================>] 1.20M 696KB/s 用时 1.8s
2025-04-17 10:07:53 (696 KB/s) - 已保存 “nginx-1.26.3.tar.gz” [1260179/1260179])
2.编译安装nginx
(1)解压nginx源码包
# 解压nginx源码包
[root@localhost ~]# tar xvf nginx-1.26.3.tar.gz
(2)预编译
[root@localhost ~]# cd nginx-1.26.3/
[root@localhost nginx-1.26.3]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@localhost nginx-1.26.3]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module checking for OS
+ Linux 4.19.90-89.11.v2401.ky10.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 7.3.0 (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
......
......
Configuration summary
+ using system PCRE2 library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
(3)编译安装
[root@localhost nginx-1.26.3]# make && make install
make -f objs/Makefile
make[1]: 进入目录“/root/nginx-1.26.3”
.......
.......
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs'
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs'
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html'
|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs'
|| mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/root/nginx-1.26.3”
二、使用systemd管理nginx服务
1.编写service文件
vim /lib/systemd/system/nginx.service
# 如下是service文件的内容
[Unit]
Description=nginx service
After=network.target remote-fs.target nss-lookup.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 stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
注:
# 写好service文件后需要systemctl daemon-reload重新加载配置
[root@localhost nginx-1.26.3]# systemctl daemon-reload
2.使用systemctl命令管理nginx服务
# 开机自启
systemctl enable nginx.service
# 取消开机自启动
systemctl disable nginx.service
# 启动
systemctl start nginx.service
# 关闭
systemctl stop nginx.service
# 重启服务
systemctl restart nginx.service
# 查看状态
systemctl status nginx.service
三、验证nginx是否安装成功
开启nginx服务,浏览器进行访问
[root@localhost nginx-1.26.3]# systemctl daemon-reload
[root@localhost nginx-1.26.3]# systemctl start nginx.service
[root@localhost nginx-1.26.3]# systemctl status nginx.service
● nginx.service - nginx service
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2025-04-17 11:44:21 CST; 8s ago
Process: 251577 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 251578 (nginx)
Tasks: 2
Memory: 1.8M
CGroup: /system.slice/nginx.service
├─251578 nginx: master process /usr/local/nginx/sbin/nginx
└─251579 nginx: worker process
4月 17 11:44:21 localhost systemd[1]: Starting nginx service...
4月 17 11:44:21 localhost systemd[1]: Started nginx service.