我尝试验证了Apache的基本认证

首先

因为在工作中遇到了Apache基本认证,由于不太了解,所以决定进行验证。

基本认证,也被称为Basic认证,是一种不对密码进行加密的认证方式。

我参考了以下文章:
https://atmarkit.itmedia.co.jp/flinux/rensai/linuxtips/698apachebasic.html

我参考了《Linux教科书 LPIC级别2 Version 4.5对应》这本小豆书籍。

建造步骤

创建VPC

请在左侧窗格中选择您所使用的VPC,然后点击“创建VPC”即可。

如果您错误地删除了默认VPC,则将不会再次创建它。请按照以下步骤创建新的VPC。

image.png
image.png

创建 EC2 实例

image.png

创建安全组

image.png

如果EC2的子网路由表中没有添加互联网网关的路由,则无法进行SSH连接。请注意。

安装apache

升格为根节点

$ sudo su -

进行Apache安装。

# yum install httpd

如果在安装完Apache后输出了以下内容,那就表示顺利完成了。

Installed:
  httpd.x86_64 0:2.4.57-1.amzn2

Dependency Installed:
  apr.x86_64 0:1.7.2-1.amzn2                      apr-util.x86_64 0:1.6.3-1.amzn2.0.1        apr-util-bdb.x86_64 0:1.6.3-1.amzn2.0.1        generic-logos-httpd.noarch 0:18.0.0-4.amzn2
  httpd-filesystem.noarch 0:2.4.57-1.amzn2        httpd-tools.x86_64 0:2.4.57-1.amzn2        mailcap.noarch 0:2.1.41-2.amzn2                mod_http2.x86_64 0:1.15.19-1.amzn2.0.1

Complete!

我已经启动了Apache服务。

# systemctl start httpd

我已经将Apache服务自动启动配置完成。

# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

我已经检查了Apache服务的状态。

# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2023-05-19 23:50:44 UTC; 2min 39s ago
     Docs: man:httpd.service(8)
 Main PID: 3588 (httpd)
   Status: "Total requests: 2; Idle/Busy workers 100/0;Requests/sec: 0.0126; Bytes served/sec:  10 B/sec"
   CGroup: /system.slice/httpd.service
           tq3588 /usr/sbin/httpd -DFOREGROUND
           tq3589 /usr/sbin/httpd -DFOREGROUND
           tq3590 /usr/sbin/httpd -DFOREGROUND
           tq3591 /usr/sbin/httpd -DFOREGROUND
           tq3592 /usr/sbin/httpd -DFOREGROUND
           tq3593 /usr/sbin/httpd -DFOREGROUND
           mq3656 /usr/sbin/httpd -DFOREGROUND

May 19 23:50:44 ip-10-0-149-64.ap-northeast-1.compute.internal systemd[1]: Starting The Apache HTTP Server...
May 19 23:50:44 ip-10-0-149-64.ap-northeast-1.compute.internal systemd[1]: Started The Apache HTTP Server.
[root@ip-10-0-149-64 ~]#

创建index.html文件

我已经创建了一个用于基本身份验证的文件夹,以及该文件夹下的index.html文件。

# cd /var/www/html

創建基本認證的資料夾。

# mkdir test
# ls -tlr
total 4
-rw-r--r-- 1 root root 183 May 19 23:48 index.html
drwxr-xr-x 2 root root  24 May 20 00:09 test

在Basic认证的文件夹下创建index.html。

你好世界的创建方法可在以下网站引用:

HTMLで「Hello World」を表示してみよう

# cd test
[root@ip-10-0-149-64 test]# ls -tlr
total 4
-rw-r--r-- 1 root root 183 May 19 23:48 index.html
[root@ip-10-0-149-64 test]#
[root@ip-10-0-149-64 test]#
[root@ip-10-0-149-64 test]# cat index.html
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
      <title>テスト</title>
  </head>
  <body>
    <h1>Hello World!!</h1>
  </body>
</html>>
  </body>
</html>

将Basic认证添加到Apache配置文件中。

获取Apache配置文件的备份

# cd /etc/httpd/conf/
# cp -p httpd.conf httpd.confbk

我已经在Apache的配置文件中添加了基本认证。

# vi httpd.conf

161行目~168行目に追加
161 <Directory "/var/www/html/test">
162
163    AuthType Basic
164    AuthName "Please Enter Your ID and Password"
165    AuthUserFile /etc/httpd/conf/htpasswd
166    Require valid-user
167
168 </Directory>

左边是设置文件,右边是先前的变更前设置文件。

# sdiff -s httpd.conf httpd.confbk

                                                              <
                                                              <
                                                              <
<Directory "/var/www/html/test">                              <
                                                              <
   AuthType Basic                                             <
   AuthName "Please Enter Your ID and Password"               <
   AuthUserFile /etc/httpd/conf/htpasswd                      <
   Require valid-user                                         <
                                                              <
</Directory>                                                  <                                                              

用户账号注册

您可以使用以下命令创建基本身份验证用户。
并且会新建一个/etc/httpd/conf/htpasswd文件。

如果「/etc/httpd/conf/htpasswd」已经存在于当前服务器上,建议在添加用户之前最好先备份一下。

# htpasswd -c /etc/httpd/conf/htpasswd test
New password:
Re-type new password:
Adding password for user test

账户已创建。密码已加密。

# cat /etc/httpd/conf/htpasswd
test:$apr1$0ZfL72is$0meCXhfZKBKzEiKX18M580

当您添加“-c”时,htpasswd将会被重新创建,之前设置的用户信息将会被清除。请注意。
对于已存在的用户,可以修改密码。

如果对现有的htpasswd文件新增一个test2用户,可以使用以下命令。在进行密码更改时也使用以下命令。

# htpasswd  /etc/httpd/conf/htpasswd test2
New password:
Re-type new password:
Adding password for user test2

已成功添加了 test2 用户。

# cat /etc/httpd/conf/htpasswd
test:$apr1$0ZfL72is$0meCXhfZKBKzEiKX18M580
test2:$apr1$6zTl2wXa$wmng9A66R9e6YxWXWYAfS/

更改htpasswd的权限

# cd /etc/httpd/conf
# chown apache:apache htpasswd

请打开浏览器

image.png
image.png
bannerAds