linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」

(38) 2023-06-22 18:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」,希望能够帮助你!!!。

一、安装apache服务

1.挂载镜像,设备状态全选,在选择iso镜像文件

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第1张

桌面出现光盘图标为挂载成功

 linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第2张

在终端打开输入以下命令 

mount /dev/sr0 /mnt  

出现下图为成功

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第3张

2.配置yum源(方便解决依赖关系)  输入以下命令   

cd /etc/yum.repos.d

rm -rf *

vim 1.repo  

1.repo的内容

[name]

baseurl=file:///mnt

enable=1

gpgcheck=0

3.安装apache 

yum install httpd  -y

4.重启apache服务

  systemctl restart httpd

4.输入lll到网站首页   

  echo 'llll' > /var/www/html/index.html    

6.访问网站

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第4张

安装时可能出现进程已锁定,在睡眠中

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第5张

有以下两种解决方法

(1)删除

rm -rf   /var/run/yum.pid

(2)删进程,ps查看进程,找到对应的进行号,进程需要杀两次

kill -9 pid号

ps

kill -9 pid号

二、apache主配置文件参数

apache主配置文件在/etc/httpd/conf/httpd.conf

全局配置,全局生效

listen 监听的ip和端口号

DocumentRoot  "/var/www/html"    后面加网站根目录

DirectoryIndex index.html   定义网站首页

局部配置,局部生效

<Directory "/var/www">         只对/var/www  生效
    AllowOverride None   
    Require all denyded          关闭远程访问(除本地ip以外的同网段ip)

     Require all granted           开启远程访问(除本地ip以外的同网段ip)
</Directory>

三、虚拟主机访问

(1)多ip访问

1.将网卡绑定多个ip,将ip设置为静态,添加多个ip地址

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第6张

ip a|grep 'inet'  查看是否绑定成功,ping看网络连通性

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第7张

 2.修改配置文件,在配置文件靠后加入以下内容,(在前面加可能出现配置不生效)

<VirtualHost 192.168.52.128>   
DocumentRoot /var/www/html/1
<Directory /var/www/html/1>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.52.129>
DocumentRoot /var/www/html/2
<Directory /var/www/html/2>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
 

3.添加内容,并访问

mkdir /var/www/html/1
mkdir /var/www/html/2
echo 'hello 192.168.52.128!' > /var/www/html/1/index.html
echo 'hello 192.168.52.129!' > /var/www/html/2/index.html
systemctl restart httpd

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第8张

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第9张

(2)多端口访问

1.修改监听端口

listen 81       在配置文件靠上

listen 82

2.修改配置文件,在配置文件靠后加入以下内容,(在前面加可能出现配置不生效)

<VirtualHost 192.168.52.128:81>   
DocumentRoot /var/www/html/81
<Directory /var/www/html/81>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.52.128:82>
DocumentRoot /var/www/html/82
<Directory /var/www/html/82>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

3.添加内容,并访问

mkdir /var/www/html/81
mkdir /var/www/html/82
echo 'hello 81' > /var/www/html/81/index.html
echo 'hello 82' > /var/www/html/82/index.html
systemctl restart httpd

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第10张

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第11张

(3)多域名访问

1.修改本地hosts文件

文件位置:/etc/hosts

添加以下内容

192.168.52.128 www.q.com www.a.com

用ping或nslookup检测是否绑定成功

2.修改配置文件

添加以下内容

<VirtualHost 192.168.52.128>   
DocumentRoot /var/www/html/q
ServerName www.q.com
<Directory /var/www/html/q>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.52.128>
DocumentRoot /var/www/html/a
ServerName www.q.com
<Directory /var/www/html/a>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

3.添加内容,并访问

mkdir /var/www/html/q
mkdir /var/www/html/a
echo 'hello www.q.com' > /var/www/html/q/index.html
echo 'hello www.a.com' > /var/www/html/a/index.html
systemctl restart httpd

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第12张

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第13张

四、用户名密码认证访问

1.修改配置文件

AllowOverride AuthConfig                              打开认证

AuthName ""                                                   提示信息
AuthType basic                                               加密类型 
AuthUserFile  /etc/httpd/user1.txt                   用来验证用户名和密码的文件

require valid-user                                             允许上面文件的用户登录

找到一个目录(Directory)添加以下内容,限制用户对目录的访问

<Directory /var/www/html>
AllowOverride AuthConfig
Authname '1'
Authtype basic
AuthUserfile /etc/httpd/user1.txt
require valid-user
</Directory>

2.添加用户

htpasswd -bc /etc/httpd/user1.txt z3 123456

3.添加内容访问

 echo '1' > /var/www/html/index.html

systemctl restart httpd

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第14张

 linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第15张

五、虚拟目录

虚拟目录就是一个别名,正常网站来说,访问的是/var/www/html根目录的文件,虚拟目录,就是把/var/html 当成根目录的一部分,比如说访问192.168.52.128/www  就是在访问/var/html目录

1.在配置文件修改

添加以下内容

Alias /www    /var/html                                       一个为访问时的路径(虚拟),第二个为真实路径      
<Directory /var/html>
    Options Indexes FollowSymLinks
    AllowOverride all
    Require all granted
</Directory>

2.添加内容访问

echo 'xunimulu' > /var/www/index.html

systemctl restart httpd

linux的apache三种虚拟主机访问与用户名密码认证访问「建议收藏」_https://bianchenghao6.com/blog__第16张

 

上一篇

已是最后文章

下一篇

已是最新文章

发表回复