安装Nginx
注意:安装Nginx需要root权限
安装yum依赖程序
命令说明:
# 安装yum依赖程序 yum install -y yum-utils
操作示例:
[root@cxypa soft]# yum install -y yum-utils 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: ftp.sjtu.edu.cn * extras: ftp.sjtu.edu.cn * updates: ftp.sjtu.edu.cn 正在解决依赖关系 --> 正在检查事务 ---> 软件包 yum-utils.noarch.0.1.1.31-50.el7 将被 升级 ---> 软件包 yum-utils.noarch.0.1.1.31-54.el7_8 将被 更新 --> 解决依赖关系完成 # 省略其他 更新完毕: yum-utils.noarch 0:1.1.31-54.el7_8 完毕! [root@cxypa soft]#
手动添加Nginx的yum仓库
yum程序使用的仓库配置文件存放在/etc/yum.repo.d
目录中。
命令说明:
# 在/etc/yum.repos.d/创建nginx.repo文件 vim /etc/yum.repos.d/nginx.repo
操作示例:
# 在/etc/yum.repos.d/创建nginx.repo文件 [root@cxypa soft]# vim /etc/yum.repos.d/nginx.repo # 填入如下内容并保存退出 [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
使用yum安装Nginx
命令说明:
# 使用yum安装Nginx yum install -y nginx
操作示例:
[root@cxypa soft]# yum install -y nginx 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: ftp.sjtu.edu.cn * extras: ftp.sjtu.edu.cn * updates: ftp.sjtu.edu.cn nginx-stable | 2.9 kB 00:00:00 nginx-stable/7/x86_64/primary_db | 80 kB 00:00:06 正在解决依赖关系 --> 正在检查事务 ---> 软件包 nginx.x86_64.1.1.22.1-1.el7.ngx 将被 安装 --> 正在处理依赖关系 libpcre2-8.so.0()(64bit),它被软件包 1:nginx-1.22.1-1.el7.ngx.x86_64 需要 --> 正在检查事务 ---> 软件包 pcre2.x86_64.0.10.23-2.el7 将被 安装 --> 解决依赖关系完成 # 省略其他 已安装: nginx.x86_64 1:1.22.1-1.el7.ngx 作为依赖被安装: pcre2.x86_64 0:10.23-2.el7 完毕! [root@cxypa soft]#
启动Nginx
Nginx安装后会自动创建一个名为nginx
的服务。
命令说明:
systemctl start nginx # 启动nginx systemctl enable nginx # 开机自动启动nginx
操作示例:
[root@cxypa soft]# systemctl start nginx [root@cxypa soft]# systemctl status nginx ● nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since 六 2022-12-24 18:15:25 CST; 12s ago Docs: http://nginx.org/en/docs/ Process: 2233 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS) Main PID: 2234 (nginx) CGroup: /system.slice/nginx.service ├─2234 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf ├─2235 nginx: worker process ├─2236 nginx: worker process ├─2237 nginx: worker process └─2238 nginx: worker process 12月 24 18:15:25 cxypa systemd[1]: Starting nginx - high performance web server... 12月 24 18:15:25 cxypa systemd[1]: Started nginx - high performance web server. [root@cxypa soft]# [root@cxypa soft]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. [root@cxypa soft]#
配置防火墙允许远程访问80端口
Nginx
默认绑定80端口,由于CentOS7默认开启了防火墙,远程连接被拦截了,需要打开80的端口号,才能远程访问Nginx。
命令说明:
# 显示现有的规则 firewall-cmd --list-all # 开放的端口永久保存到防火墙 firewall-cmd --zone=public --add-port=80/tcp --permanent # 重启防火墙 systemctl restart firewalld
操作示例:
[root@cxypa soft]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: ssh dhcpv6-client ports: 3306/tcp 8080/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@cxypa soft]# firewall-cmd --zone=public --add-port=80/tcp --permanent success [root@cxypa soft]# systemctl restart firewalld [root@cxypa soft]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: ssh dhcpv6-client ports: 3306/tcp 8080/tcp 80/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@cxypa soft]#
使用宿主机远程访问Nginx
看到这个页面说明Nginx安装配置完成。