PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 20, 2022

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

 September 20, 2022     apache, bash, cron, linux, virtualhost     No comments   

Issue

I am attempting an exercise given by my instructor. We were tasked with setting up a VirtualHost that could be accessed mon-fri/1-5. On top of that, we must allow access to the site from IP "XX.XX.XX.XXX" at anytime.

Current crontab:

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

Bash file:

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

Can someone explain to me how I am going to serve this site to the IP without serving it to everyone at once?

Thanks!


Solution

Lets say this is your config file.

/etc/apache2/httpd.conf

Run these commands :

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

Now create this script somewhere in your system :

#!/bin/bash

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

And add execution permissions to it :

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

Now modify /etc/apache2/restricted.conf to filter requests by IP. You can find information on that here : https://httpd.apache.org/docs/2.4/es/mod/mod_authz_core.html#require

Now set the root cron as :

sudo crontab -e

And in there add this :

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

And be happy!



Answered By - Matias Barrios
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing