综合练习:给openlab搭建web网站
网站需求:
前提准备
systemctl stop firewalld
setenforce 0
yum install httpd -y
yum install mod_ssl
1.基于域名访问网站
基于域名[www.openlab.com](http://www.openlab.com)可以访问网站内容为: welcome to openlab!!!
1、在httpd子配置文件 书写配置,方便更改以及排错 - - - 同样以conf.d结尾
# * * * 在/etc/httpd/conf.d/vhosts.conf 中编辑子配置文件 * * * # [root@localhost www]# vim /etc/httpd/conf.d/vhosts.conf [root@localhost www]# cat /etc/httpd/conf.d/vhosts.conf <Virtualhost 192.168.84.132:80> ServerName www.openlab.com DocumentRoot /www/openlab </virtualhost> <Directory /www> AllowOverride none Require all granted </Directory>
2、创建对应的目录,编辑测试页面
[root@localhost mnt]# mkdir -pv /www/openlab mkdir: 已创建目录 '/www' mkdir: 已创建目录 '/www/openlab' # * * * 在index.html中编辑页面 * * * # [root@localhost www]# vim /www/openlab/index.html [root@localhost www]# cat /www/openlab/index.html <html> <head> <title>Openlab</title> <style> body { background-color: lightblue; } h1 { color: white; text-align: center; } </style> </head> <body> <h1>Welcome to Openlab!!!</h1> </body> </html>
3.修改hosts文件,定义IP地址和主机名的映射关系
# * * * 将我们的IP 和域名 加在后面 * * * # [root@localhost www]# vim /etc/hosts [root@localhost www]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.84.132 www.openlab.com
4.重启服务,测试
[root@localhost www]# systemctl restart httpd [root@localhost www]# curl www.openlab.com <html> <head> <title>Openlab</title> <style> body { background-color: lightblue; } h1 { color: white; text-align: center; } </style> </head> <body> <h1>Welcome to Openlab!!!</h1> </body> </html>
2.创建三个子界面
给该公司创建三个子界面分别显示学生信息,教学资料和缴费网站,
[www.openlab.com/student] (http://www.openlab.com/student) 网站访问学生信息,[www.openlab.com/data] (http://www.openlab.com/data)网站访问教学资料 [www.openlab.com/money] (http://www.openlab.com/money)网站访问缴费网站。
1.通过配置虚拟主机的方式来实现多个子目录的访问
[root@localhost www]# cat /etc/httpd/conf.d/vhosts.conf <Virtualhost 192.168.84.132:80> ServerName www.openlab.com DocumentRoot /www/openlab Alias /student /www/student Alias /data /www/data Alias /money /www/money </virtualhost> <Directory /www> AllowOverride none Require all granted </Directory>
2.创建对应的目录,以及测试内容
[root@localhost www]# mkdir -pv /www/student mkdir: 已创建目录 '/www/student' [root@localhost www]# mkdir -pv /www/data mkdir: 已创建目录 '/www/data' [root@localhost www]# mkdir -pv /www/money mkdir: 已创建目录 '/www/money' [root@localhost www]# vim /www/student/index.html # * * * 内容略 * * * # [root@localhost www]# vim /www/data/index.html # * * * 内容略 * * * # [root@localhost www]# vim /www/money/index.html # * * * 内容略 * * * #
3.重启服务、测试
3.用户访问控制、制作证书
学生信息网站只有song和tian两人可以访问,其他用户不能访问。
访问缴费网站实现数据加密基于https访问。
1.因为要对学生信息网站做控制用户访问 所以要增加一些配置并删除 Require all granted
<Directory /www/data> AllowOverride none AuthType basic AuthName "login" AuthUserfile /etc/httpd/users Require user song Require user tian </Directory>
2.添加用户信息,第一次需要加C,以后不需要 否则会被覆盖
[root@localhost li]# htpasswd -c /etc/httpd/users song New password: Re-type new password: Adding password for user song [root@localhost li]# htpasswd /etc/httpd/users tian New password: Re-type new password: Adding password for user tian [root@localhost li]# cat /etc/httpd/users song:$apr1$52aQBK3f$vzmk7unQt8D0ytDmep5PD/ tian:$apr1$B8v4pYfA$LS5IDnZxVz/OLoGjWO3d50
3. 测试
4.制作服务器证书
因为我们不曾指定路径 所以制作的证书和密钥会放在当时的工作目录
建议将crt文件放在 /etc/pki/tls/certs/ key文件放在 /etc/pki/tls/private/
[root@localhost tls]# openssl req -newkey rsa -nodes -keyout openlab.key -x509 -days 365 -out openlab.crt ...+............+.+......+.........+.....+............+...+.+.....+.+..+.+..............+.......+...+..+.+..+...+..........+...+...........+......+....+...+.....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.+......+..+.+......+.....+.+........+.+...+......+...............+...........+.........+.........+.+..+....+......+...+......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..+.+...+......+...+..+...+...+.............+..+....+.....+.+....................+....+...........+.........+.+.....+...+....+...+...+...............+............+........+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .........+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..+...+..+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*....+......+........+.......+.....+.............+..+...+......+.+......+........+.......+..+.......+.....+...+.......+......+...+..............+.+.....+.......+...+.....+.+.....+.........+......+......+.........+.+...+..+.......+..+...+.+............+..+......+......+.+........+.......+...+......+...............+..................+...............+..+....+.....+.......+...+...........+.+....................+...+............+....+....................+.+.....+............+......+....+...........+....+..+.+...+......+......+...+..+..................+.......+...+.....................+.....+...+...+...+.+...............+...+..+...+......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:li State or Province Name (full name) []:ninxia Locality Name (eg, city) [Default City]:yinchuan Organization Name (eg, company) [Default Company Ltd]:no Organizational Unit Name (eg, section) []:no Common Name (eg, your name or your server's hostname) []:redhat Email Address []:1907781431@qq.com
5.开启认证引擎 ,输入密钥密钥和证书的所在的位置
<Virtualhost 192.168.84.132:443> ServerName www.openlab.com DocumentRoot /www/openlab Alias /student /www/student Alias /data /www/data Alias /money /www/money SSLEngine on SSLCertificateFile /etc/pki/tls/certs/openlab.crt SSLCertificateKeyFile /etc/pki/tls/private/openlab.key </virtualhost>
6.测试 (很明显,可以)