跨境派

跨境派

跨境派,专注跨境行业新闻资讯、跨境电商知识分享!

当前位置:首页 > 综合服务 > 物流仓储 > CentOS系列:【Linux】CentOS7操作系统安装nginx实战(多种方法,超详细)

CentOS系列:【Linux】CentOS7操作系统安装nginx实战(多种方法,超详细)

时间:2024-04-10 16:15:34 来源:网络cs 作者:纳雷武 栏目:物流仓储 阅读:

标签: 方法  详细  实战  系列  操作  系统  安装 

CentOS7操作系统安装nginx实战(多种方法,超详细)

一. 实验环境二. 使用yum安装nginx2.1 添加yum源2.1.1 使用官网提供的源地址(方法一)1. 找到官网的源2. 使用rpm -ivh 进行安装3. 安装完成之后查看源: 2.1.2 使用epel的方式进行安装(方法二)1. 先安装epel2. 安装完成后,查看安装的epel包即可 2.2 开始安装nginx上面的两个方法不管选择哪个,都可以使用yum进行安装: 2.3 启动并进行测试2.4 其他的一些用法:1. 停止服务:2. 重新加载nginx3. 打开防火墙的80端口: 三. 编译方式安装nginx3.1 下载所需要的包3.2 创建目录并解压nginx包3.3 安装编译需要的包3.4 安装并测试3.5 简化默认的启动方式 四. 拓展内容4.1 编译安装完nginx的配置文件位置4.2 配置访问状态统计




一. 实验环境

本次的实验环境见下表:

操作系统服务器IPhostname
centos7.6192.168.1.41mufengrow41

如何查看相应的参数:

查看操作系统:

[root@mufenggrow ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) 

查看ip

[root@mufenggrow41 ~]# ifconfig |grep inet |awk 'NR==1{print $2}'192.168.1.56

二. 使用yum安装nginx

2.1 添加yum源

nginx不在的默认的yum源中, 可以使用epel或者官网提供的yum源来安装。

以下两种方法,选择任意一种即可,也就是2.1.1和2.1.2两个小节的内容,任选其一:

2.1.1 使用官网提供的源地址(方法一)
1. 找到官网的源

官网提供的源地址:http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

在这里插入图片描述

2. 使用rpm -ivh 进行安装
[root@mufeng ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm获取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm警告:/var/tmp/rpm-tmp.DRyoO4: 头V4 RSA/SHA1 Signature, 密钥 ID 7bd9bf62: NOKEY准备中...                          ################################# [100%]正在升级/安装...   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
3. 安装完成之后查看源:

在这里插入图片描述

2.1.2 使用epel的方式进行安装(方法二)
1. 先安装epel
[root@mufeng ~]# sudo yum install yum-utils
2. 安装完成后,查看安装的epel包即可
yum install epel-release

2.2 开始安装nginx

上面的两个方法不管选择哪个,都可以使用yum进行安装:
[root@mufeng ~]# yum install nginx

2.3 启动并进行测试

# 查看nginx版本[root@mufeng ~]# nginx -vnginx version: nginx/1.22.1
# 设置开机自启动[root@mufeng ~]# systemctl enable nginxCreated symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
# 启动nginx[root@mufeng ~]# systemctl start nginx[root@mufeng ~]# 

测试 :
直接访问IP即可:
在这里插入图片描述

2.4 其他的一些用法:

1. 停止服务:
 systemctl restart nginx
2. 重新加载nginx
systemctl reload nginx
3. 打开防火墙的80端口:
[root@mufeng ~]# firewall-cmd --zone=public --permanent --add-service=http success[root@mufeng ~]# firewall-cmd --reloadsuccess[root@mufeng ~]# 

命令的作用是将 http 服务添加到 public 区域的永久规则中,即允许通过防火墙访问 http 服务。

其中,
–zone=public 指定了作用的区域为 public
–permanent 表示该规则将被永久保存
–add-service=http 指定添加的服务为 http

三. 编译方式安装nginx

3.1 下载所需要的包

nginx 包下载地址: http://nginx.org/en/download.html

[root@mufeng ~]# wget http://nginx.org/download/nginx-1.22.1.tar.gz

在这里插入图片描述

3.2 创建目录并解压nginx包

root@mufeng ~]# lsanaconda-ks.cfg  initial-setup-ks.cfg  nginx-1.22.1.tar.gz  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@mufeng ~]# mkdir tools
[root@mufeng ~]# mv nginx-1.22.1.tar.gz tools/
[root@mufeng ~]# cd tools/
[root@mufeng tools]# tar xf nginx-1.22.1.tar.gz 
[root@mufeng tools]# lsnginx-1.22.1  nginx-1.22.1.tar.gz
[root@mufeng tools]# cd nginx-1.22.1/
[root@mufeng nginx-1.22.1]# lsauto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src[root@mufeng nginx-1.22.1]# 

3.3 安装编译需要的包

一般编译都需要gcc,如果没有会报错

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre
root@mufeng nginx-1.22.1]# make && make install

这里指定了 nginx 的安装路径为 /usr/local/nginx,同时启用了 SSL 和状态监控模块。

在编译 Nginx 时,可以使用 ./configure --help 命令来查看可以使用的编译选项

3.4 安装并测试

使用make和make install进行安装

[root@mufeng nginx-1.22.1]# make && make install

开始测试:浏览器输入IP:

在这里插入图片描述

3.5 简化默认的启动方式

默认的操作方式,比如查看配置文件是否正确:

[root@mufeng nginx-1.22.1]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

我们使用软链接或者alias的形式来简化,这里我们使用软链接:

[root@mufeng nginx-1.22.1]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
[root@mufeng nginx-1.22.1]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@mufeng nginx-1.22.1]# 

启动方式:

[root@mufeng nginx-1.22.1]# nginx

重新加载方式:

[root@mufeng nginx-1.22.1]# nginx -s reload

关闭的话只能通过杀死进程了:

在这里插入图片描述

四. 拓展内容

4.1 编译安装完nginx的配置文件位置

conf: /usr/local/nginx , 存放 nginx 的配置文件,nginx.conf 是 nginx 服务最核心最主要的配置文件。

html:/usr/local/nginx,保存 ningx 服务器的 web 文件。也可以更改为其他目录保存 web 文件。

logs: /var/logs/nginx,保存 ningx 服务器的访问日志、错误日志等日志。

sbin: /usr/local/nginx,保存 nginx 二进制启动脚本。可以接受不同参数以实现不同的功能。

在这里插入图片描述
如果启动或者重启的过程中报错,可以查看logs中的日志。

4.2 配置访问状态统计

我们在编译的时候添加了 –with-http_stub_status_module这个模块,你可以使用nginx -V查看是否包含这个模块。

在这里插入图片描述

然后修改配置文件:

[root@mufeng nginx]# vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

代码如下:

 location / {            root   html;            index  index.html index.htm;        }location /status {stub_status on;access_log off;}

查看配置文件是否正确

[root@mufeng nginx]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重启:

nginx -s reload

测试:

在这里插入图片描述

可以看到: 当前的活动连接数为3个
554 表示: 已处理的连接数为5, 成功的TCP握手次数为5, 已处理的请求数为1







我是沐风晓月

【Linux】CentOS7操作系统安装nginx实战(多种方法,超详细)

本文链接:https://www.kjpai.cn/news/2024-04-10/156612.html,文章来源:网络cs,作者:纳雷武,版权归作者所有,如需转载请注明来源和作者,否则将追究法律责任!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。

上一篇:Zabbix的详细安装部署

下一篇:返回列表

文章评论