Apache的作用
在web被访问时通常使用http://的方式
http:// ##超文本传输协议
http:// 超文本传输协议提供软件:
Apache
nginx
stgw
jfe
Tengine
Apache的安装
dnf install httpd.x86_64 -y
Apache的启用
systemctl enable --now httpd //开启服务并设定服务位开机启动
firewall-cmd --list-all //查看火墙信息
firewall-cmd --permanent --add-service=http //在火墙中永久开启http访问
firewall-cmd --permanent --add-service=https //在火墙中永久开启https访问
firewall-cmd --reload //刷新火墙使设定生效
Apache的基本信息
服务名称: httpd
配置文件:/etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.conf
默认发布目录: /var/www/html
默认发布文件:index.html
默认端口:80 #http 443 #https
用户:apache
日志:/etc/httpd/logs
Apache的基本配置
1.Apache端口的修改
vim /etc/httpd/conf/httpd.conf //编辑配置文件
Listen 6666 //更改端口为6666
firewall-cmd --permanent --add-port=6666/tcp //打开端口
firewall-cmd --reload //重新加载火墙
semanage port -l | grep http
semanage port -a -t http_port_t -p tcp 6666
systemctl restart httpd
2.默认发布文件
在apache中默认的发布文件在/var/www/html/
中,且指定文件名称为index.html
如果进入网址后发现进入测试页,表示该文件不存在。
我们需要编辑该文件。
使用以下方法更改默认文件:
vim /etc/httpd/conf/httpd.conf
DirectoryIndex westos.html index.html
systemctl restart httpd
此时,我们编辑一个westos.html文件。
重新载入网页,得到以下内容:
ps:注意要重新启动服务哦!
3.默认发布目录
通过上一个实验我们得知,apache的默认发布目录为/var/www/html/。
我们可以通过以下方法更改默认发布目录。
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/westos/html"
<Directory "![/westos/](https://img-blog.csdnimg.cn/20200307235042880.png)html">
Require all granted
</Directory>
semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?'
restorecon -RvvF /westos/
systemctl restart httpd
firefox http://192.168.0.11
首先,我们新建一个目录,并且编辑文件westos.html。如图:
然后,编辑apache的配置文件,修改文件中的指定默认目录地址:
Apache的访问控制
#实验素材#
mkdir /westos/html/haha
vim /westos/html/haha/index.html
<h1>hahadir's page</h1>
firefox http://10.211.55.6/haha
1.基于客户端ip的访问控制
#ip白名单#
<Directory "/westos/html/haha">
Order Deny,Allow
Allow from 192.168.0.102
Deny from All
</Directory>
#ip黑名单#
<Directory "/westos/html/haha">
Order Allow,Deny
Allow from All
Deny from 192.168.0.102
</Directory>
2.基于用户认证
vim /etc/httpd/conf/httpd.conf
<Directory "/westos/html/haha">
AuthUserfile /etc/httpd/htpasswdfile
AuthName "Please input your name and password"
AuthType basic
Require user admin
Require valid-user
1
</Directory>
htpasswd -cm /etc/httpd/htpasswdfile admin