Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow server to run cron jobs but block access to normal user with .htacess

have a cron job that is located in a folder in the normal directory.

I am trying to not allow access to this file by anyone else EXCEPT my server for running the cron job.

I tried:

order deny,allow
deny from all
allow from 127.0.0.1

But no luck. I have gone down the root of putting the cron job outside the web root but i could not get it to run no matter what me and my host tried.

Thanks.

like image 505
Lovelock Avatar asked Sep 10 '25 08:09

Lovelock


1 Answers

Two things here: (a) getting the cronjob to run, (b) access restriction.

Cronjob

New crontab entry:

*/10 * * * * /usr/bin/php /somewhere/www/cronjob.php

Set correct permissons on cronjob.php:

  • Execute flag: chmod +x /somewhere/www/cronjob.php

Access Restriction

In general, it is a good practice, to place the script files for cronjobs outside of the www path.

If you really need to place them in www, then you might protect them with an access restriction. For the webserver Apache, this would work via .htaccess, like so:

.htaccess inside /somewhere/www/:

<Files "cronjob.php">
Order Allow,Deny
Deny from all
</Files>

This protects the file cronjob.php from outside access, but allows cron to execute the file.

If nothing works, follow my step by step guide: https://stackoverflow.com/a/22744360/1163786

like image 183
Jens A. Koch Avatar answered Sep 12 '25 21:09

Jens A. Koch