3.3.1 Apache的作用
Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一
在web被访问时通常使用http://的方式
http:// ##超文本传输协议
http://超文本传输协议提供软件Apache,nginx,stgw,jfe,Tengine
3.3.2 Apache的安装和基本信息
dnf install httpd -y
启用:
firewall-cmd --list-all ##查看火墙信息
firewall-cmd --permanent --add-service=http ##在火墙中永久开启http访问
firewall-cmd --permanent --add-service=https ##在火墙中永久开启https访问
firewall-cmd --reload ##刷新火墙使设定生效
systemctl enable --now httpd ##开启服务并服务位开机启动设定
服务名称:httpd
配置文件:
/etc/httpd/conf/httpd.conf ##主配置文件
/etc/httpd/conf.d/*.conf ##子配置文件默认发布目录:
/var/www/html ##此目录下优先访问index.html 如有其它文件需加上文件名,配置文件122行默认发布文件:
index.html ##可在配置文件169-171行调整默认配置文件默认端口:
80 ##http
443 ##https
默认用户用户组:
apache日志:
/etc/httpd/logs
3.3.3 Apache的基本配置
1、Apache端口修改
vim /etc/httpd/conf/httpd.conf ##配置文件
Listen 8080
firewall-cmd --permanent --add-port=8080/tcp ##在火墙中允许http访问8080
firewall-cmd --reload
systemctl restart httpd
##http://172.25.254.122:8080
2、默认发布文件
vim /etc/httpd/conf/httpd.conf
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
systemctl restart httpd
3、默认发布目录
mkdir /westos_apache
semanage fcontext -a -t httpd_sys_content_t '/westos_apache(/.*)?' ##修改目录安全上下文
restorecon -RvvF /westos_apache/ ##刷新
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/westos_apache"
<Directory "/westos_apache">
Require all granted
</Directory>
systemctl restart httpd
3.3.4 Apache的访问控制
mkdir /var/www/html/westos
vim /var/www/html/westos/index.html
1、基于ip认证访问
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/westos"> ##允许除23主机外所有人可访问
Order Allow,Deny
Allow from all
Deny from 172.25.254.23
</Directory><Directory "/var/www/html/westos"> #不允许除23主机外所有人可访问
Order Deny,Allow
Deny from all
Allow from 172.25.254.23
</Directory>
systemctl restart httpd
2、基于用户认证访问
htpasswd -cm /etc/httpd/.htpasswd admin ##生成认证文件 cm创建设定,在添加用户时不要加-c参数否则会覆盖源文件内容<Directory "/var/www/html/westos">
AuthUserFile /etc/httpd/.htpasswd ##指定认证文件
AuthName "Please input username and passwd" ##认证提示语
AuthType basic ##认证类型
Require user admin ##允许通过的认证用户
#Require valid users ##允许所有用户通过认证</Directory>
systemctl restart httpd
3.3.5 Apache的虚拟主机
此设置可使用户访问多个网址,减少服务器访问时间和内存
mkdir -p /var/www/vhost/westos.org/{news,music,map}
echo news.westos.org > /var/www/vhost/westos.org/news/index.html
echo music.westos.org > /var/www/vhost/westos.org/music/index.html
echo map.westos.org > /var/www/vhost/westos.org/map/index.html
vim /etc/httpd/conf.d/vhosts.conf
<virtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog logs/default.log combined</virtualHost>
<virtualHost *:80>
ServerName music.westos.org ##网址名称
DocumentRoot /var/www/vhost/westos.org/music ##网址访问地址
CustomLog logs/music.log combined ##产生日志</virtualHost>
<virtualHost *:80>
ServerName news.westos.org
DocumentRoot /var/www/vhost/westos.org/news
CustomLog logs/news.log combined</virtualHost>
<virtualHost *:80>
ServerName map.westos.org
DocumentRoot /var/www/vhost/westos.org/map
CustomLog logs/map.log combined
systemctl restart httpd测试主机:
3.3.6 Apache的语言支持
1、php语言
vim /var/www/html/index.php
<?php
phpinfo();
?>dnf install php -y
systemctl restart httpd
firefox http://172.25.254.122/index.php
2、cgi语言
mkdir /var/www/html/cgidir
vim /var/www/html/cgidir/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
chmod +x index.cgi ##给文件和目录加入w权限
ls -Zd /var/www/cgi-bin
semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgidir(/.*)?'
restorecon -RvvF /var/www/html/cgidir/
vim /etc/httpd/conf.d/vhost.conf
<Directory "/var/www/html/cgidir">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>firefox http://172.25.254.222/cgidir/index.cgi
3、wsgi语言
vim /var/www/html/wsgi/index.wsgi
def application(env, westos):
westos('200 ok',[('Content-Type', 'text/html')])
return [b'hello westos hahahahah!']
chmod +x index.wsgi
dnf install python3-mod_wsgi.x86_64 -y
systemctl restart httpdvim /etc/httpd/conf.d/vhost
<VirtualHost *:80>
ServerName wsgi.westos.org
WSGIScriptAlias / /var/www/html/wsgi/index.wsgi
</VirtualHost>
systemctl restart httpd
测试:用a主机测试需在a主机写域名 wsgi.westos.org
3.3.7 Apache的加密访问
dnf install mod_ssl -y ##安装加密插件
mkdir /etc/httpd/tls
req --newkey rsa:2048 -nodes -sha256 -keyout /etc/httpd/tls/westos.org.key -x509 -days 365 -out /etc/httpd/tls/westos.org.crt ##生成钥匙证书***rsa:2048 加密位数
***-sha256 加密方式
***x509 证书格式
***-days 365 证书有效时间
***-req 请求
***-in 加载签证名称
***-signkey /etc/pki/tls/private/www.westos.com.key
mkdir /var/www/vhost/westos.org/login ##
vim /var/www/vhost/westos.org/login/index.html
login.westos.org
vim /etc/httpd//conf.d/vhosts.conf
<virtualHost *:80>
ServerName login.westos.org ##搜索域名
RewriteEngine On ##默认访问80端口时王爷重写功能打开
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 ##自动跳转***^(/.*)$ ##客户地址栏中输入的地址
***%{HTTP_HOST} ##客户主机
***$1 ##RewriteRule后面跟的第一串字符的值
***[redirect]=301 ##永久重写,(302临时重写)
</virtualHost><virtualHost *:443>
ServerName login.westos.org ##搜索域名
DocumentRoot /var/www/vhost/westos.org/login ##存放路径
CustomLog logs/login.log combined ##日志
SSLEngine on ##开启ssl服务
SSLCertificateFile /etc/httpd/tls/westos.org.crt ##证书
SSLCertificateKeyFile /etc/httpd/tls/westos.org.key ##钥匙</virtualHost>
systemctl restart httpd
3.3.8 Squid+Apache
squid 正向代理
实验准备:
a主机可上网,b主机不可上网
设置a主机122
dnf install squid -y
vim /etc/squid/squid.conf
59行 加上http_access allow all
65行 去#cache
firewall-cmd --permanent --add-service=squid
firewall-cmd --reload
systemctl restart squid
配置网卡文件设置网址和DNS=114.114.114.114 网关为可上网网关172.25.254.250设置主机b
配置网卡文件设置网址 并设置prefaereces
network settings 选择Manual 设置HTTP 172.25.254.222(可上网a地址)和网络端口3128, 测试主机a浏览器
squid 反向代理
设置主机a122为资源机
安装httpd并设置火墙
echo sabdb > /var/www/html/idex.html ##添加主页资源
设置主机b222为代理机
设置火墙可通过httpd
vim /etc/squid/squid.conf
http_port 80 vhost vport
cache_peer 172.25.254.122 parent 80 0 proxy-only
重启服务
测试 主机浏览器