本文介绍如何在 银河麒麟操作系统(kylin v10)上进行 nginx 的离线安装与配置,包括依赖包下载、https 配置、多端口站点部署等完整步骤。
1. 准备环境
由于是离线安装,需要提前下载 nginx 及其依赖的 rpm 包。
银河麒麟官方镜像地址示例:https://update.cs2c.com.cn/ns/v10/v10sp3/os/adv/lic/updates/x86_64/packages/
其中
v10、v10sp3、x86_64根据你的系统版本和 cpu 架构调整。
需要下载以下 rpm 包(注意版本匹配):
gperftools-libs-.......rpmnghttp2-.......rpmnginx-.......rpmnginx-all-modules-.......rpmnginx-filesystem-.......rpmopenssl-.......rpmopenssl-libs-.......rpmpcre2-.......rpmzlib-.......rpm
2. 安装 nginx 及依赖
将下载好的 rpm 包放到 /tmp/nginx_rpm/ 目录:
cd /tmp/nginx_rpm/ rpm -ivh *.rpm --force --nodeps
3. 生成 ssl 证书
创建证书目录:
mkdir -p /etc/nginx/ssl cd /etc/nginx/ssl
生成自签名证书(有效期 365 天):
openssl req -newkey rsa:2048 -nodes -keyout test.key \
-x509 -days 365 -out test.crt \
-subj "/c=cn/st=beijing/l=beijing/o=test/ou=it/cn=localhost"
4. 创建测试站点目录
mkdir -p /var/www/test-web mkdir -p /var/www/test-app echo "web index" > /var/www/test-web/index.html echo "app index" > /var/www/test-app/index.html
5. 配置 nginx
编辑 /etc/nginx/nginx.conf(替换原内容):
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# gzip 压缩
gzip on;
gzip_min_length 1k;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/javascript application/xml;
gzip_vary on;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
6. 新建多站点配置文件
新建 /etc/nginx/conf.d/project.conf:
# web http → https
server {
listen 80;
server_name localhost;
return 301 https://$server_name$request_uri;
}
# web https
server {
listen 443 ssl http2;
server_name localhost;
client_max_body_size 1024m;
ssl_certificate /etc/nginx/ssl/test.crt;
ssl_certificate_key /etc/nginx/ssl/test.key;
ssl_protocols tlsv1.2 tlsv1.3;
ssl_ciphers high:!anull:!md5;
location / {
root /var/www/test-web;
index index.html;
try_files $uri $uri/ /index.html;
}
location /test/ {
proxy_pass http://192.168.2.67:9652;
proxy_http_version 1.1;
proxy_set_header connection "";
proxy_set_header host $host;
proxy_set_header x-real-ip $remote_addr;
}
}
# app http → https
server {
listen 3000;
server_name localhost;
return 301 https://$server_name:3001$request_uri;
}
# app https
server {
listen 3001 ssl http2;
server_name localhost;
client_max_body_size 1024m;
ssl_certificate /etc/nginx/ssl/test.crt;
ssl_certificate_key /etc/nginx/ssl/test.key;
ssl_protocols tlsv1.2 tlsv1.3;
ssl_ciphers high:!anull:!md5;
location / {
root /var/www/test-app;
index index.html;
try_files $uri $uri/ /index.html;
}
location /test/ {
proxy_pass http://192.168.2.67:9652;
proxy_http_version 1.1;
proxy_set_header connection "";
proxy_set_header host $host;
proxy_set_header x-real-ip $remote_addr;
}
}
测试配置文件:nginx -t
7. 注册服务并启动 nginx
创建服务文件:vim /etc/systemd/system/nginx.service
写入以下内容:
[unit] description=the nginx http and reverse proxy server after=network.target remote-fs.target nss-lookup.target [service] type=forking pidfile=/run/nginx.pid # nginx will fail to start if /run/nginx.pid already exists but has the wrong # selinux context. this might happen when running `nginx -t` from the cmdline. execstartpre=/usr/bin/rm -f /run/nginx.pid execstartpre=/usr/sbin/nginx -t execstart=/usr/sbin/nginx execreload=/bin/kill -s hup $mainpid killsignal=sigquit timeoutstopsec=5 killmode=mixed privatetmp=true [install] wantedby=multi-user.target
设置开机启动并启动 nginx:
systemctl enable nginx systemctl start nginx systemctl status nginx
8. 开放防火墙端口
sudo firewall-cmd --add-port=80/tcp --permanent sudo firewall-cmd --add-port=443/tcp --permanent sudo firewall-cmd --add-port=3000/tcp --permanent sudo firewall-cmd --add-port=3001/tcp --permanent sudo firewall-cmd --reload
9. 访问测试
web 站点:http://localhost 会自动跳转到 https://localhosthttps://localhost 显示 web index
app 站点:http://localhost:3000 会跳转到 https://localhost:3001https://localhost:3001 显示 app index
10. 总结
本文介绍了在银河麒麟 v10 系统中 离线安装 nginx 的全过程,包括:
- 依赖包下载与安装
- 自签名 ssl 证书生成
- 多站点 https 配置
- 反向代理示例
- 防火墙端口开放
通过该流程,即使在无外网环境下,也能在 kylin os 上快速部署 nginx 服务器。
到此这篇关于银河麒麟(kylin)离线安装nginx并部署多服务完整步骤的文章就介绍到这了,更多相关银河麒麟离线安装nginx部署多服务内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论