Apache httpd2.2版本以及2.4版本部分实验

环境准备实验环境:
主机IP描述192.168.5.181操作系统为CentOS7,安装httpd2.4版本192.168.5.121操作系统为CentOS6,安装httpd2.2版本,安装MySQL数据库192.168.5.180测试用Linux系统,安装有curl工具192.168.5.190测试用Linux系统,安装有curl工具192.168.5.182CA证书颁发机构在两台主机上面先清空防火墙规则,关闭Selinux,然后用yum安装httpd,在CentOS6上面,默认的Base源里面是httpd2.2版本;在CentOS7上面,默认的Base源里面是httpd2.4版本。
$iptables-tfilter-F$setenforce0$yuminstallhttpd在CentOS7上面查看httpd版本:$yuminfohttpd|grep-iversionVersion:2.4.6在CentOS6上面查看httpd版本:$yuminfohttpd|grep-iversionVersion:2.2.15实验一:基于主机名称的虚拟主机CentOS6, httpd2.2环境
在/etc/httpd/conf.d/目录下面添加一个新的配置项virtualhost.conf,编辑里面的内容如下所示,添加NameVirtualHost指令,指明用192.168.5.121:80作为基于FQDN的虚拟主机,添加两个VirtualHost配置段,分别使用www1.stuX.com和www2.stuX.com作为主机名
分别给两台虚拟主机自定义日志功能:
www1.stuX.com的访问日志是/web/vhosts/www1/access_log
www1.stuX.com的错误日志是/web/vhosts/www1/error_log
www2.stuX.com的访问日志是/web/vhosts/www2/access_log
www2.stuX.com的错误日志是/web/vhosts/www2/error_log
之后重启httpd服务:
$cat/etc/httpd/conf.d/virtualhost.confNameVirtualHost192.168.5.121:80<VirtualHost192.168.5.121:80>ServerNamewww1.stuX.comDocumentRoot"/web/vhosts/www1"LogFormat"%h%u%t\"%r\"%>s\"%{Referer}i\"\"%{User-Agent}i\""custom1CustomLog/web/vhosts/www1/access_logcustom1ErrorLog/web/vhosts/www1/error_log<Directory"/web/vhosts/www1">Orderallow,denyAllowfromall</Directory></VirtualHost><VirtualHost192.168.5.121:80>ServerNamewww2.stuX.comDocumentRoot"/web/vhosts/www2"LogFormat"%h%u%t\"%r\"%>s\"%{Referer}i\"\"%{User-Agent}i\""custom2CustomLog/web/vhosts/www2/access_logcustom2ErrorLog/web/vhosts/www2/error_log<Directory"/web/vhosts/www2">Orderallow,denyAllowfromall</Directory></VirtualHost>$servicehttpdstart创建/web/vhosts/www1和/web/vhosts/www2目录,分别在目录里面添加一个简单的测试页面:
$mkdir-p/web/vhosts/www{1,2}$echo"Thisiswww1.stuX.com">/web/vhosts/www1/index.html$echo"Thisiswww2.stuX.com">/web/vhosts/www2/index.htmlCentOS7, httpd2.4环境
同样在/etc/httpd/conf.d目录下面添加一个新的配置项virtualhost.conf。与CentOS6不同的是,省略掉了NameVirtualHost指令,并且ACL权限的配置也发生了变化。使用www3.stuX.com和www4.stuX.com作为主机名。
定义日志功能:
www3.stuX.com的访问日志是/web/vhosts/www3/access_log
www3.stuX.com的错误日志是/web/vhosts/www3/error_log
www4.stuX.com的访问日志是/web/vhosts/www4/access_log
www4.stuX.com的错误日志是/web/vhosts/www4/error_log
之后重启httpd.service
<VirtualHost192.168.5.181:80>ServerNamewww3.stuX.comDocumentRoot"/web/vhosts/www3"LogFormat"%h%u%t\"%r\"%>s\"%{Referer}i\"\"%{User-Agent}i\""custom3CustomLog/web/vhosts/www3/access_logcustom3ErrorLog/web/vhosts/www3/error_log<Directory"/web/vhosts/www3">OptionsNoneAllowOverrideNone<RequireAll>RequireallgrantedRequirenotip192.168.5.190</RequireAll></Directory></VirtualHost><VirtualHost192.168.5.181:80>ServerNamewww4.stuX.comDocumentRoot"/web/vhosts/www4"LogFormat"%h%u%t\"%r\"%>s\"%{Referer}i\"\"%{User-Agent}i\""custom4CustomLog/web/vhosts/www4/access_logcustom3ErrorLog/web/vhosts/www4/error_log<Directory"/web/vhosts/www4">OptionsNoneAllowOverrideNoneRequireallgranted</Directory></VirtualHost>创建/web/vhosts/www3和/web/vhosts/www4目录,分别在目录里面添加一个简单的测试页面:
$mkdir-p/web/vhosts/www{3,4}$echo"Thisiswww3.stuX.com">/web/vhosts/www3/index.html$echo"Thisiswww4.stuX.com">/web/vhosts/www4/index.html客户端测试
在客户端配置/etc/hosts文件,用来解析主机名
root@alternative:~#cat/etc/hosts|grep-iwww192.168.5.121www1.stuX.comwww2.stuX.com192.168.5.181www3.stuX.comwww4.stuX.com通过客户端的测试,可以看到结果如下所示,完成了基于主机名的虚拟主机配置:
root@alternative:~#curlhttp://www1.stuX.comThisiswww1.stuX.comroot@alternative:~#curlhttp://www2.stuX.comThisiswww2.stuX.comroot@alternative:~#curlhttp://www3.stuX.comThisiswww3.stuX.comroot@alternative:~#curlhttp://www4.stuX.comThisiswww4.stuX.com伪装客户端和跳转地址root@alternative:~#curl-A"curltest"-e"http://www.baidu.com"http://www1.stuX.comThisiswww1.stuX.comroot@alternative:~#curl-A"curltest2"-e"http://www.sina.com"http://www2.stuX.comThisiswww2.stuX.comroot@alternative:~#curl-A"curltest3"-e"http://www.sohu.com"http://www3.stuX.comThisiswww3.stuX.comroot@alternative:~#curl-A"curltest4"-e"http://www.163.com"http://www4.stuX.comThisiswww4.stuX.com发起一些错误的请求,用来检测error_log是否生效root@alternative:~#curlhttp://www1.stuX.com/123<!DOCTYPEHTMLPUBLIC"-//IETF//DTDHTML2.0//EN"><html><head><title>404NotFound</title></head><body><h2>NotFound</h2><p>TherequestedURL/123wasnotfoundonthisserver.</p><hr><address>Apache/2.2.15(CentOS)Serveratwww1.stux.comPort80</address></body></html>root@alternative:~#curlhttp://www2.stuX.com/456<!DOCTYPEHTMLPUBLIC"-//IETF//DTDHTML2.0//EN"><html><head><title>404NotFound</title></head><body><h2>NotFound</h2><p>TherequestedURL/456wasnotfoundonthisserver.</p><hr><address>Apache/2.2.15(CentOS)Serveratwww2.stux.comPort80</address></body></html>root@alternative:~#curlhttp://www3.stuX.com/789<!DOCTYPEHTMLPUBLIC"-//IETF//DTDHTML2.0//EN"><html><head><title>404NotFound</title></head><body><h2>NotFound</h2><p>TherequestedURL/789wasnotfoundonthisserver.</p></body></html>root@alternative:~#curlhttp://www4.stuX.com/000<!DOCTYPEHTMLPUBLIC"-//IETF//DTDHTML2.0//EN"><html><head><title>404NotFound</title></head><body><h2>NotFound</h2><p>TherequestedURL/000wasnotfoundonthisserver.</p></body></html>查看一下访问日志以及错误日志:
$tail-f/web/vhosts/www{1,2}/{access,error}_log==>/web/vhosts/www1/access_log<==192.168.5.180-[02/Jun/2017:14:46:24+0800]"GET/HTTP/1.1"200"-""curl/7.22.0(x86_64-pc-linux-gnu)libcurl/7.22.0OpenSSL/1.0.1zlib/1.2.3.4libidn/1.23librtmp/2.3"192.168.5.180-[02/Jun/2017:14:46:40+0800]"GET/123HTTP/1.1"404"-""curl/7.22.0(x86_64-pc-linux-gnu)libcurl/7.22.0OpenSSL/1.0.1zlib/1.2.3.4libidn/1.23librtmp/2.3"192.168.5.180-[02/Jun/2017:14:49:01+0800]"GET/HTTP/1.1"200"http://www.baidu.com""curltest"==>/web/vhosts/www1/error_log<==[FriJun0214:46:402017][error][client192.168.5.180]Filedoesnotexist:/web/vhosts/www1/123==>/web/vhosts/www2/access_log<==192.168.5.180-[02/Jun/2017:14:46:28+0800]"GET/HTTP/1.1"200"-""curl/7.22.0(x86_64-pc-linux-gnu)libcurl/7.22.0OpenSSL/1.0.1zlib/1.2.3.4libidn/1.23librtmp/2.3"192.168.5.180-[02/Jun/2017:14:46:52+0800]"GET/456HTTP/1.1"404"-""curl/7.22.0(x86_64-pc-linux-gnu)libcurl/7.22.0OpenSSL/1.0.1zlib/1.2.3.4libidn/1.23librtmp/2.3"192.168.5.180-[02/Jun/2017:14:49:16+0800]"GET/HTTP/1.1"200"http://www.sina.com""curltest2"==>/web/vhosts/www2/error_log<==[FriJun0214:46:522017][error][client192.168.5.180]Filedoesnotexist:/web/vhosts/www2/456实验二:协议登录认证对于httpd2.2版本的www1.stuX.com虚拟主机,以及httpd2.4版本的www3.stuX.com虚拟主机,分别添加状态监控页面,并且利用第三方模块mod_auth_mysql.so对用户账户进行认证与授权。用户账户存放在192.168.5.121这个节点的mysql服务器上面。认证采用aes加密认证。详细配置方案,请参照其他博文。
在mysql里面建立一个名为http_auth的数据库,在该数据库下建立一个名为mysql_auth的数据表,在表中添加两个用户,分别为admin和root,采用aes_encrypt函数对密码进行加密,加密用的salt分别为’hello’和’root’。如下所示:
mysql>usehttp_auth;Databasechangedmysql>showtables;+---------------------+|Tables_in_http_auth|+---------------------+|mysql_auth|+---------------------+1rowinset(0.00sec)mysql>descmysql_auth;+-------------+----------+------+-----+---------+-------+|Field|Type|Null|Key|Default|Extra|+-------------+----------+------+-----+---------+-------+|user_name|char(30)|NO|PRI|NULL|||user_passwd|tinyblob|YES||NULL|||user_group|char(25)|YES||NULL|||salt|tinyblob|YES||NULL||+-------------+----------+------+-----+---------+-------+4rowsinset(0.01sec)mysql>select*frommysql_auth;+-----------+------------------+------------+-------+|user_name|user_passwd|user_group|salt|+-----------+------------------+------------+-------+|admin|?G°??P-S|admin|hello||root|???¥V′l?Gχ|admin|root|+-----------+------------------+------------+-------+2rowsinset(0.00sec)注:确保mysql开启了用户远程访问的权限,在这里使用mysql的root@’%’用户,开启访问数据库的权限:grant all pribileges on *.* to root@'%' identified by 'root' with grant option
CentOS6, httpd2.2环境
将mod_auth_mysql.so模块加载进来,确保mod_auth_mysql.so模块在操作系统中存在,并且在/etc/httpd/modules里面有副本,这样便可以使用相对于ServerRoot的相对路径来引用,在主配置文件/etc/httpd/conf/httpd.conf里面添加一行:
LoadModulemysql_auth_modulemodules/mod_auth_mysql.so以实验一的virtualhost.conf文件为基础,添加<Location>指令段开启状态页面,并针对状态页面做基于用户的协议认证,添加权限控制的选项,如下所示:
注: 针对mod_auth_mysql.so的配置指令,详细请参照该模块的文档。
注:这里的AuthBasicAuthoritative指令尤为重要,因为使用的是第三方认证模块,如果不设定为Off的话,httpd将认为该模块为非法模块从而无法使用。
............<Location/status>SetHandlerserver-statusOrderdeny,allowAllowfromallAuthTypeBasicAuthBasicAuthoritativeOffAuthName"authlogin"AuthMySQLHost192.168.5.121AuthMySQLPort3306AuthMySQLUserrootAuthMySQLPasswordshrootAuthMySQLDBhttp_authAuthMySQLUserTablemysql_authAuthMySQLNameFielduser_nameAuthMySQLPasswordFielduser_passwdAuthMySQLEnableonAuthMySQLPwEncryptionaesAuthMySQLSaltFieldsaltrequirevalid-user</Location>............配置完毕之后,用service httpd restart命令重启httpd服务。
CentOS7, httpd2.4环境
同样需要将mod_auth_mysql.so添加进来,确保mod_auth_mysql.so模块在操作系统中存在,并且在/etc/httpd/modules里面有副本,这样便可以使用相对于ServerRoot的相对路径来引用。
httpd2.4的模块加载配置文件和上面的httpd2.2的模块配置加载文件不同,需要在/etc/httpd/conf.modules.d目录下面创建一个单独的模块加载配置文件,这里创建一个名字为10-mysql.conf的配置文件,在里面添加一行:
LoadModulemysql_auth_modulemodules/mod_auth_mysql.so以实验一的virtualhost.conf文件为基础,添加<Location>指令段开启状态页面,并针对状态页面做基于用户的协议认证,添加权限控制的选项,如下所示:
注: 针对mod_auth_mysql.so的配置指令,详细请参照该模块的文档。
注:这里的AuthBasicAuthoritative指令尤为重要,因为使用的是第三方认证模块,如果不设定为Off的话,httpd将认为该模块为非法模块从而无法使用。
注:在httpd2.4里面,如果不显式定义AuthUserFile,有可能会遇到认证失败的情况。因为使用mysql里面的数据进行认证,因此这里只需要指定文件系统的认证文件为/dev/null即可。
了解更多网络知识关注:http://www.vecloud.com/