Apache和Nginx

⼀、Apache 概念
1、概述
最早的 web 服务程序,基于 http 协议提供⽹⻚浏览服务。
2、特点
模块化设置、开放源代码、跨平台应⽤、⽀持多种 web 编程语
⾔、运⾏稳定。
3、⼯作模式
(1)Prefork:使⽤进程处理请求,在该模式中⽐较消耗内存,但稳
定性⾼,如某个进程出现问题,不会影响其他请求。
(2)Worker:属于多进程模式,每个进程⽣成多个进程;在该模式下
消耗的资源⽐较⼩,适合⾼并发请求,但稳定性没有 Prefork 模式稳
定。
(3)Event:该模式与 Worker 模式较为相似,不同之处在于在该模
式下可以解决keepalive ⻓连接时占⽤线程资源导致浪费的问题。(4)keep-alive ⻓连接:TCP连接在发送后将仍然保持打开状态,
于是,浏览器可以继续通过相同的连接发送请求。保持连接节省了为
每个请求建⽴新连接所需的时间,还节约了带宽。实现⻓连接要客户
端和服务端都⽀持⻓连接。
⼆、搭建 apache 服务器
1、安装并设置防⽕墙序

[root@server2 ~]# yum -y install httpd
上次元数据过期检查:6:39:47 前,执⾏于 2023年08⽉31⽇ 星
期四 15时10分42秒。
依赖关系解决。
===================================================
===========
软件包 架构 版本 仓库
⼤⼩
===================================================
===========
安装:
 httpd x86_64 2.4.37-
51.module+el8.7.0+1059+126e9251
......省略部分安装信息......
[root@server2 ~]# systemctl start httpd #启
动apache
[root@server2 ~]# netstat -anpt | grep httpd #查
看端⼝确认apache已启⽤
tcp6 0 0 :::80 :::* 
 LISTEN 3512/httpd 
[root@server2 ~]# systemctl status
firewalld.service #查看防⽕墙是否启⽤,若启⽤则设置
apache服务可通⾏规则
● firewalld.service - firewalld - dynamic firewall
daemon
 Loaded: loaded
(/usr/lib/systemd/system/firewalld.service;>
 Active: active (running) since Thu 2023-08-31
21:49:59 CST>
......省略部分状态信息......
[root@server2 ~]# firewall-cmd --permanent --addservice=http #设置防⽕墙放⾏apache
success
[root@server2 ~]# firewall-cmd --reload #重载防
⽕墙规则
success
[root@server2 ~]# firewall-cmd --list-all #查看当
前区域下防⽕墙所有规则
public (active)
 target: default
 icmp-block-inversion: no
 interfaces: ens32 ens34 ens35
 sources:
 services: cockpit dhcpv6-client http ssh 
#apache(http)已放⾏
 ports: 873/tcp
 protocols:
 forward: no
 masquerade: no
 forward-ports:
 source-ports:
 icmp-blocks:
 rich rules:

2、apache 配置⽂件
yum 安装的 apache,配置⽂件在 /etc/httpd/conf/httpd.conf
如果是编译安装的 apache,那么配置⽂件在⾃⼰编译安装的安
装⽬录下

[root@server2 ~]# vim /etc/httpd/conf/httpd.conf
37 # Listen: Allows you to bind Apache to specific
IP addresses and/or
38 # ports, instead of the default. See also the
<VirtualHost>
39 # directive.
40 #
41 # Change this to Listen on specific IP addresses
as shown below to
42 # prevent Apache from glomming onto all bound IP
addresses.
43 #
44 #Listen 12.34.56.78:80
45 Listen 80 #默认的httpd监听端⼝,可在下⾯添加其他⾃
设端⼝
46
47 #
48 # Dynamic Shared Object (DSO) Support
......省略部分内容
92 # ServerName gives the name and port that the
server uses to identify itself.
93 # This can often be determined automatically,
but we recom mend you specify
94 # it explicitly to prevent problems during
startup.
95 #
96 # If your host doesn't have a registered DNS
name, enter i ts IP address here.
97 #
98 ServerName www.example.com:80 #指定httpd服务域
名和该域名的端⼝,可以⼿动修改其他域名
99
100 #
101 # Deny access to the entirety of your server's
filesystem. You must

3、apache 索引⽂件
在⼀般的前段服务器中http nginx tomcat,在没有指定⽂件路径的时
候,默认先访问资源⽂件夹中的index.xxx
./html/index.html
httpd 使⽤ yum 安装时,默认的站html⽂件在
/var/www/html/index.html,如果 index.html ⽂件不存在,需要
⼿动创建
在⽹站⽅⾯,index通常是指主⽬录的意思,index.html是⽬录下
默认打开的⻚⾯。
⽐如,⽹站的域名是www.abc.com,如果设置了 index.html 是
默认主⻚,那么打开 http://www.abc.com 和打开⻚⾯ http://ww
w.abc.com/index.html 就是⼀样的。他们打开的都是⽹站⾸⻚,
因为index.html是⽹站默认的主⻚。
在 index.html 写⼊如下内容,可以让主⻚显示⼀个圆形,圆⼼处
显示⽂字“这就是主⻚“
[root@server2 ~]# vim /var/www/html/index.html
<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title>ssjie's page</title>
 <style type="text/css">
 .bigMom{
 margin:0 auto;
 width:600px; 
#圆的半径宽度
 height:600px; 
#圆的半径⾼度
 line-height:600px; 
#⾏间距
 text-align:center; 
#⽂字位置:居中
 color:black; 
#⽂字颜⾊:⿊⾊
 border-radius:50%; 
#圆⻆率,50%是圆
 backgroundcolor:cyan; #圆的颜⾊:⻘⾊
 }
 </style>
 </head>
 <body>
 <div class="bigMom">
 欢迎来到主⻚!
 </div>
 </body>
</html>

4、服务器访问测试
浏览器内输⼊搭建好的 httpd 服务器 IP 地址或域名(当前服务
器地址为 192.168.33.110)

5、IP 不同,域名相同的虚拟主机
(1)修改主配置
设置 IP 相同,域名不同的虚拟主机,配置⽂件在
/etc/httpd/conf/httpd.conf
.htaccess ⽂件:可以实现,⽂件夹密码保护、⽤户⾃动重定
向、⾃定义错误⻚⾯、改变你的⽂件扩展名、封禁特定IP地址的
⽤户、只允许特定IP地址的⽤户、禁⽌⽬录列表,以及使⽤其他
⽂件作为index⽂件等⼀些功能。
[root@server2 ~]# vim /etc/httpd/conf/httpd.conf
45 Listen 80
46
47 ServerName www.yh666.com:80 #添加
如下内容,域名⾃起,IP地址为⾃⼰的主机
48
49 NameVirtualHost 192.168.33.110
50 <VirtualHost 192.168.33.110>
51 DocumentRoot /var/www/html/yang #标明
索引⽂件路径,所以还需⾃⼰在该路径创建新的索引⽂件
52 ServerName www.yh666.com #域名
⾃起
53 <directory /var/www/html/yang>
54 allowoverride None #确定
是否被允许读取.htaccess⽂件内容
55 require all granted #允许
所有⽤户访问指定的(/var/www/html)⽂档或⽬录
56 </directory>
57 </virtualhost>
58
59 <virtualhost 192.168.33.110>
60 documentroot /var/www/html/hong #同上
61 servername www.hong777.com
62 <directory /var/www/html/hong>
63 allowoverride none
64 require all granted
65 </directory>
66 </virtualhost>
:wq #保存并退出
[root@server2 html]# systemctl reload httpd.service 
#重载服务

(2)创建索引⽂件
索引⽂件:index.html,⼀般为⽹站的⾸⻚。
[root@server2 ~]# cd /var/www/html/ #移动
到索引⽂件所在位置
[root@server2 html]# ls #当前
⽬录下有个索引⽂件模板
index.html
[root@server2 html]# mkdir yang #创建
与上⾯主配置对应的索引⽬录 yang
[root@server2 html]# cp -p index.html yang/ #将索
引⽂件的模板复制到新建的yang⽬录下
[root@server2 html]# mkdir hong #创建
与上⾯主配置对应的索引⽬录 hong
[root@server2 html]# cp -p index.html hong/ #复制
索引⽂件到hong⽬录下
[root@server2 ~]# vim /var/www/html/yang/index.html 
#第⼀个域名的索引⽂件
[root@server2 ~]# vim /var/www/html/hong/index.html 
#第⼆个域名的索引⽂件
#修改⽅式如下:
19 <body>
20 <div class="bigMom">
21 这⾥是www.yh666.com! 
#仅测试情况下,修改此处即可
22 </div> 
#两个⽂件都改,避免测试⽆效
23 </body>
:wq #保存并退出

(3)修改本地 hosts
本地 hosts:将⼀些常⽤的⽹址域名与其对应的IP地址建⽴⼀个
关联“数据库”,当⽤户在浏览器中输⼊⼀个需要登录的⽹址时,
系统会⾸先⾃动从Hosts⽂件中寻找对应的IP地址,⼀旦找到,
系统会⽴即打开对应⽹⻚,如果没有找到,则系统会再将⽹址提
交DNS域名解析服务器进⾏IP地址的解析。

如果该⽂件需要权限,那么右键该⽂件,进⼊“属性”,点击“安
全”选项卡,找到⾃⼰电脑的主⽤户,点编辑,然后再根据图示
给予⽤户权限。
(4)域名访问测试
浏览器输⼊配置好的域名 www.yh666.com

6、IP 相同,端⼝不同的虚拟主机
(1)修改主配置
编辑 /etc/httpd/conf/httpd.conf 配置⽂件
[root@server2 ~]# vim /etc/httpd/conf/httpd.conf
45 Listen 80
46 Listen 88 #添加⼀个88端⼝
47
48 ServerName www.yh666.com:80
49
50 NameVirtualHost 192.168.33.110
51 <VirtualHost 192.168.33.110:80>
52 DocumentRoot /var/www/html/yang
53 ServerName www.yh666.com
54 <directory /var/www/html/yang>
55 allowoverride None
56 require all granted
57 </directory>
58 </virtualhost>
59
60 <virtualhost 192.168.33.110:88> #修改
hong777.com的虚拟主机端⼝为88,其余内容不变
61 documentroot /var/www/html/hong
62 servername www.hong777.com
63 <directory /var/www/html/hong>
64 allowoverride none
65 require all granted
66 </directory>
67 </virtualhost>
:wq #保存并退出
[root@server2 html]# systemctl reload httpd.service 
#重载服务

(2)访问测试
浏览器内分别输⼊ 192.168.33.110:80 和 192.168.33.110:88
在访问时记得关闭防⽕墙或者修改防⽕墙放⾏端⼝规则。
[root@server2 ~]# firewall-cmd --add-port=88/tcp 
#添加tcp协议的88端⼝防⽕墙放⾏服务
success
[root@server2 ~]# firewall-cmd --list-all
public (active)
 target: default
 icmp-block-inversion: no
 interfaces: ens32 ens34 ens35
 sources:
 services: cockpit dhcpv6-client http ssh
 ports: 873/tcp 88/tcp #添加成功
 protocols:
 forward: no
 masquerade: no
 forward-ports:
 source-ports:
 icmp-blocks:
 rich rules:

7、域名相同,IP 不同的虚拟主机
(1)添加⽹络⼦接⼝
[root@server2 ~]# ifconfig ens32:0
192.168.33.120/24 #添加⽹卡虚拟⼦接⼝命令格式
[root@server2 ~]# ifconfig #查询当前系统存在的⽹卡信

......省略部分⽹卡信息......
ens32:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> 
mtu 1500
 inet 192.168.33.120 netmask 255.255.255.0 
broadcast 192.168.33.255
 ether 00:0c:29:bd:1a:25 txqueuelen 1000 
(Ethernet)
......省略部分⽹卡信息......
(2)修改主配置
[root@server2 ~]# vim /etc/httpd/conf/httpd.conf
45 Listen 80
46 Listen 88
47
48 ServerName www.yh666.com:80
49
50 NameVirtualHost 192.168.33.110
51 <VirtualHost 192.168.33.110:80>
52 DocumentRoot /var/www/html/yang
53 ServerName www.yh666.com
54 <directory /var/www/html/yang>
55 allowoverride None
56 require all granted
57 </directory>
58 </virtualhost>
59
60 <virtualhost 192.168.33.120:88> #修改此处IP
地址为虚拟⼦接⼝,其余不变
61 documentroot /var/www/html/hong
62 servername www.hong777.com
63 <directory /var/www/html/hong>
64 allowoverride none
65 require all granted
66 </directory>
67 </virtualhost>
:wq #保存并退出
[root@server2 html]# systemctl reload httpd.service 
#重载服务

(3)访问测试
浏览器输⼊分别 192.168.33.110 和 192.168.33.120

⼀、Nginx 的内置变量


⼆、开启 nginx 状态监听模块
1、修改配置
进⼊ nginx 主配置⽂件(yum 和编译安装的⽂件路径不⼀
样)。
找到“server { }”,添加⼀个“location { }”,也就是虚拟主机。
[root@server2 ~]# vim
/usr/local/nginx/conf/nginx.conf
......省略部分内容......
 server {
 listen 80; #监听端⼝
 server_name localhost; #服务器名称
 charset utf-8; #字符集,utf-8为中⽂
字符集
#access_log logs/host.access.log main;
 location /status { #nginx状态的监听模块
 stub_status on;
 access_log off;
 }
 }
......省略部分内容......
:wq #保存并退出
[root@server2 ~]# systemctl restart nginx.service 
#重启nginx

2、访问测试
浏览器访问 192.168.33.110/status,查看 nginx 状态信息。

三、nginx 虚拟主机配置
⼀个“location”相当于⼀个虚拟主机,也就是⽤户访问⽹站时,点击跳转的另⼀个⻚⾯。
location 内可以添加 nginx 各种功能模块。
[root@server2 ~]# vim
/usr/local/nginx/conf/nginx.conf
......省略部分内容......
 server {
 listen 80; #监听端⼝
 server_name localhost;
 charset utf-8;
#access_log logs/host.access.log main;
 location /status {
 stub_status on;
 access_log off;
 }
 location / {
 root html; #⽹站在服务器上的⽬
录,默认为/usr/local/nginx/html
 index index.html index.htm; #跳转到
的⽹站⾸⻚
 }
 }
......省略部分内容......
:wq
[root@server2 ~]# systemctl restart nginx.service 
#重启nginx
四、nginx 反向代理配置
反向代理:⽤户直接访问反向代理服务器就可以获得⽬标服务器
(后端服务器)的资源。

1、修改配置
在配置⽂件中添加⼀⾏反向代理块指令(proxy_pass),表示当访问本机地址 192.168.33.110 的 80 端⼝时即可跳转到后端。服务器 192.168.33.11 的 80 端⼝上。
[root@server2 ~]# vim
/usr/local/nginx/conf/nginx.conf
......省略部分内容......
 server {
 listen 80;
 server_name localhost;
 charset utf-8;
#access_log logs/host.access.log main;
 location /status {
 stub_status on;
 access_log off;
 }
 location / {
 root html;
 index index.html index.htm;
 proxy_pass http://192.168.33.11:80; 
#添加⼀个反向代理模块
 }
......省略部分内容......
[root@server2 ~]# systemctl reload nginx.service 
#重载nginx配置

3、访问测试
浏览器输⼊本机地址 192.168.33.110,将会⾃动跳转到后端服务器 192.168.33.11 上。

  • 13
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值