Apache
1.Apache的作用
在web被访问时通常使用http://的方式
http:// ##超文本传输协议
http:// 超文本传输协议提供软件:
Apache
nginx
stgw
jfe
Tengine
2.Apache的安装及启用
dnf install httpd -y 安装哈帕齐
firewall-cmd --permanent --add-service=http 在火墙中永久开启http
firewall-cmd --permanent --add-service=https 在火墙中永久开启https
firewall-cmd --reload 重启
systemctl enable --now httpd 打开httpd服务
vim /var/www/html/index.html 编辑文件,然后可以在firefox中浏览自己编辑的内容
3.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
4.Apache的基本配置
1.Apache端口修改在文件中改端口-默认为80:
vim /etc/httpd/conf/httpd.conf
Listen 8080
这里我们改为8080
firewall-cmd --permanent --add-port=8080/tcp 防火墙允许8080端口访问
firewall-cmd --reload 重新加载
systemctl restart httpd 重启服务读取
用火狐访问时:要指明端口
http://172.25.254.140:8080
2.默认发布文件
默认发布文件为/var/www/html/index.html
要改变默认发布文件则现在这个路径下建立另外一个文件xxx.html 这里建立为westos.html
vim /etc/httpd/conf/httpd.conf
DirectoryIndex westos.html index.html 在/var/www/html 下建立一新的发布文件westos.html 在httpd.conf中调整优先级
systemctl restart httpd
3.默认发布目录
同理,要改变默认发布目录先建立一个目录
mkdir /westos_apache
需要改变目录的安全上下文:
ls -Zd /var/www/html/
semanage fcontext -a -t httpd_sys_content_t '/westos_apache(/.*)?'
restorecon -RvvF /westos_apache/
如图:
vim /westos_apache/westos.html 编辑发布文件内容
vim /etc/httpd/conf/httpd.conf
在httpd配置文件当中更改默认发布目录指向:
DocumentRoot "/westos_apache"
<Directory "/westos_apache">
Require all granted
</Directory>
systemctl restart httpd 重启服务
在火狐浏览器访问,查看效果
firefox http://172.25.254.140
6.Apache的访问控制
#实验素材#
mkdir /var/www/html/westos
vim /var/www/html/westos/index.html
firefox http://172.25.254.140/westos
1.基于客户端ip的访问控制
(1)ip白名单
ip白名单,只让指定ip访问
<Directory “/var/www/html/westos”>
Order Deny,Allow
Deny from All
Allow from 172.25.254.40
< /Directory>
如图:
(2)ip黑名单
ip黑名单,只拒绝指定ip的访问
<Directory “/var/www/html/westos”>
Order Allow,Deny
Allow from All
Deny from 172.25.254.40
< /Directory>
如图:
2.基于用户认证
用户认证功可以加强访问的安全度
在/var/www/html/ 路径下 编辑发布文件westos/index.html
vim /etc/httpd/conf/httpd.conf 改为默认访问家目录
<Directory "/var/www/html/westos">
AuthUserfile /etc/httpd/.htpasswd ##指定认证文件
AuthName "Please input your name and password" ##认证提示语
AuthType basic ##认证类型
Require user admin ##允许通过的认证用户 2选1
# Require valid-user ##允许所有用户通过认证 2选1
</Directory>
htpasswd -cm /etc/httpd/htpasswdfile user1 ##生成认证文件
输入用户名和密码
注意:
当/etc/httpd/htpasswdfile存在那么在添加用户时不要加-c参数否则会覆盖源文件内容
htpasswd -cm /etc/httpd/htpasswdfile user1
htpasswd -m /etc/httpd/htpasswdfile admin
7.Apache的虚拟主机
在虚拟机中
mkdir -p /var/www/westos.com/{news,music}
echo "news.westos.com" >/var/www/westos.com/news/index.html
echo "music.westos.com" > /var/www/westos.com/music/index.html
|---->域名<-------|
vim /etc/httpd/conf.d/vhost.conf ##httpd子文件中添加每一个连接的信息
<VirtualHost _default_:80>
DocumentRoot "/var/www/html"
CustomLog logs/default.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName news.westos.com
DocumentRoot "/var/www/westos.com/news"
CustomLog logs/news.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName music.westos.com
DocumentRoot "/var/www/westos.com/music"
CustomLog logs/music.log combined
</VirtualHost>
在你要打开火狐的主机中进行解析
真机主机中 vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.140 www.westos.org music.westos.org news.westos.org
在浏览器所在主机中
vim /etc/hosts
172.25.254.140 www.westos.com music.westos.ocm news.westos.com
测试:
firefox http://www.westos.com
firefox http://wenku.westos.com
firefox http://news.westos.com
8.Apache的语言支持
1.php
实验前先在指定路径建立号目录文件
mkdir /var/www/html/php
vim /var/www/html/php/index.php
内容如下:
<?php
phpinfo();
?>
dnf install php -y 下载
systemctl restart httpd 重启
访问firefox: http://172.25.254.140/php/index.php
2.cgi
mkdir /var/www/html/cgi
vim /var/www/html/cgi/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
给这个脚本加执行的权限:
chmod +x /var/www/html/cgi/index.cgi
安全上下文:
使cgi可以执行
semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi/index.cgi(/.*)?'
restorecon -RvvF /var/www/html/cgi/index.cgi
vim /etc/httpd/conf.d/vhosts.conf
<Directory "/var/www/html/cgidir">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
主机上解析cgi文件 vim /etc/hosts
测试:
firefox http://172.25.254.140/cgi/index.cgi
3.wsgi
编写wsgi的测试文件
vim /var/www/html/wsgi/index.wsgi
def application(env, westos):
westos('200 ok',[('Content-Type', 'text/html')])
return [b'hello westos ahhahahahah!']
dnf install python3-mod_wsgi 下载
systemctl restart httpd 重启
vim /etc/httpd/conf.d/vhost 编写子配置文件
<VirtualHost *:80>
ServerName wsgi.westos.org
WSGIScriptAlias / /var/www/html/wsgi/index.wsgi
</VirtualHost>
如图: