1.apache
企业中常用的web服务。用来提供http://(超文本传输协议)
2.apache的安装部署
9 yum install httpd -y ##apache软件
10 yum install httpd-manual ##apache的手册
11 systemctl start httpd
12 systemctl enable httpd
13 firewall-cmd --list-all ##列出火墙信息
14 firewall-cmd --permanent --add-service=http ##永久允许http
15 firewall-cmd --reload ##火墙重新加载策略
17 /var/www/html ##apache默认发布目录
18 /var/www/html/index ##apache默认发布文件
19 vim /var/www/html/index.html
20 <h1> hello world </h1>
21 :wq
22 # 测试 http://172.25.61.150
23 # http://172.25.61.150/manual
3.apache的基础信息
#主配置目录: /etc/httpd/conf
#主配置文件: /etc/httpd/conf/httpd.conf
#子配置目录: /etc/httpd/conf.d/
#子配置文件: /etc/httpd/conf.d/*.conf
#默认发布目录: /var/www/html
#默认发布文件: index.html
#默认端口: 80
修改默认端口:
vim /etc/httpd/conf/httpd.conf
#Listen 12.34.56.78:80
Listen 8080 ##修改默认端口为8080
修改默认发布文件:
默认发布文件就是访问apache时没有指定文件名称时默认访问的文件
这个文件可以指定多个,有访问顺序
vim /etc/httpd/conf/httpd.conf
164 DirectoryIndex index.html test.html ##当index.html不存在时访问test.html
修改默认发布目录
DocumentRoot "/westos/html"
<Directory "/westos/html">
Require all granted
<./Directory>
semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)'
restorecon -RvvF /westos/
4.apache的虚拟主机
mkdir /var/www/virtual/westos.com/music -p
mkdir /var/www/virtual/westos.com/news -p
vim /var/www/virtual/westos.com/news/index.html
vim /var/www/virtual/westos.com/music/index.html
vim adefault.conf
vim news.conf
vim music.conf
systemctl restart httpd
测试:
vim /etc/hosts
172.25.61.150 www.westos.com news.westos.com music.westos.com login.westos.com
在浏览器上输入网址即可
5.apache内部访问控制
1.针对主机的访问控制
<Directory "/var/www/html/test">
Order deny,allow
Allow from 172.25.61.150
Deny form all
</Directory>
2.用户方式访问控制
vim /var/www/html/admin/index.html
<h1>admin pages</h1>
htpasswd -cm /etc/httpd/htuser admin
htpasswd -m /etc/httpd/htuser admin1
cd /etc/httpd/conf.d
vim adefault.conf
systemctl restart httpd
<VirtualHost _default_:80>
DocumentRoot "/var/www/html"
</VirtualHost>
<Directory "/var/www/html">
Require all granted
Order Allow,Deny
Allow from all
</Directory>
<Directory "/var/www/html/admin">
AuthUserfile "/etc/httpd/htuser"
AuthName "please input username and password"
AuthType Basic
Require user admin
Require valid-user
</Directory>
~
http://172.25.254.134/admin/
我在这里访问的是172.25.61.150,我们可以看到加锁的网页,必须输入刚才设定的用户和密码才能看到网页的内容
6.Apache支持的语言
1.html
2.php
vim /var/www/html/index.php
yum install php -y
systemctl restart httpd
3.cgi
mkdir -p /var/www/html/cgi
semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?
restcd /var/www/html/cgi/
cd /var/www/html/cgi/
vim index.cgi
chmod +x index.cgi
./index.cgi
cd /etc/httpd/conf.d
vim adefault.conf
systemctl restart httpd
4.wsgi
cp /mnt/westos.wsgi /var/www/html/cgi/
yum install mod_wsgi.x86_64
vim westos.wsgi
#!/usr/bin/env python
import time
def application (environ, start_response):
response_body = 'UNIX EPOCH time is now: %s\n' % time.time()
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', '1'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
chmod +x westos.wsgi
./westos.wsgi
cd /etc/httpd/conf.d
vim adefault.conf
5 </directory>
16 <virtualhost _default_:80>
17 documentroot "/var/www/html"
18 WSGIScriptAlias /WSGI /var/www/html/cgi/westos.wsgi
19 </virtualhost>
测试:
systemctl restart httpd.service
7.https
HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer 或 Hypertext Transfer Protocol Secure,超文本传输安全协议),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。 它是一个URI scheme(抽象标识符体系),句法类同http:体系。用于安全的HTTP数据传输。https:URL表明它使用了HTTP,但HTTPS存在不同于HTTP的默认端口及一个加密/身份验证层(在HTTP与TCP之间)。这个系统的最初研发由网景公司(Netscape)进行,并内置于其浏览器Netscape Navigator中,提供了身份验证与加密通讯方法。现在它被广泛用于万维网上安全敏感的通讯,例如交易支付方面。
185 yum install mod_ssl -y
186 yum install crypto-utils -y
188 genkey www.westos.com
执行之后按照以下步骤选择相关选项
这里我们需要等待一会儿
这里需要管理员敲击键盘随即获取字符进行加密
完成之后可以看到最终生成的密钥
189 vim /etc/httpd/conf.d/ssl.conf
190 101 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
191 109 SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
测试:在浏览器上输入https:ip
我们还可以查看到证书的相关信息
8.网页重写
我们在登陆百度的时候在浏览器上输入www.baidu.com后回车,会发现前面会自动加上https,这就是网页重写
mkdir /var/www/virtual/westos.com/login/
cd /var/www/virtual/westos.com/login/
vim index.html
1 <h1> login pages </h1>
cd /etc/httpd/conf.d
mv login.conf z_login.conf
vim z_login.conf
<VirtualHost *:443>
ServerName login.westos.com
DocumentRoot "/var/www/virtual/westos.com/login"
CustomLog "logs/login.logs" combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
</VirtualHost>
<Directory "/var/www/virtual/login.westos.com/html">
Require all granted
</Directory>
测试:在浏览器上输入login.westos.com,会发现会自动加上https
^(/.)$ ##客户在浏览器地址栏中输入的所有字符
https:// ##强制客户加密访问
%{HTTP_HOST} ##客户请求主机
$1 ##" $1"表示^(/.) $的值