Apache基本认证配置

Nginx基本认证配置 类似,可以配置简单的密码文件认证(其实这个方法就是 Apache 内置方法, Nginx 也是借用Apache的工具实现)

  • 安装Apache工具包:

安装Apache工具
sudo apt update
sudo apt install apache2-utils
  • 创建密码文件,这里创建了一个用户 huatai :

创建密码文件
sudo htpasswd -c /etc/apache2/.htpasswd huatai

如果要添加新用户,则去掉 -c 参数(创建文件)就可以

Apache 简单配置 加上基本认证
<VirtualHost *:80>
	ServerAdmin webmaster@localhost

	#DocumentRoot /var/www/html
	DocumentRoot /home/huatai/docs/github.com/huataihuang/cloud-atlas/build/html
        <Directory "/home/huatai/docs/github.com/huataihuang/cloud-atlas/build/html">
            Options Indexes FollowSymLinks
            #AllowOverride All
            #Order allow,deny
            #Allow from all
            #Require all granted

            AuthType Basic
            AuthName "Restricted Content"
            AuthUserFile /etc/apache2/.htpasswd
            Require valid-user
        </Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

备注

Apache 简单配置 不同,必须去除原先取消安全限制的配置部分,即:

<Directory "/home/huatai/docs/github.com/huataihuang/cloud-atlas/build/html">
    ...
    #AllowOverride All
    #Order allow,deny
    #Allow from all
    #Require all granted
    ...
</Directory>

否则认证会被覆盖掉(即不生效)

参考