一、基本介绍
Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一,Apache的主程序名为httpd。
二、apache安装
1.关闭防火墙和selinux
[root@localhost ~]# systemctl stop firewalld && setenforce 0
2、yum安装,开启,查看apache端口
[root@localhost ~]# yum install -y httpd
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# netstat -lntp | grep 80 #apache的端口为80,net-tools
index.html :默认访问网站的主页名称
默认发布网站的目录:/var/www/html
3、编写测试页面,并访问
[root@qfedu.com ~]# echo dir1 > /var/www/html/index.html
apache 安装完成
三、访问控制
将http.conf文档复制并去掉httpd.conf的#
去掉httpd.conf里面的空行
[root@localhost ~]# cd /etc/httpd/conf/
[root@localhost conf]# sed -i.back '/#.*/' httpd.conf
[root@localhost conf]# sed -i '/^$/d' httpd.conf
1、允许所有人访问
vim /etc/httpd/conf/httpd.conf 编辑配置文件
<Directory "/var/www">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
systemctl restart httpd 重启httpd
2、只拒绝一部分客户端访问:
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www">
Options Indexes FollowSymLinks
AllowOverride None
<RequireAll>
Require not ip 10.36.192.24 10.36.192.25 #将不允许访问的客户端IP写在这里,以空格隔开
Require all granted
</RequireAll>
</Directory>
systemctl restart httpd #重启httpd
3、只允许部分ip访问
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www">
Options Indexes FollowSymLinks
AllowOverride None
<RequireAll>
Require ip 10.36.192.16
</RequireAll>
systemctl restart httpd
4、拒绝所有ip访问
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www">
Options Indexes FollowSymLinks
AllowOverride None
<RequireALL>
Require all deined
</RequireAll>
systemctl restart httpd
四、修改默认网页发布目录
1.创建网站发布目录
[root@localhost ~]# mkdir /web
2.修改配置文件
[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/web" # 修改网站根目录为/web
<Directory "/web"> # 把这个也对应的修改为/web
3.创建测试页面
[root@localhost ~]# vim index.html
这是一个测试页面
4.重启httpd
[root@localhost ~]# systemctl restart httpd
五、虚拟主机
虚拟主机:将多个网站放在同一台服务器上
1 、基于端口
(1)修改配置信息
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd"
Listen 90
Listen 100 #添加端口
(2)修改次配置信息
[root@localhost dir2]# vim /etc/httpd/conf.d/test.conf
<VirtualHost *:90>
DocumentRoot /web/dir1
<Directory "/web/dir1">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:100>
DocumentRoot /web/dir2
<Directory "/web/dir2">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
(3)创建目录,发布项目
[root@localhost ~]# mkdir /web/dir1
[root@localhost ~]# mkdir /web/dir2
[root@localhost ~]# touch /web/dir1/index.html
[root@localhost ~]# touch /web/dir2/index.html
将前端代码发布到index.html中
(4)重启httpd并查看
2.基于域名
(1)修改次配置文件
[root@localhost ~]# vim /etc/httpd/conf.d/test.conf
<VirtualHost *:80>
DocumentRoot /web/dir1
ServerName www.zhangxiao.com
<Directory "/web/dir1">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /web/dir2
ServerName www.zx.com
<Directory "/web/dir2">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
(2)重启httpd
[root@localhost~]# systemctl restart httpd
(3)配置域名解析
C:\Windows\System32\drivers\etc
10.36.192.66 www.zhangxiao.com
10.36.192.66 www.zx.com
(4)重启并查看
3.基于ip
(1)添加一个新的网卡ip
[root@localhost ~]# ip a a 10.36.192.188 dev ens33
(2)修改配置次文件
[root@localhost ~]# vim /etc/httpd/conf.d/test.conf
<VirtualHost 10.36.192.66:80>
DocumentRoot /web/dir1
<Directory "/web/dir1">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 10.36.192.188:80>
DocumentRoot /web/dir2
<Directory "/web/dir2">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
(3)重启httpd
[root@localhost ~]# systemctl restart httpd