Apache(上)

1)安装

yum install httpd -y      ##安装客户端访问的浏览器
systemctl start httpd
systemctl enable httpd

查看默认端口:ss -antlpe | grep httpd ##端口为80
这里写图片描述

默认发布目录:/var/www/html
默认发布文件:index.html
cd /var/www/html
vim index.html
<h1>hello,world!</h1>         ##此文件的格式完全遵循html格式
:wq
systemctl restart httpd
firefox
http://172.25.254.108         ##IP为本机的

这里写图片描述

Apache的配置文件:/etc/httpd/conf/httpd.conf

修改默认发布文件:改变在/var/www/html中的文件顺序即可
cd  /var/www/html
vim westos
<h1>weclome to westos!</h1>
:wq
vim /etc/httpd/conf/httpd.conf
改变165行默认文件的发布顺序

这里写图片描述
这里写图片描述

修改默认发布目录:改变默认发布目录的,授予权限,改变目录的安全上下文和

mkdir /westos/html -p
semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?'
restorecon -RvvF /westos/

vim /westos/html/index.html
<h1>/westos/html/index.html's page</h1>
:wq

vim /etc/httpd/conf/httpd.conf         ##修改配置文件内容

#DocumentRoot "/var/www/html"          ##注释原本的默认发布目录
DocumentRoot "/westos/html"

<Directory "/westos/html">
          Require all granted
</Directory>
:wq

systemctl restart httpd

这里写图片描述

Apache访问权限:

<Directory "/westos/html">        ##只允许172.25.254.8访问
          Require all granted
          Order Deny,Allow        ##Order最后面的起作用
          Allow from 172.25.254.8
          Deny from all
</Directory>

<Directory "/westos/html">        ##允许除172.25.254.8之外的所又用户访问
          Require all granted
          Order Allow,Deny
          Allow from 172.25.254.8
          Deny from all
</Directory>

设置授权访问用户名单(authfile):
cd /etc/httpd/conf                ##切换位置

htpasswd -cm authfile admin0      ##首位授权用户建立
New password:
Re-type new password:

htpasswd -m authfile admin1       ##首位授权用户建立之后的其他所有用户的建立
New password:
Re-type new password:

cat authfile                     ##查看授权访问用户名单

vim /etc/httpd/conf/httpd.conf
<Directory "/westos/html">       ##用户访问Apache需要进行身份认证,只有通过了授权访问列表才可以访问
          Require all granted
          AuthUserFile /etc/httpd/conf/authfile
          AuthName "Please input your name and password:"
          AuthType basic
          Require valid-user
</Directory>

<Directory "/westos/html">       ##用户访问Apache需要进行身份认证,只有通过了授权访问列表中的admin0用户才可以访问
          Require all granted
          AuthUserFile /etc/httpd/conf/authfile
          AuthName "Please input your name and password:"
          AuthType basic
          Require user admin0
</Directory>

Apache的虚拟控制:

mkdir /var/www/westos/news.westos.com -p
mkdir /var/www/westos/gongyi.westos.com -p
##新建目录,此目录的位置没有约束,唯一的约束就是安全上下文一定要是Apache默认发布目录的安全上下文

vim /var/www/westos/news.westos.com/index.html
<h1>news.westos.com's page</h1>
:wq
vim /var/www/westos/gongyi.westos.com/index.html
<h1>gongyi.westos.com's page</h1>
:wq

cd /etc/httpd/conf.d/

vim default.conf
<Virtualhost _default_:80>
         DocumentRoot "/var/www/html"
         CustomLog "logs/default.log" combined
</virtualhost>

vim gongyi.conf
<Virtualhost *:80>
         ServerName gongyi.westos.com
         DocumentRoot "/var/www/westos/gongyi.westos.com"
         CustomLog "logs/gongyi.log" combined
</virtualhost>
<Directory "/var/www/westos/gongyi.westos.com">
         Require all granted
</Directory>

vim news.conf
<Virtualhost *:80>
         ServerName news.westos.com
         DocumentRoot "/var/www/westos/gongyi.westos.com"
         CustomLog "logs/news.log" combined
</virtualhost>
<Directory "/var/www/westos/news.westos.com">
         Require all granted
</Directory>

systemctl restart httpd

测试:
vim /etc/hosts
172.25.254.108 www.westos.com news.westos.com gongyi.westos.com

firefox输入域名测试即可:
  www.westos.com
  news.westos.com
  gongyi.westos.com

这里写图片描述
这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值