NGINX系列(二) NGINX快速部署 1 2 3 4 5 6 7 作者:李晓辉 联系方式: 1. 微信:Lxh_Chat 2. 邮箱:939958092@qq.com
环境介绍: 操作系统 IP地址 NGINX版本 Rocky9.4 192.168.8.3 1.26.2
由于后续我将介绍如何通过编译安装nginx以支持IP地址以及地理位置识别功能,所以这里不再重复介绍编译安装过程,仅介绍包包管理器
准备软件包仓库 这里的centos仓库,适用于rhel类型的所有分支,我们这里向系统添加了一个名为nginx
的仓库
1 2 3 4 5 6 7 cat > /etc/yum.repos.d/nginx.repo <<EOF [nginx] name =nginx repobaseurl =http://nginx.org/packages/centos/9 /x86_64 /gpgcheck =0 enabled =1 EOF
尝试生成仓库索引,如果可以成功,就说明仓库可用
1 2 3 4 5 6 7 8 9 [root@lxhhost ~]# dnf makecache nginx repo 8.7 kB/s | 2.9 kB 00:00 Rocky Linux 9 - BaseOS 4.8 kB/s | 4.1 kB 00:00 Rocky Linux 9 - BaseOS 1.0 MB/s | 2.3 MB 00:02 Rocky Linux 9 - AppStream 1.8 kB/s | 4.5 kB 00:02 Rocky Linux 9 - AppStream 8.4 MB/s | 8.5 MB 00:01 Rocky Linux 9 - Extras 3.7 kB/s | 2.9 kB 00:00 Rocky Linux 9 - Extras 15 kB/s | 16 kB 00:01 Metadata cache created.
安装NGINX软件 从输出看,我们已经从nginx仓库中,安装了1.26.2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [root@lxhhost ~]# dnf install nginx -y ... ============================================================================================================================================================================================================== Package Architecture Version Repository Size ============================================================================================================================================================================================================== Installing: nginx x86_64 2:1.26.2-2.el9.ngx nginx 996 k Transaction Summary ============================================================================================================================================================================================================== Install 1 Package Total download size: 996 k Installed size: 3.3 M Downloading Packages: nginx-1.26.2-2.el9.ngx.x86_64.rpm 428 kB/s | 996 kB 00:02 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Installed: nginx-2:1.26.2-2.el9.ngx.x86_64 ... Complete! [root@lxhhost ~]#
准备测试页面 NGINX已经安装好了,我们来试试是否能够提供网页,nginx的服务默认需要我们将网页放到/usr/share/nginx/html
目录中,首页需要以index.html
或者index.htm
命名
我这里准备了一个内容为Hello nginx, I'm lixiaohui
的网页作为其首页
1 2 3 [root@lxhhost ~]# echo "Hello nginx, I'm lixiaohui" > /usr/share/nginx/html/index.html [root@lxhhost ~]# cat /usr/share/nginx/html/index.html Hello nginx, I'm lixiaohui
启用并启动服务 这里用enable --now
的语法,将nginx的服务执行enable和start操作,以便于马上启动nginx服务,并通知服务器,将nginx服务在开机时自动启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [root@lxhhost ~]# systemctl enable nginx --now Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service. [root@lxhhost ~]# systemctl status nginx ● nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled) Active: active (running) since Sat 2025-01-18 15:45:27 CST; 6s ago Docs: http://nginx.org/en/docs/ Process: 1411 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS) Main PID: 1412 (nginx) Tasks: 5 (limit : 24584) Memory: 5.2M CPU: 9ms CGroup: /system.slice/nginx.service ├─1412 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf" ├─1413 "nginx: worker process" ├─1414 "nginx: worker process" ├─1415 "nginx: worker process" └─1416 "nginx: worker process" Jan 18 15:45:27 lxhhost systemd[1]: Starting nginx - high performance web server... Jan 18 15:45:27 lxhhost systemd[1]: Started nginx - high performance web server.
开通防火墙 1 2 3 4 [root@lxhhost ~]# firewall-cmd --add-service=http --permanent success [root@lxhhost ~]# firewall-cmd --reload success
访问测试网页 1 2 [root@lxhhost ~]# curl http://192.168.8.3 Hello nginx, I'm lixiaohui
优雅重载NGINX 如果后续修改了配置文件,可用下面的方法优雅重载nginx,比如下面告诉nginx来重新加载配置文件,而不中断服务
给nginx发送reload信号,可以不中断nginx进程的情况下重载配置文件
版权声明: 本博客中的内容未经允许不得转载和引用,转载和引用需获得作者同意, 作者微信: Lxh_Chat