栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

CentOS7.8搭建Apache服务详解

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

CentOS7.8搭建Apache服务详解

目前世界主流使用的web服务器软件

apache

跨平台、Windows、Linux不同的机器平台运行 nginxlis微软Lighttpd

HTTP服务器,apache高度模块化的web服务器

apache支持多虚拟主机功能

基于端口的多虚拟主机基于域名的多虚拟主机基于IP的多虚拟主机 Linux安装apache

linux安装软件的方式

去官网获取软件源代码,自由选择版本,获取代码后,进行源代码编译安装,扩展额外的功能,自定义安装路径rpm包手动安装,不好用,需要自行解决软件依赖配置yum源,使用yum自动化安装,解决依赖关系

1.使用yum源进行安装
yum install -y httpd
2.查看本机80号端口是否被占用
netstat -tunlp | grep 80
3.启动apache并设置开启自启
systemctl start httpd && systemctl enable httpd
4.检查解析apache是否启动成功
systemctl status httpd
curl 127.0.0.1
apache配置文件解析
1.检查apache安装了哪些文件,通过yum安装的软件,如何检查安装的信息呢
rpm -ql httpd | grep '.conf'

2.apache主配置目录:/etc/httpd/
主配置文件绝对路径:/etc/httpd/conf/httpd.conf

3.查看主配置文件的有益信息
[root@localhost ~]# grep -Ev "^[#]|^$" /etc/httpd/conf/httpd.conf 
ServerRoot "/etc/httpd"   httpd主配置目录定义
Listen 80           定义httpd运行的端口
Include conf.modules.dpublic_html">
     AllowOverride all   允许所有其他配置文件都能够修改其配置
     添加一些认证的配置,使得用户可以用账号,密码访问该目录
     authuserfile "/etc/httpd/passwd"  
     authname "input your account"
     authtype basic
     require user qiu
    #AllowOverride FileInfo AuthConfig Limit Indexes
    #Options MultiViews Indexes SymlinksIfOwnerMatch IncludesNoExec
    #Require method GET POST OPTIONS


3.创建网站的资料,注意权限的问题
[root@localhost ~]# su - qhj
[qhj@localhost ~]$ mkdir public_html 创建共享文件夹
[qhj@localhost ~]$ echo hello test >> public_html/index.html  创建共享网页文件
[qhj@localhost ~]$ chmod -Rf 755 public_html/  修改权限

4.创建apache的用户认证文件,注意要使用root用户执行
[root@localhost ~]# htpasswd -c /etc/httpd/passwd qiu
New password: 
Re-type new password: 
Adding password for user qiu

5.重新启动apache服务
systemctl restart httpd

6.使用浏览器访问是否配置完成
http://192.168.100.200/~qhj

注:应该把home下面的用户家目录修改成711权限(默认700),否则浏览器访问无法进行认证
[root@localhost ~]# chmod 711 /home/qhj/
[root@localhost ~]# ll /home/
total 4
drwx--x--x. 3 qhj qhj 4096 May  4 12:54 qhj
基于IP的多虚拟主机

在一台服务器上,绑定多个IP或多个网卡,每个IP地址部署一个网站,当用户请求不同的IP地址时,apache根据用户的请求信息,来响应不同的网站内容

1.给Linux服务器绑定多个IP
[root@localhost ~]# ip a | grep "inet 192"
    inet 192.168.100.100/24 brd 192.168.100.255 scope global eno16777736
#给服务器添加2个IP地址
[root@localhost ~]# ip address add 192.168.100.101/24 dev eno16777736
[root@localhost ~]# ip address add 192.168.100.102/24 dev eno16777736
#检查服务器IP地址是否添加成功
[root@localhost ~]# ip a | grep "inet 192"
    inet 192.168.100.100/24 brd 192.168.100.255 scope global eno16777736
    inet 192.168.100.101/24 scope global secondary eno16777736
    inet 192.168.100.102/24 scope global secondary eno16777736

在Linux服务器上,添加多个站点资料

192.168.100.100  /www/test01
192.168.100.101  /www/test02
192.168.100.102  /www/test03

#创建3个站点的目录
[root@localhost ~]# mkdir -p /www/{test01,test02,test03}
[root@localhost ~]# ls /www/
test01  test02  test03

#创建3个站点的网页资料
[root@localhost ~]# echo "hello is test01" >> /www/test01/index.html
[root@localhost ~]# echo "hello is test02" >> /www/test02/index.html
[root@localhost ~]# echo "hello is test03" >> /www/test03/index.html

#针对三个不同的IP地址,做一个域名的解析关系,在hosts文件中,添加如下对应解析关系(注意是在客户端下添加)
192.168.100.100  www.test01.com
192.168.100.101  www.test02.com
192.168.100.102  www.test03.com

配置apache的主配置文件,定义多个虚拟主机

#192.168.100.100的虚拟主机

documentRoot /www/test01
ServerName www.test01.com

AllowOverride None
Require all granted



#192.168.100.101的虚拟主机

documentRoot /www/test02
ServerName  www.test02.com

AllowOverride None
Require all granted



#192.168.100.102的虚拟主机

documentRoot /www/test03
ServerName www.test03.com

AllowOverride None
Require all granted


重新启动apache服务并测试是否成功

重新启动服务
systemctl restart httpd
测试是否成功
[root@localhost ~]# curl www.test01.com
hello is test01
[root@localhost ~]# curl www.test02.com
hello is test02
[root@localhost ~]# curl www.test03.com
hello is test03
基于多域名的多虚拟主机

在服务器上,配置一个IP绑定多个域名,每个域名对应一个网站;当用户请求不同的I域名时,apache根据用户的请求信息,来响应不同的网站内容

配置单IP绑定多个域名,在hosts文件中,添加对应的域名解析关系(客户端)
192.168.100.100 www.test04.com www.test05.com

创建站点目录
mkdir /www/{test04,test05}

创建站点网页文件
[root@localhost ~]# echo "hello is test04" >> /www/test04/index.html
[root@localhost ~]# echo "hello is test05" >> /www/test05/index.html

配置apache的主配置文件,定义多个虚拟主机或在其包含的目录中创建*.conf文件添加以下内容

#www.test04.com的虚拟主机

documentRoot /www/test04
ServerName www.test04.com

AllowOverride None
Require all granted



#www.test05.com的虚拟主机

documentRoot /www/test05
ServerName  www.test05.com

AllowOverride None
Require all granted


重新启动服务并测试是否成功

重启服务
systemctl restart httpd

测试
[root@localhost ~]# curl www.test04.com
hello is test04
[root@localhost ~]# curl www.test05.com
hello is test05
基于多端口的多虚拟主机
修改主配置文件
#添加监听端口
Listen 8080
Listen 8081

#8080端口的虚拟主机

documentRoot /www/test06
ServerName www.test06.com

AllowOverride None
Require all granted



#8081端口的虚拟主机

documentRoot /www/test07
ServerName  www.test07.com

AllowOverride None
Require all granted


创建站点目录
mkdir /www/{test06,test07}
创建站点网页文件
[root@localhost ~]# echo "port 8080 test" >> /www/test06/index.html
[root@localhost ~]# echo "port 8081 test" >> /www/test07/index.html

重新启动服务并测试

重启服务
systemctl restart httpd
测试
[root@localhost ~]# curl 192.168.100.100:8081
port 8081 test
[root@localhost ~]# curl 192.168.100.100:8080
port 8080 test
apache设置站点访问权限
1.直接修改apache配置文件,针对一个虚拟主机去修改访问的权限

documentRoot /www/test03
ServerName www.test03.com

#允许所有人访问所有权限
#AllowOverride None
#Require all granted
#针对虚拟主机站点,进行访问控制,可以控制单个IP地址,也可以控制整个网段的访问
Order allow,deny  先允许后拒绝
Allow from 192.168.100.0/24 (2.4版本之前)
Require ip 192.168.178.0/24 (2.4版本之后的写法)


定义访客日志格式
ErrorLog "logs/error_log"  错误日志存放位置

#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel warn  日志级别

  定义日志格式模块
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
    LogFormat "%h %l %u %t "%r" %>s %b" common

    
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %I %O" combinedio
apache状态监测功能
1.修改配置文件
vim /etc/httpd/conf/httpd.conf

2.添加如下参数
#设置状态页功能,用户访问192.168.100.100/server-status

SetHandler server-status

Require ip 192.168.100.0/24


使用apache自带的压测命令,给服务器发送大量的请求

1.使用ab命令
yum install -y httpd-tools

2.使用ab命令,给服务器并发的发送大量请求
#给服务器发送100000个请求,100个并发数
ab -c 100 -n 100000 http://192.168.100.100
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/748085.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号