I’m on an apache2 installation and want to deny access to a specific php file with a .htaccess file
<Files test.php>
Require all denied
</Files>
Which does not work. However, denying access to a different file type does work as expected
<Files test.html>
Require all denied
</Files>
I’ve tried with various file types, all work as expected, except for php files. What do I miss here? I’m using php-fpm.
2
Answers
My Test Environment
Ubuntu 20.04
Install Apache 2 And Php
Start Apache
modify /etc/hosts
append
demoweb
anddemoweb.local
into 127.0.0.1test demoweb.local
Create My Domain – demoweb
Create Domain Directory
Create Domain index.html
/var/www/demoweb/index.html
Create Domain Config
/etc/apache2/sites-available/demoweb.conf
Very Important
Enable your Domain
Create your .htaccess
/var/www/demoweb/.htaccess
Create Test php
/var/www/demoweb/info.php
/var/www/demoweb/info2.php
Restart Apache
Test 1
use Firefox or curl open
http://demoweb.local/info2.php
Test 2
use Firefox or curl open
http://demoweb.local/info.php
will get error:
IMPORTANT:
if test all ok , delete
/var/www/demoweb/info2.php
and/var/www/demoweb/info.php
Summary
I am test all steps OK in my ubuntu 20.4
Apache 2 + php-fpm
My Test Environment
Ubuntu 20.04
Install Apache 2 And Php
Enable Necessary Apache Modules
Configure Apache to Use PHP-FPM
Within the <VirtualHost *:80> block, add the following lines:
Note: Ensure to replace php7.4-fpm.sock with the actual socket path for your PHP version. You can check this by looking into the /run/php/ directory.
Note: No .htaccess , I put it in
/etc/apache2/sites-available/000-default.conf
Restart Apache and PHP-FPM Services
Create Test php
Add the following content:
Add the following content:
Test 1
use Firefox or curl open
http://localhost/info2.php
Test 2
use Firefox or curl open
http://localhost/info.php
will get error:
IMPORTANT:
if test all ok , delete
/var/www/html/info.php
and/var/www/html/info2.php
Summary
I am test all steps OK in my ubuntu 20.4
My test environment always starts with re-copying a cleanly initialized Ubuntu VM.