练习实践 web中间件httpd-id:2-编译安装-web登录认证设置

参考来源:
用编译的方式安装apache httpd服务

编译安装过程记录

1.下载准备环境:

yum -y install gcc gcc-c++ make pcre pcre-devel gd-devel openssl-devel  zlib zlib-devel  apr-*

根据之前的操作文档和实际安装经验,提前将所需依赖项安装好,避免出现报错,需要停下检查后,还需要重新执行,减少时间花费;

2.使用wget下载应用软件压缩包,使用tar工具解压,进入httpd目录中

[root@centos7 www]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.62.tar.bz2
--2024-07-24 17:28:11--  https://mirrors.aliyun.com/apache/httpd/httpd-2.4.62.tar.bz2
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 182.49.63.193, 182.49.63.186, 182.49.63.187, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|182.49.63.193|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:7521661 (7.2M) [application/x-bzip]
正在保存至: “httpd-2.4.62.tar.bz2”

100%[==========================================>] 7,521,661    531KB/s 用时 14s    

2024-07-24 17:28:25 (540 KB/s) - 已保存 “httpd-2.4.62.tar.bz2” [7521661/7521661])

[root@centos7 httpd-2.4.62]# tar xf httpd-2.4.62.tar.bz2
[root@centos7 httpd-2.4.62]# cd httpd-2.4.62
[root@centos7 httpd-2.4.62]# ls
ABOUT_APACHE     CHANGES          httpd.dep       libhttpd.mak    README.cmake
acinclude.m4     changes-entries  httpd.dsp       LICENSE         README.platforms
Apache-apr2.dsw  CMakeLists.txt   httpd.mak       Makefile.in     ROADMAP
Apache.dsw       config.layout    httpd.spec      Makefile.win    server
apache_probes.d  config.log       include         modules         srclib
ap.d             config.nice      INSTALL         NOTICE          support
build            configure        InstallBin.dsp  NWGNUmakefile   test
BuildAll.dsp     configure.in     LAYOUT          os              VERSIONING
BuildBin.dsp     docs             libhttpd.dep    README
buildconf        emacs-style      libhttpd.dsp    README.CHANGES

配置软件模块

./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi

./configure \
--prefix=/usr/local/httpd \		#指定将 httpd 服务程序的安装路径
--enable-so \					#启用动态加载模块支持,使 httpd 具备进一步扩展功能的能力
--enable-rewrite \				#启用网页地址重写功能,用于网站优化、防盗链及目录迁移维护
--enable-charset-lite \			#启动字符集支持,以便支持使用各种字符集编码的页面
--enable-cgi					#启用CGI(通用网关接口)脚本程序支持,便于网站的外部扩展应用访问能力

编译安装

make				#make -j 2  表示开2核同时进行编译
make install

------编译安装完成

web登录认证设置

web登录认证的场景介绍
用户认证的用途:当一个网站被访问的时候,需要输入用户名和密码才能进入,而不是直接登录网站,这种认证的形式可以针对网站的一个目录进行,也可以针对单个的访问文件进行

优化配置文件路径,并把httpd服务的可执行程序文件放入路径环境变量的目录中便于系统识别

ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/

检查配置文件语法

[root@centos7 httpd-2.4.62]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::7f9:9a7d:e440:9c3b. Set the 'ServerName' directive globally to suppress this message
Syntax OK

解决方法,在[root@centos7 local]# vim /usr/local/httpd/conf/httpd.conf的205行,添加

ServerName localhost:80

在这里插入图片描述

配置文件编辑

将默认httpd-vhosts.conf配置文件全部删除,配置文件路径

[root@centos7 local]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf

小技巧tips:vim工具中可以使用高效删除文本全部内容

在 Vim 中,清空整个文件内容可以使用以下命令:
打开文件后,按 Esc 确保处于普通模式。
输入 gg 跳转到文件的第一行。
输入 dG 删除从当前行到文件末尾的所有内容

将下面内容写入配置文件中,

<VirtualHost *:80>
    DocumentRoot "/www/abc" ##为将要访问的页面的目录         
<Directory /www/abc>
    AllowOverride AuthConfig #允许对/www/abc 目录下的内容进行用户认证
    AuthName "xiaoyue"     ##指定存放的用户                         
    AuthType Basic
    AuthUserFile /www/.htpasswd  # #指定存放用户名和密码的文件
    require valid-user
</Directory>
</VirtualHost>

配置虚拟主机访问权限

207 #
208 # Deny access to the entirety of your server's filesystem. You must
209 # explicitly permit access to web content directories in other 
210 # <Directory> blocks below.
211 #
212 <Directory />
213     AllowOverride none
214 #   Require all denied
215     Require all granted
216 </Directory>

vhost功能启用

编辑Apache的主配置文件,打开虚拟主机配置,在vim工具中,通过:/httpd-vhost 查询关键词,将vhost功能启用

[root@centos7 local]# vim /usr/local/httpd/conf/httpd.conf 
487 # Virtual hosts
488 Include conf/extra/httpd-vhosts.conf

htpasswd生成密码文件

[root@centos7 local]# htpasswd -c /www/.htpasswd xiaoyue
New password: 
Re-type new password: 
Adding password for user xiaoyue
[root@centos7 local]# cat /www/.htpasswd 
xiaoyue:$apr1$ZhDxzvAD$oEKDCz98M4Fx7RE9W93Ar.

检查配置参数并尝试启动

[root@centos7 bin]# apachectl graceful
httpd not running, trying to start
[root@centos7 bin]# apachectl start
httpd (pid 23473) already running

验证效果,出现输入用户名和密码页面即表示成功

通过elinks工具验证

[root@centos7 bin]# elinks http://127.0.0.1/www/abc

在这里插入图片描述

通过浏览器工具验证

在这里插入图片描述
快速回顾:
编译安装:从哪些渠道能够得到软件包,编译安装httpd如何实现?
httpd认证设置:配置文件中修改的内容,配置用户认证的htpasswd工具使用,验证效果的命令行浏览器elinks;
–END–

  • 12
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值