一.Apache服务详解
1、Apache是什么
Apache是使用最广泛的Web服务器软件。Apache是由Apache Software Foundation开发和维护的,它是一个免费提供的开源软件,它占全球所有网络服务器的67%。
2、Apache的特点
1.支持最新的HTTP/1.1通信协议 2.拥有简单而强有力的基于文件的配置过程 3.支持通用网关接口 4.支持基于IP和基于域名的虚拟主机 5.支持多种方式的HTTP认证 6.集成Perl处理模块 7.集成代理服务器模块 8.支持实时监视服务器状态和定制服务器日志 9.支持服务器端包含指令(SSI) 10.支持安全Socket层(SSL) 11.提供用户会话过程的跟踪 12.支持FastCGI 13.通过第三方模块可以支持JavaServlets
3、Apache的安装与环境配置
命令 功能 yum search apache 查找apache相关安装包 yum install httpd -y 根据查找的安装包安装apache yum install httpd-manual 安装apache的手册 systemctl start httpd 开启httpd服务 systemctl enable htttpd 设定开机时自动激活httpd服务 firewall-cmd --permanent --add-service=http 添加http服务权限至防火墙 firewall-cmd --reload 加载使修改立即生效 firewall-cmd --list-all 查看http是否加入防火墙权限
4、Apache的基本信息
命令 功能 /var/www/html 默认发布目录 /var/www/html/index.html 默认发布文件 /etc/httpd/conf 主配置目录 /etc/httpd/conf/httpd.conf 主配置文件 /etc/httpd/conf.d 子配置目录 /etc/httpd/conf.d/*.conf 子配置文件 80 默认的端口 httpd_sys_content_t 默认的安全上下文 apache 程序开启默认用户 /etc/httpd/logs/* 默认apache日志
测试:
命令 功能 vim /var/www/html/index.html 建立index.html(默认发布文件) test 输入测试内容text,完成后退出保存 http://xxx.xxx.xxx.xxx(服务端IP) 在客户端测试,查看测试内容是否符合 vim /var/www/html/index.html 修改index.html test 改变测试内容的字体样式 http://xxx.xxx.xxx.xxx(服务端IP) 在客户端查看样式是否改变
5、修改Apache的默认配置信息
(1)默认发布目录信息修改
指定发布目录位置(发布目录唯一)
命令 功能 mkdir -p /test/html 递归建立新发布目录 vim /test/html/index.html 在新发布目录中建立默认发布文件 testneirong 输入测试内容,完成后退出保存 vim /etc/httpd/conf/httpd.conf 修改配置文件 DocumentRoot “/test/html” 指定发布目录为/test/html
授权新发布目录(授权后的目录才可被查看)
命令 功能 <Directory “/test” 在配置文件中给新发布目录添加授权 Require all granted 添加完成,退出保存 systemctl restart httpd 重启服务
测试
命令 功能 http://xxx.xxx.xxx.xxx(服务端IP) 在客户端查看测试内容是否符合
还原
测试完成后将改动还原,使实验环境纯净
(2)默认发布文件信息修改
指定发布文件位置,并含有优先级关系(发布文件可不唯一)
命令 功能 vim /var/www/html/test1.html 建立发布文件test1.html test1.html 测试内容为test1.html vim /var/www/html/test2.html 建立发布文件test2.html test2.html 测试内容为test2.html vim /etc/httpd/conf/httpd.conf 修改主配置文件 DirectoryIndex test1.html test2.html index.html 指定发布文件,文件的优先级为从前到后 systemctl restart httpd 重启服务
测试
命令 功能 http://xxx.xxx.xxx.xxx(服务端IP) 在客户端测试,此时看到的内容应为test1.html(发布目录为test1.html) rm -rf /var/www/html/test1.html 删除test1.html http://xxx.xxx.xxx.xxx(服务端IP) 在客户端测试,此时看到的内容应为test2.html(发布目录为test2.html) rm -rf /var/www/html/test2.html 删除test2.html http://xxx.xxx.xxx.xxx(服务端IP) 在客户端测试,此时看到的内容应为test(发布目录为index.html)
还原
测试完成后将改动还原,使实验环境纯净
(3)默认端口修改
修改端口为8080
命令 功能 vim /test/html/index.conf 修改主配置文件 Listen 8080 将端口修改为8080 systemctl restart httpd 重启服务
测试
命令 功能 http://xxx.xxx.xxx.xxx(服务端IP):8080 在客户端以8080端口查看能否查看到内容 netstat -antulp grep httpd
还原
测试完成后将改动还原,使实验环境纯净
6、Apache的虚拟主机设定
(1)在服务端的配置
命令 功能 mkdir -p /var/webvirt/test.org/movies/html 递归建立目录 vim /var/webvirt/test.org/movies/html/index.html 建立测试文件 movies.test.org page 编辑测试内容,完成后退出保存 vim /etc/httpd/conf.d/vhost.conf 修改子配置文件(虚拟机)
<VirtualHost _default_:80> |将默认的通过80端口查找的域名指引到此
DocumentRoot /var/www/html |指引查看此目录
CustomLog logs/default.log combined |设定此虚拟机的日志文件,combined意为混合记录
</VirtualHost> |设定结束
<VirtualHost *:80> |将所有符合域名的通过80端口的查找指引到此
ServerName movies.test.org |域名为movies.test.org
DocumentRoot /var/webvirt/test.org/movies/html |指引至的发布目录绝对路径
CustomLog logs/movies.log combined |设置日志文件,混合记录
</VirtualHost> |设定结束
<Directory /var/webvirt/test.org> |给/var/webvirt/test.org添加授权,授权后该目录及目录中的所有内容均有授权
Require all granted
</Directory> |授权结束
命令 功能 systemctl restart httpd 重启httpd服务
(2)在客户端的配置
命令 功能 vim /etc/hosts 修改该文件 xxx.xxx.xxx.xxx(服务端IP) www.test.org(添加域名解析) news.test.org movies.test.org 添加域名解析,将IP与域名对应
(3)在客户端测试
命令 功能 http://www.test.org 测试是否可以显示正确内容 http://movies.test.org 测试是否可以显示正确内容(movies.test.org page)
7、客户端访问权限设定
(1)客户端访问权限
服务端配置
命令 功能 htpasswd -cm /etc/httpd/.httppassfile user 新建user用户登录权限加入/etc/httpd/.httppassfile中(加入列表) htpasswd -m /etc/httpd/.httppassfile user1 将user1用户登录权限加入列表(若有c,则会新建,覆盖之前文件中的内容) htpasswd -m /etc/httpd/.httppassfile user2 将user2用户登录权限加入列表 cat /etc/httpd/.httppassfile 查看该文件,是否加入 vim /etc/httpd/conf.d/vhost.conf 修改该文件
<Directory /var/www/html> |对/var/www/html做出设定
Order allow,deny |设定先读allow,后读deny(后读内容若与先读内容冲突,则后读的内容会覆盖先读的内容,如先读为1、2、1,后读为1、2、3,则3会将1覆盖,最终为1、2、3)
Allow from all |设定allow为所有人均可
Deny from 172.25.254.54 |设定deny为该IP不可,deny后读,所以该IP的设定覆盖,最终设定将为所有人除该IP外均可登录
</Directory> |设定结束
<Directory /var/www/html> |对/var/www/html做出设定
Order deny,allow |设定先读deny,后读allow
Allow from 172.25.254.54 |设定该IP可登录,因为allow后读,覆盖该IP的设定,所以最终将设定为所有人除该IP外均不可登录
Deny from all |设定所有人不可
</Directory> |设定结束
命令 功能 systemctl restart httpd 重启服务
客户端测试
命令 功能 http://www.test.org 查看设定完成前后,是否可以登录的变化
(2)基于用户的访问控制
服务端配置
命令 功能 vim /etc/httpd/conf.d/vhost.conf 修改该文件
<Directory /var/www/html> |对该目录做出设定
AuthUserFile /etc/httpd/.httppassfile|设定用户认证文件为/etc/httpd/.httppassfile
AuthType basic |认证类型为基本认证(用户名、密码认证)
AuthName "请输入您的用户名和密码" |认证时显示的信息为“请输入您的用户名和密码”
Require user user |允许通过认证的用户为“user”用户
</Directory> |设定结束
命令 功能 systemctl restart httpd 重启服务
客户端测试
命令 功能 http://www.test.org 查看使用user用户和密码是否可以登录查看(应可以)、查看使用其他用户和密码是否可以登录查看(应不可)
(3)基于IP的访问控制
服务端配置
命令 功能 vim /etc/httpd/conf.d/vhost.conf 修改该文件
<Directory /var/www/html> |对该目录做出设定
AuthUserFile /etc/httpd/.httppassfile|设定用户认证文件为/etc/httpd/.httppassfile
AuthType basic |认证类型为基本认证(用户名、密码认证)
AuthName "请输入您的用户名和密码" |认证时显示的信息为“请输入您的用户名和密码”
Require valid-user |允许通过认证的用户为valid-user(列表中有的用户,如刚刚加入的user、user1、user2用户)
</Directory> |设定结束
命令 功能 systemctl restart httpd 重启服务
客户端测试
命令 功能 http://www.test.org 查看使用user、user1、user2(在列表中的用户)是否可以登录查看(应均可)
8、如何使PHP语言与perl语言文件与python语言在Apache中可查看
(1)PHP语言
服务端配置
命令 功能 yum install php -y 安装php vim /var/www/html/index.php 建立php语言文件
<?php
phpinfo();
?> |意为显示php的测试页
命令 功能 systemctl restart httpd 重启httpd服务
客户端测试
命令 功能 http://xxx.xxx.xxx.xxx/index.php 查看是否能看到php的测试页,若能看到,则说明php语言编写的文件可查看成功
(2)perl语言
服务器配置
命令 功能 yum install httpd-manual -y 安装httpd-manual(httpd手册) mkdir /var/www/html/cgi 建立测试目录(cgi为通用网关接口) vim /var/www/html/cgi/index.cgi 建立该文件,文件用perl语音编写
#!/usr/bin/perl |说明运行环境
print "Content-type: text/html\n\n"; |显示内容设定
print `date`; |显示内容为date命令的结果
命令 功能 vim /etc/httpd/conf.d/vhost.conf 修改该文件
<Directory /var/www/html> |对该目录做出设定
Options +ExecCGI
AddHandler cgi-script .cgi |该两行的意义是通过cgi将perl语言编写的文件使用apache可以查看到结果
</Directory> |设定结束
命令 功能 systemctl restart httpd 重启服务
客户端测试
命令 功能 http://xxx.xxx.xxx.xxx/manual 查看手册,寻找使用cgi查看perl文件的命令,用来编写上方所用到的配置文件 http://xxx.xxx.xxx.xxx/cgi/index.cgi 查看是否可以查看到perl文件的结果(即date命令的结果)
(3)python语言
服务器配置
命令 功能 yum install mod_wsgi -y 安装mod_wsgi vim /var/www/html/index.wsgi 建立python语言编写的文件
文件内容如下,一个用python语言编写的测试页
#!/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]
命令 功能 vim /etc/httpd/conf.d/vhost.conf 修改该文件
<VirtualHost _default_:80> |设定默认路径通过80端口访问时的引导
DocumentRoot /var/www/html |设定引导的发布目录
CustomLog logs/default.log combined |设定日志文件,混合记录
WSGIScriptAlias /WSGI /var/www/html/index.wsgi |设定用WSGI接口,在/WSGI(可自主起名的虚拟目录)下使用apache查看到python文件的结果
</VirtualHost> |设定结束
命令 功能 systemctl restart httpd 重启服务
客户端测试
命令 功能 http://xxx.xxx.xxx.xxx/WSGI 查看是否能看到python文件的结果
9、https加密安全访问
命令 功能 yum install mod_ssl -y 安装mod_ssl yum install crypto-utils -y 安装crypto-utils genkey www.test.com 生成访问www.test.com时的密钥 vim /etc/httpd/conf.d/ssl.conf 修改该文件 101 SSLCertificateFile 生成的公钥绝对路径 修改公钥路径为刚刚生成的公钥 109 SSLCertificateKeyFile 生成的私钥绝对路径 修改私钥路径为刚刚生成的私钥 /etc/pki/tls/certs/www.test.com.crt 公钥 /etc/pki/tls/private/www.test.com.key 私钥
客户端测试
命令 功能 https://xxx.xxx.xxx.xxx 在客户端测试登录
10、网页重写(设定https虚拟主机)
服务端配置
命令 功能 vim /etc/httpd/conf.d/vhost.conf 编辑Apache虚拟机配置文件
<VirtualHost *:443> |设定https虚拟主机(https开启的端口为443)
ServerName abc.test.org |设定域名为abc.test.org
DocumentRoot /var/webvirt/test.org/abc/html |设定发布目录
CustomLog logs/abc.log combined |设定日志目录
SSLEngine on |开启SSL
SSLCertificateFile /etc/pki/tls/certs/www.test.com.crt |公钥地址
SSLCertificateKeyFile /etc/pki/tls/private/www.test.com.key |私钥地址
</VirtualHost>
<VirtualHost *:80>
ServerName abc.test.org |设定域名为abc.test.org
RewriteEngine on |开启重写
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301] |设定重写的规则,^(/.*)$为客户在浏览器地址栏中输入的所有字符,%{HTTP_HOST}为客户请求主机(获取访问的域名),[redirect=301](永久重写,302时为临时重写)
</VirtualHost>
命令 功能 systemctl restart httpd 重启服务 mkdir /var/webvirt/test.org/abc/html -p 建立指定的发布目录 vim /var/webvirt/test.org/abc/html/index.html 建立指定的发布文件 https page 编辑测试内容,完成后退出保存
客户端测试
命令 功能 abc.test.org 查看是否自动转化为“https://abc.test.org”,在下载过认证后,进入查看是否与测试的内容相符合