1、建立httpd服务器(基于编译的方式进行),要求:
提供两个基于名称的虚拟主机:
(a)www1.itab.com,页面文件目录为/web/vhosts/www1;错误日志为/var/log/httpd/www1.err,访问日志为/var/log/httpd/www1.access;
(b)www2.itab.com,页面文件目录为/web/vhosts/www2;错误日志为/var/log/httpd/www2.err,访问日志为/var/log/httpd/www2.access;
(c)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名;
(d)通过www1.itab.com/server-status输出httpd工作状态相关信息,且只允许提供帐号密码才能访问(status:status);
(e) www1不允许192.168.1.0/24网络中的主机访问;
2、为上面的的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点;
(1)要求使用证书认证,证书中要求使用的国家(CN)、州(HA)、城市(ZZ)和组织(iTab);
(2)设置部门为Ops,主机名为www2.itab.com,邮件为admin@stuX.com;
现在分别使用httpd-2.2和httpd-2.4搭建符合以上要求的HTTP/HTTPS服务。
httpd-2.2
(1) 使用yum安装httpd服务程序。
[root@web~]#yum-yinstallhttpd
(2) 要想使用虚拟主机,必须先注释掉中心主机的文档根路径(DocRoot)。
[root@web~]#vim/etc/httpd/conf/httpd.conf#DocumentRoot"/var/www/html"
(3) 在/etc/httpd/conf.d目录下创建vhost.conf文件,专门用于配置虚拟主机。
NameVirtualHost192.168.10.128:80<VirtualHost192.168.10.128:80>#第一台虚拟主机配置ServerNamewww1.itab.com#第一台虚拟主机的主机名DocumentRoot"/web/vhosts/www1"#第一台虚拟主机的DocRootErrorLoglogs/www1.err#错误日志路径CustomLoglogs/www1.accesscombined#访问日志路径<Directory"/web/vhosts/www1">#基于IP地址做访问控制Orderallow,denyDenyfrom192.168.1#不允许192.168.1.0/24网络中的主机访问</Directory><Location/server-status>#第一台虚拟主机开启server-status工作状态输出功能SetHandlerserver-statusAuthTypeBasic#基于账号密码做控制AuthName"Enteryourusernameandpassword."#认证提示AuthUserFile"/etc/httpd/.htpasswd"#存放账号密码的文件路径Requireuserstatus#只允许status用户登录</Location></VirtualHost><VirtualHost"192.168.10.128:80">#第二台虚拟主机配置ServerNamewww2.itab.com#第二台虚拟主机的主机名DocumentRoot"/web/vhosts/www2"#第二台虚拟主机的DocRootErrorLoglogs/www2.err#错误日志路径CustomLoglogs/www2.accesscombined#访问日志路径</VirtualHost>
(4) 创建虚拟主机的文档根目录,并为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名。
[root@web~]#mkdir-pv/web/vhosts/{www1,www2}mkdir:createddirectory`/web'mkdir:createddirectory`/web/vhosts'mkdir:createddirectory`/web/vhosts/www1'mkdir:createddirectory`/web/vhosts/www2'[root@web~]#cat/web/vhosts/www1/index.html<h1>www1.itab.com</h1>[root@web~]#cat/web/vhosts/www2/index.html<h1>www2.itab.com</h1>
(5) 设置站点主页面。
[root@web~]#vim/etc/httpd/conf/httpd.confDirectoryIndexindex.html
(6) 创建访问status页面的账号(status)和密码(status)。
[root@web~]#htpasswd-cm/etc/httpd/.htpasswdstatusNewpassword:Re-typenewpassword:Addingpasswordforuserstatus
(7) 检查语法错误,如果没有错误,则启动http服务。
[root@web~]#httpd-tSyntaxOK[root@web~]#servicehttpdstartStartinghttpd:[OK]
(8) 查看httpd是否已经监听在80端口。
[root@web~]#ss-tnl|grep80LISTEN0128:::80:::*
(9) 测试。
9.1 测试status页面。
使用192.168.10.140的主机测试,如下。
[root@test1~]#elinkswww1.itab.com/server-status
打开页面如下。
650) this.width=650;” src=”/d/file/jishu/2017-05-22/1ebc7bd08dfa8db3ca43cc1f8b9a4faf.png” style=”float:none;” title=”121212.PNG” alt=”wKioL1khh8axV3eYAACII3hT1aA365.png” />
650) this.width=650;” src=”/d/file/jishu/2017-05-22/5863e0fa91d32874eb8a5aef8995b5da.png” title=”12121222.PNG” alt=”wKioL1khh96jdd69AACJ-aR4L_c494.png” />
650) this.width=650;” src=”/d/file/jishu/2017-05-22/d5f1a0aab7dd9a1093153001b78c4170.png” title=”1111.PNG” style=”float:none;” alt=”wKiom1khh8bhSjxvAACDo9UJyrY377.png” />
9.2 测试两个虚拟主机的页面。
同样使用192.168.10.140这台主机来测试,如下。
[root@test1~]#elinkswww1.itab.com
访问www1.itab.com时,打开页面如下。
650) this.width=650;” src=”/d/file/jishu/2017-05-22/cacf6e4e3a7d8e5dc2221e495bc55d6e.png” title=”1.PNG” alt=”wKiom1khiJjwWexXAAAbZDcvdpM442.png” />
[root@test1~]#elinkswww2.itab.com
访问www2.itab.com时,打开页面如下。
650) this.width=650;” src=”/d/file/jishu/2017-05-22/e7b770a557da65f4a3480878bd59c702.png” title=”2.PNG” alt=”wKioL1khiLOR3UGnAAAhuD55N6E149.png” />
对于192.168.1.0/24网段的主机,则无法访问www1.itab.com,其打开页面如下。
650) this.width=650;” src=”/d/file/jishu/2017-05-22/8b1f302d1975afa07f4d163db8f81687.png” title=”3.PNG” alt=”wKioL1khiYvDUUmBAAAtt1UOZoY031.png” />
(10) 现在为上面的的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点。
为了方便,这里通过建立私有CA来提供服务器证书。CA主机和Webf服务器主机如下:
CA主机:192.168.10.140Web服务器主机:192.168.10.128
10.1 在CA主机(192.168.10.128上)建立私有CA
① CA生成私钥。
[root@test1~]#(umask077;opensslgenrsa-out/etc/pki/CA/private/cakey.pem2048)
② 生成自签证书。
[root@test1~]#opensslreq-new-x509-key/etc/pki/CA/private/cakey.pem-out/etc/pki/CA/cacert.pem-days365Youareabouttobeaskedtoenterinformationthatwillbeincorporatedintoyourcertificaterequest.WhatyouareabouttoenteriswhatiscalledaDistinguishedNameoraDN.TherearequiteafewfieldsbutyoucanleavesomeblankForsomefieldstherewillbeadefaultvalue,Ifyouenter'.',thefieldwillbeleftblank.-----CountryName(2lettercode)[XX]:CNStateorProvinceName(fullname)[]:HALocalityName(eg,city)[DefaultCity]:ZZOrganizationName(eg,company)[DefaultCompanyLtd]:iTabOrganizationalUnitName(eg,section)[]:OpsCommonName(eg,yournameoryourserver'shostname)[]:ca.itab.comEmailAddress[]:caadmin@itab.com
③ 为CA提供所需的目录及文件。
[root@test1~]#mkdir-pv/etc/pki/CA/{certs,crl,newcerts}[root@test1~]#touch/etc/pki/CA/{serial,index.txt}[root@test1~]#echo01>/etc/pki/CA/serial
10.2 在Web服务器主机(192.168.10.128)上创建证书签署请求。
① Web服务器主机生成私钥。
[root@web~]#(umask077;opensslgenrsa-out/etc/httpd/ssl/httpd.key2048)
② Web服务器生成证书签署请求。
[root@web~]#opensslreq-new-key/etc/httpd/ssl/httpd.key-out/etc/httpd/ssl/httpd.csr-days365Youareabouttobeaskedtoenterinformationthatwillbeincorporatedintoyourcertificaterequest.WhatyouareabouttoenteriswhatiscalledaDistinguishedNameoraDN.TherearequiteafewfieldsbutyoucanleavesomeblankForsomefieldstherewillbeadefaultvalue,Ifyouenter'.',thefieldwillbeleftblank.-----CountryName(2lettercode)[XX]:CNStateorProvinceName(fullname)[]:HALocalityName(eg,city)[DefaultCity]:ZZOrganizationName(eg,company)[DefaultCompanyLtd]:iTabOrganizationalUnitName(eg,section)[]:OpsCommonName(eg,yournameoryourserver'shostname)[]:www2.itab.comEmailAddress[]:admin@itab.comPleaseenterthefollowing'extra'attributestobesentwithyourcertificaterequestAchallengepassword[]:Anoptionalcompanyname[]:
③ 通过可靠方式将证书签署请求发送给CA主机。这里为了演示方便,所以直接通过scp命令传送。
[root@web~]#scp/etc/httpd/ssl/httpd.csrroot@192.168.10.140:/tmp/
10.3 在CA主机(192.168.10.140)上签署证书请求。
① CA主机为Web服务器签证。
[root@test1~]#opensslca-in/tmp/httpd.csr-out/etc/pki/CA/certs/httpd.crt-days365Usingconfigurationfrom/etc/pki/tls/openssl.cnfCheckthattherequestmatchesthesignatureSignatureokCertificateDetails:SerialNumber:1(0x1)ValidityNotBefore:May2113:50:382017GMTNotAfter:May2113:50:382018GMTSubject:countryName=CNstateOrProvinceName=HAorganizationName=iTaborganizationalUnitName=OpscommonName=www2.itab.comemailAddress=admin@itab.comX509v3extensions:X509v3BasicConstraints:CA:FALSENetscapeComment:OpenSSLGeneratedCertificateX509v3SubjectKeyIdentifier:C9:0A:4A:B8:2C:67:8A:1C:68:D5:1E:F6:40:57:21:BD:48:75:D5:75X509v3AuthorityKeyIdentifier:keyid:81:F9:59:3E:F7:7C:8C:A4:1A:6B:7F:6F:9E:4F:8B:A6:18:A5:20:30CertificateistobecertifieduntilMay2113:50:382018GMT(365days)Signthecertificate?[y/n]:y1outof1certificaterequestscertified,commit?[y/n]yWriteoutdatabasewith1newentriesDataBaseUpdated
② CA主机将已签署的证书通过可靠方式传送给该Webf服务器。这里同样使用scp命令。
[root@test1~]#scp/etc/pki/CA/certs/httpd.crtroot@192.168.10.128:/etc/httpd/ssl/
(11) Web服务器在已经得到CA签署的证书了,现在开始配置HTTPS服务,首先安装mod_ssl模块。
[root@web~]#yuminstallmod_ssl
查看ssl_module是否已经加载。
[root@web~]#httpd-M|grepssl_modulessl_module(shared)#ssl模块已经加载SyntaxOK
(12) 修改/etc/httpd/conf.d/ssl.conf配置文件。
[root@web~]#vim/etc/httpd/conf.d/ssl.conf#修改以下配置项<VirtualHost192.168.10.128:443>DocumentRoot"/web/vhosts/www2"ServerNamewww2.itab.comSSLCertificateFile/etc/httpd/ssl/httpd.crtSSLCertificateKeyFile/etc/httpd/ssl/httpd.key
检查语法错误。
[root@web~]#httpd-tSyntaxOK
重载服务。
[root@web~]#servicehttpdreload
查看是否监听在443端口。
[root@web~]#ss-tnl|grep443LISTEN0128:::443:::*
(13) 使用Web服务器本主机测试。
[root@web~]#openssls_client-connectwww2.itab.com:443-CAfile/etc/pki/CA/cacert.pemCONNECTED(00000003)......(中间省略)......New,TLSv1/SSLv3,CipherisECDHE-RSA-AES256-GCM-SHA384Serverpublickeyis2048bitSecureRenegotiationISsupportedCompression:NONEExpansion:NONESSL-Session:Protocol:TLSv1.2Cipher:ECDHE-RSA-AES256-GCM-SHA384Session-ID:379BE969583B315BC9C8C43F64107EE2CD9CB12668ECA26725D8749BA9CDABD2Session-ID-ctx:Master-Key:FB5258D5C5FB3EF484831FC306B883EA84D106399CC5630A34169E7B2949FD45A9B90D025AB82EC34887A7C9D8E3F7E2Key-Arg:NoneKrb5Principal:NonePSKidentity:NonePSKidentityhint:NoneTLSsessionticketlifetimehint:300(seconds)TLSsessionticket:0000-55bada0d93a967a4-461b21ab6fa3035dU.....g.F.!.o..]0010-6eaad823258d7d8a-41db47eaa93bfb7cn..#%.}.A.G..;.|0020-7059b2a36ccd7e00-4e8a3eef7d6bf468pY..l.~.N.>.}k.h0030-590a2d6da11b3684-627c3f515d24aa90Y.-m..6.b|?Q]$..0040-a87f0aa9f7a3fab2-a0ea30ef35807d13..........0.5.}.0050-668cd247beb5471f-6490e9c7ce7ea66ff..G..G.d....~.o0060-10e5ab589870ab14-07ca04b0c42fd8f6...X.p......./..0070-705285f21c9313ca-0cef7eb25af3f209pR........~.Z...0080-e3fde6d101184b63-74ceb99cc0a52e1e......Kct.......0090-8c9a7d7f3569b381-01647a9c2c612407..}.5i...dz.,a$.00a0-2969afb819a778ff-2a2ea58ffe1121b0)i....x.*.....!.00b0-8678fdb7ae553b10-44b4812502400ac5.x...U;.D..%.@..StartTime:1491487607Timeout:300(sec)Verifyreturncode:21(unabletoverifythefirstcertificate)---GET/index.htmlHTTP/1.1#测试能否获取www2.itab.com的主页面资源Host:www2.itab.comHTTP/1.1200OK#获取成功Date:Thu,06Apr201714:06:57GMTServer:Apache/2.2.15(CentOS)Last-Modified:Thu,06Apr201711:29:45GMTETag:"4342e-19-54c7dd36ab98c"Accept-Ranges:bytesContent-Length:25Connection:closeContent-Type:text/html;charset=UTF-8<h1>www2.itab.com</h1>closed[root@web~]#
测试完毕,使用httpd-2.2搭建HTTP/HTTPS服务成功。接下来使用httpd-2.4搭建同样的HTTP/HTTPS服务。
httpd-2.4
httpd-2.4跟httpd-2.2不同在于任意目录下的页面只有显式授权才能被访问,因此,在配置虚拟主机访问控制时,如下所示。
[root@test1~]#cat/etc/httpd/conf.d/vhosts.conf<VirtualHost192.168.10.140:80>ServerNamewww1.itab.comDocumentRoot"/web/vhosts/www1"ErrorLoglogs/www1.errCustomLoglogs/www1.accesscombined<Location/server-status>SetHandlerserver-statusAuthTypeBasicAuthName"Enteryourusernameandpassword"AuthUserFile"/etc/httpd/.htpasswd"Requireuserstatus</Location><Directory"/web/vhosts/www1"><RequireAll>#显示授权RequireallgrantedRequirenotip192.168.10.128</RequireAll></Directory></VirtualHost><VirtualHost192.168.10.140:80>ServerNamewww2.itab.comDocumentRoot"/web/vhosts/www2"ErrorLoglogs/www2.errCustomLoglogs/www2.accesscombined<Directory"/web/vhosts/www2"><RequireAll>#显示授权RequireallgrantedRequirenotip192.168.10.128</RequireAll></Directory></VirtualHost>
还有在配置HTTPS时,也是需要显式授权才能被访问到,如下。
[root@test1~]#vim/etc/httpd/conf.d/ssl.conf<VirtualHost192.168.10.140:443>DocumentRoot"/web/vhosts/www2"ServerNamewww2.itab.com<Directory"/web/vhosts/www2">#显式授权Requireallgranted</Directory>SSLCertificateFile/etc/httpd/ssl/httpd2.crtSSLCertificateKeyFile/etc/httpd/ssl/httpd.key</VirtualHost>
除此之外,其它配置同httpd-2.2,这里不再赘述。