入职必会-开发环境搭建43-Linux软件安装-安装Nginx

本文涉及的产品
云防火墙,500元 1000GB
简介: 本文介绍在Linux中下载和安装Nginx

安装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

http://192.168.100.133

看到这个页面说明Nginx安装配置完成。

相关文章
|
3天前
|
安全 关系型数据库 MySQL
Linux下安装mysql8.0(以tar.xz包安装--编译安装)
通过上述步骤,您完成了从下载、编译、安装到配置MySQL 8.0的全过程。此过程虽然较为复杂,但提供了对MySQL安装环境的完全控制,有助于满足特定的部署需求。在实际操作中,根据具体的系统环境,可能还需调整部分步骤或解决未预见的依赖问题。始终参考官方文档和社区资源,保持安装过程与最新版本的兼容性。
151 67
|
1天前
|
负载均衡 应用服务中间件 Linux
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
这篇博客文章详细介绍了Nginx的下载、安装、配置以及使用,包括正向代理、反向代理、负载均衡、动静分离等高级功能,并通过具体实例讲解了如何进行配置。
25 4
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
|
4天前
|
存储 Oracle 关系型数据库
|
1天前
|
Ubuntu Linux
Linux中软件安装问题
【10月更文挑战第4天】
4 1
|
6天前
|
应用服务中间件 Linux nginx
Linux下操作Nginx相关命令
Linux下操作Nginx相关命令
|
6天前
|
Linux 虚拟化
Vmware 傻瓜式安装(不可不知道的Linux基础知识和技术 01)
本文介绍了VMware虚拟机的下载与安装步骤。首先,通过提供的网盘链接下载VMware安装包。接着,详细描述了安装流程,包括接受协议、选择安装路径(建议避免系统C盘)、取消更新选项等。最后,输入许可证密钥完成安装,并展示了打开虚拟机后的主界面。整个过程简单易懂,适合新手操作。
70 1
|
6天前
|
负载均衡 算法 应用服务中间件
Nginx安装及配置详解
Nginx安装及配置详解
|
6天前
|
应用服务中间件 程序员 开发工具
mac下安装nginx
mac下安装nginx
|
5天前
|
应用服务中间件 Linux Shell
Linux 配置 Nginx 服务的详细步骤,绝对干货
Linux 配置 Nginx 服务的详细步骤,绝对干货
21 0
|
6天前
|
Unix Linux Go
Linux 使用Yum安装Go和配置环境
Linux 使用Yum安装Go和配置环境