一、Apache的虚拟主机
1.虚拟主机
虚拟主机(Virtual Host),又称虚拟服务器、主机空间或是网页空间,是一种网络技术,可以让多个主机名称,在一个单一的服务器上运作,而且可以分开支持每个单一的主机名称。
虚拟主机可以运行多个网站或服务。虚拟并非指不存在,而是指空间是由实体的服务器延伸而来,其硬件系统可以是基于服务器群,或者单个服务器。
其技术是互联网服务器采用的节省服务器硬件成本的技术,虚拟主机技术主要应用于HTTP、FTP、EMAIL等多项服务,将一台服务器的某项或者全部服务内容逻辑划分为多个服务段位,对外表现为多个服务器,从而充分利用服务器硬件资源。
2.实验环境的搭建
[root@yxy ~]# mkdir -p /var/www/westos.com/{news,bbs,tieba}/html [root@yxy ~]# echo news.westos.com > /var/www/westos.com/news/html/index.html [root@yxy ~]# echo bbs.westos.com > /var/www/westos.com/bbs/html/index.html [root@yxy ~]# echo tieba.westos.com > /var/www/westos.com/tieba/html/index.html
vim /etc/httpd/Vhost.conf ##修改发布目录的配置文件 <VirtualHost _default_:80> DocumentRoot "/var/www/html" CustomLog logs/default.log combined </VirtualHost> <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/html CustomLog logs/news.log combined </VirtualHost> <VirtualHost *:80 > ServerName bbs.westos.com DocumentRoot /var/www/westos.com/bbs/html CustomLog logs/bbs.log combined </VirtualHost> <VirtualHost *:80 > ServerName tieba.westos.com DocumentRoot /var/www/westos.com/tieba/html CustomLog logs/tieba.log combined </VirtualHost> <Directory "/var/www/westos.com"> Require all granted </Directory>
3.测试
vim /etc/hosts ##测试页面做好本地解析 172.25.254.10 www.westos.com news.westos.com bbs.westos.com tieba.westos.com firefox http://www.westos.com firefox http://news.westos.com firefox http://bbs.westos.com firefox http://tieba.westos.com
二、Apache的语言支持
1.PHP语言的支持
vim /var/www/html/index.php <?php phpinfo(); ?> dnf install php -y systemctl restart httpd firefox http://172.25.254.10/index.php
2.cgi语言的支持
mkdir /var/www/html/cgi 在默认发布目录下建立cgidir目录 vim /var/www/html/cgi/index.cgi 编辑发布文件内容为脚本 #!/usr/bin/perl print "Content-type: text/html\n\n"; print `date`;
vim /etc/httpd/conf.d/vhost.conf 编辑主配置文件 <Directory "/var/www/html/cgi"> 添加支持cgi脚本 Options +ExecCGI AddHandler cgi-script .cgi </Directory> chmod +x /var/www/html/cgi/index.cgi 给脚本index.cgi添加执行权限 semanage fcontext -l |grep cgi 查询cgi安全上下文 semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?' 修改安全上下文 restorecon -RvvF /var/www/html/cgi/ 刷新 firefox http://172.25.254.10/cgi/index.cgi
三、Apache的加密访问
1.安装加密插件
dnf install mod_ssl -y
2.生成证件
openssl genrsa -out /etc/pki/tls/private/www.westos.com.key 2048 #生成私钥 openssl req -new -key /etc/pki/tls/private/www.westos.com.key \ -out /etc/pki/tls/certs/ www.westos.com.csr #生成证书签名文件 openssl x509 -req -days 365 -in \ /etc/pki/tls/certs/www.westos.com.csr \ -signkey /etc/pki/tls/private/www.westos.com.key \ -out /etc/pki/tls/certs/ www.westos.com.crt #生成证书 x509 证书格式 -req 请求 -in 加载签证名称 -signkey /etc/pki/tls/private/www.westos.com.key vim /etc/httpd/conf.d/ssl.conf SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
3.编辑主配置文件且重新加载httpd
vim /etc/httpd/conf.d/vhost.conf <VirtualHost *:80> ServerName login.westos.com RewriteEngine on RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 </VirtualHost> <VirtualHost *:443> ServerName login.westos.com DocumentRoot "/www/westos.com/login" CustomLog logs/login.log combined SSLEngine on SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key </VirtualHost> systemctl restart httpd ^(/.*)$ ##客户地址栏中输入的地址 %{HTTP_HOST} ##客户主机 $1 ##RewriteRule后面跟的第一串字符的值
四、Squid+Apache
1.什么是Squid服务
代理缓存服务器:接收到用户请求后,自动下载指定资源并存储到本地服务器,以后用户请求相同资源时,直接把存储在本地服务器的资源直接传给用户。
2.squid 正向代理
<1>正向代理:
标准代理模式
客户端请求访问一个web页面,访问代理服务器,若代理服务器有缓存数据,则直接从代理服务器下载数据,若代理服务器没有缓存,则会请求原站点数据,从原站点拿到数据后给客户端,这就提高了访问速度。但是,要想实现这种方式,必须在每一个内部主机的浏览器上明确指名代理服务器的IP地址和端口号。
透明代理模式
透明代理缓冲服务器和正向代理服务器的功能完全相同。但是,代理操作对客户端的浏览器是透明的(即不需指明代理服务器的IP和端口)。
<2>实验环境:
- 单网卡主机设定ip不能上网
- 双网卡主机设定ip1可以连接单网卡主机,设定ip2可以上网
<3>实验效果:
- 让单网卡主机不能上网但浏览器可以访问互联网页
<4>操作步骤:
在双网卡主机中
dnf install squid -y
vim /etc/squid/squid.conf
59 http_access allow all #允许任何人
65 cache_dir ufs /var/spool/squid 100 16 256 #缓存文件地址
systemctl restart squid
firewall-cmd --permanent --add-port=3128/tcp
firewall-cmd --reload
在单网卡主机中
选择 NetWork Proxy
172.25.254.20 3128
<5>测试
在单网卡主机中
ping www.baidu.com 不通
在浏览器中访问www.baidu.com可以
3.squid反向代理
<1>反向代理:
反向代理是和前两种代理完全不同的一种代理服务。它指定了一个或多个代理服务器,当用户访问原始站点时,实际上由代理服务器来相应用户的请求。这减小了服务器端的压力,同时可以做到负载均衡。
<2>实验环境:
- 172.25.254.20 ##Apache服务器
- 172.25.254.10 ##squid,没有数据负责缓存
<3>修改配置文件
vim /etc/squid/squid.conf
http_port 80 vhost vport ##vhost 支持虚拟域名 vport 支持虚拟端口
#当172.25.254.10的80端口被访问会从172.25.254.20的80端口缓存数据
cache_peer 172.25.254.20 parent 80 0 proxy-only
systemctl restart squid
<4>测试
firefox http://172.25.254.10
访问看到的是172.25.254.20上的数据