如何配置 Apache server/crontab/bash 文件以允许在白天 and/or 的特定时间从特定 IP 进行访问?

How to configure an Apache server/crontab/bash file to allow access at certain times during the day and/or from specific IPs?

我正在尝试我的导师给出的练习。我们的任务是设置一个可以访问的 VirtualHost mon-fri/1-5。最重要的是,我们必须允许随时从 IP "XX.XX.XX.XXX" 访问该站点。

当前的 crontab:

* 13 * * 1-5 root apachectl start
* 17 * * 1-5 root apachectl stop
* * * * * root ./bash

Bash 文件:

currIP=$(hostname -I)
case "$currIP" in
        *XX.XX.XX.XXX*)
              ???????
;;
esac

有人可以向我解释一下我如何将此网站提供给 IP 而不是一次提供给所有人吗?

谢谢!

假设这是您的配置文件。

/etc/apache2/httpd.conf

运行 这些命令:

sudo cp /etc/apache2/httpd.conf all.conf
sudo cp /etc/apache2/httpd.conf restricted.conf

现在在您系统的某处创建此脚本:

#!/bin/bash

[[ "" = "all" ]] && { ln -s --force /etc/apache2/all.conf /etc/apache2/httpd.conf ;}
[[ "" = "restricted" ]] && { ln -s --force /etc/apache2/restricted.conf /etc/apache2/httpd.conf ;}
sudo systemctl restart httpd

并为其添加执行权限:

sudo chmod +x /path/to/your/script.sh

现在修改/etc/apache2/restricted.conf以通过IP过滤请求。您可以在此处找到相关信息:https://httpd.apache.org/docs/2.4/es/mod/mod_authz_core.html#require

现在将根 cron 设置为:

sudo crontab -e

并在其中添加:

* 13 * * 1-5 /path/to/your/script.sh "all"
* 17 * * 1-5 /path/to/your/script.sh "restricted"

开心就好!