RHCE(三)--- 基于HTTP、HTTPS搭建静态网页

目录

一、http配置文件

二、综合练习:请给openlab搭建web网站

1、配置 /etc/httpd/conf.d/vhosts.conf(首先确保安装httpd)

2、创建对应目录和用户

3、写入相应内容到相应目录下的 index.hcml文件中(网页显示的内容)

4、配置缴费网站(www.openlab.com/money)基于https访问

(1)安装mod_ssl 加密模块

(2)生成证书

(3)修改 /etc/httpd/conf.d/vhosts.conf 下的关于www.openlab.com/money部分

5、重启服务

三、测试

1、在缓存文件 /etc/hosts 中添加 IP 与域名信息

2、访问www.openlab.com

3、只有 song 和 tian 可以访问www.openlab.com/student,其他用户不能访问

4、访问www.openlab.com/data

5、数据加密访问www.openlab.com/money


一、http配置文件

1、httpd主配置文件在 /etc/httpd/conf/httpd.conf

2、额外的参数文件 /etc/httpd/conf.d/*.conf

3、设置默认访问apache欢迎界面的配置文件 /etc/httpd/conf.d/welcome.conf

4、apache欢迎界面的具体文件 /usr/share/httpd/noindex/index.html

5、默认的首页所在目录/var/www/html/,当输入网址时所显示的数据,就是放在这个目录当中的首页文件(默认为index.html)

6、自己定义的静态页面是在/var/www/html目录,因为主配置文件中DocumentRoot "/var/www/html"  #网页文件存放的目录

7、静态网页的名字是index.html 是因为主配置文件中目录模块配置的目录索引,索引文件名为index.html

<IfModule dir_module> #加载一个目录模块
     DirectoryIndex index.html
</IfModule>

8、默认给一些可执行的CGI(网页程序)程序放置的目录/var/www/cgi-bin/,当输入网址/cgi-bin/时所显示的数据所在

9、默认的Apache日志文件都放在/var/log/httpd/


二、综合练习:请给openlab搭建web网站

1.基于域名[www.openlab.com](http://www.openlab.com)可以访问网站内容为 welcome to openlab!!!


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网站访问缴费网站)

3.要求(1)学生信息网站只有song和tian两人可以访问,其他用户不能访问

          (2)访问缴费网站实现数据加密基于https访问

1、配置 /etc/httpd/conf.d/vhosts.conf(首先确保安装httpd)

[root@server ~]# vim /etc/httpd/conf.d/vhosts.conf


<Virtualhost 192.168.225.140:80>
        DocumentRoot /www/openlab
        ServerName www.openlab.com
</Virtualhost>

<Virtualhost 192.168.225.140:80>
        DocumentRoot /www/openlab/student
        ServerName www.openlab.com/student
</Virtualhost>

<Virtualhost 192.168.225.140:80>
        DocumentRoot /www/openlab/data
        ServerName www.openlab.com/data
</Virtualhost>

<Virtualhost 192.168.225.140:80>
        DocumentRoot /www/openlab/money
        ServerName www.openlab.com/money
</Virtualhost>

<Directory /www/openlab>
        AllowOverride none
        Require all granted
</Directory>

//用户认证
<Directory /www/openlab/student>
        AuthType Basic                    //基本认证类型
        AuthName "Please login:"          //提示信息
        AuthUserFile /etc/httpd/userfile  //用户认证文件的用户名和密码指定的文件所在位置
        Require user song     //指定哪个用户可以访问服务器
        Require user tian
</Directory>

2、创建对应目录和用户

[root@server ~]# mkdir /www/openlab/{student,data,money} -pv
mkdir: created directory '/www'
mkdir: created directory '/www/openlab'
mkdir: created directory '/www/openlab/student'
mkdir: created directory '/www/openlab/data'
mkdir: created directory '/www/openlab/money'

[root@server ~]# htpasswd -c /etc/httpd/userfile song
New password: 
Re-type new password: 
Adding password for user song
[root@server ~]# htpasswd /etc/httpd/userfile tian
New password: 
Re-type new password: 
Adding password for user tian
[root@server ~]# htpasswd /etc/httpd/userfile wu
New password: 
Re-type new password: 
Adding password for user wu

3、写入相应内容到相应目录下的 index.hcml文件中(网页显示的内容)

[root@server ~]# echo welcome to openlab > /www/openlab/index.html
[root@server ~]# echo student information > /www/openlab/student/index.html
[root@server ~]# echo teaching information > /www/openlab/data/index.html
[root@server ~]# echo payment information > /www/openlab/money/index.html

4、配置缴费网站(www.openlab.com/money)基于https访问

(1)安装mod_ssl 加密模块

[root@server ~]# yum install mod_ssl -y
[root@server ~]# vim /etc/httpd/conf.d/ssl.conf

<VirtualHost _default_:443>
SSLEngine on      //开启ssl认证访问
SSLCertificateFile /etc/pki/tls/certs/localhost.crt     //指定证书路径
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key   //指定私钥文件路径

(2)生成证书

[root@server ~]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout /etc/pki/tls/private/openlab.key -x509 -days 365 -out /etc/pki/tls/certs/openlab.crt
Generating a RSA private key
..........................................................................................++++
.............................................................++++
writing new private key to '/etc/pki/tls/private/openlab.key'
-----
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]:86
State or Province Name (full name) []:shannxi                
Locality Name (eg, city) [Default City]:xi'an   
Organization Name (eg, company) [Default Company Ltd]:openlab
Organizational Unit Name (eg, section) []:ce
Common Name (eg, your name or your server's hostname) []:xixi
Email Address []:ada
[root@server ~]# ll /etc/pki/tls/private/
total 4
-rw-------. 1 root root 3272 Sep  6 20:45 openlab.key
[root@server ~]# ll /etc/pki/tls/certs
total 4
lrwxrwxrwx. 1 root root   49 Jun 17  2021 ca-bundle.crt -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
lrwxrwxrwx. 1 root root   55 Jun 17  2021 ca-bundle.trust.crt -> /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
-rw-r--r--. 1 root root 2057 Sep  6 20:50 openlab.crt

(3)修改 /etc/httpd/conf.d/vhosts.conf 下的关于www.openlab.com/money部分

[root@server ~]# vim /etc/httpd/conf.d/vhosts.conf 

<Virtualhost 192.168.225.140:443>   //修改端口为443
        DocumentRoot /www/openlab/money
        ServerName www.openlab.com/money
        SSLEngine on          //开启ssl认证访问
        SSLCertificateFile /etc/pki/tls/certs/openlab.crt        //指定证书路径
        SSLCertificateKeyFile /etc/pki/tls/private/openlab.key   //指定私钥文件路径
</Virtualhost>

5、重启服务

[root@server ~]# systemctl restart httpd


三、测试

1、在缓存文件 /etc/hosts 中添加 IP 与域名信息

[root@server ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.225.140 www.openlab.com
windows 下的 hosts 文件路径:
C:\Windows\System32\drivers\etc\hosts
 

2、访问www.openlab.com

[root@server ~]# curl www.openlab.com
welcome to openlab

3、只有 song 和 tian 可以访问www.openlab.com/student,其他用户不能访问

[root@server ~]# curl www.openlab.com/student/ -u song
Enter host password for user 'song':
student information
[root@server ~]# curl www.openlab.com/student/ -u tian
Enter host password for user 'tian':
student information
[root@server ~]# curl www.openlab.com/student/ -u wu
Enter host password for user 'wu':
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>      //请求未经授权
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>

4、访问www.openlab.com/data

[root@server ~]# curl www.openlab.com/data/
teaching information

5、数据加密访问www.openlab.com/money

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值