Execute custom filename with PHP (via Apache) - php

On my development machine, this works as expected (requests to server.com/.myhandler are executed as php), but when I uploaded to the production machine (running ubuntu server 11.04) it just serves the un-executed php. Is there anything extra I must configure for this to work?
Contents of .htaccess file:
AddType text/html .myhandler
<FilesMatch "\.myhandler$">
SetHandler application/x-httpd-php
</FilesMatch>
DirectoryIndex .myhandler index.php index.htm

Most possibly this type of configuration is not allowed via .htaccess in the directory where your script and .htaccess reside in. Put this in your Apache site / VirtualHost configuration:
<Directory /absolute/path/to/webroot/>
AllowOverride All
</Directory>

Related

Include html or php at beginning of each page

I need insert some code or file (banner) to every web site using .htaccess on php7.3-fpm.
File .htaccess.
<FilesMatch \.php$>
# Apache 2.4.10+ can proxy to unix socket
#SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/"
SetHandler "proxy:unix:/var/run/php/php7.3-fpm.sock|fcgi://localhost/"
</FilesMatch>
I tried add
SetEnv PHP_VALUE 'auto_prepend_file "/var/www/html/test/banner.php"'
or
php_value auto_prepend_file "/dir/path/banner.php"
but nothing worked. Is possible to do this via .htaccess file, or I must set it in configuration file of php-fpm?
Thanks.

PHP Version Doesn't Change After Adding AddHandler

I am trying to get one of my sites on my server to use PHP 7.3. I have added this to my .htaccess file:
AddHandler application/x-httpd-php73 .php
It is the only line in that file. My directives of the root of the site look like this:
<Directory "/var/www/html/dev">
allow from all
AllowOverride All
Require all granted
</Directory>
Whenever I run my phpInfo script, it is still reporting PHP 7.0. What am I missing?

How to execute php code in html files (php-fpm and apache 2.4) on centos 7

As we have upgrade OS from centos 6.9 to Centos 7 on server. We have installed "Apache/2.4.6" and "PHP 5.6.36 (fpm-fcgi)" on this server. But we are facing problem to execute "PHP Code" in ".html" files. PHP code working fine in ".php file" but not in ".html files". I have ready many blogs but no where mentioned exact solution.
Can you please guide us how we can execute PHP code in .html files.
Note : We are using Apache 2.4, PHP-FPM and MPM_worker on centos 7.
After change below in files "/etc/php-fpm.d/www.conf" and "/etc/httpd/conf.d/php.conf" :
"security.limit_extensions = .php .php3 .php4 .php5 .htm" // in www.conf
and
SetHandler "proxy:fcgi://127.0.0.1:9000" // in php.conf
PHP code working in htm files but due to to these changes every html files renders as php. We want only php code render as php not html code render by php.
Your help would be appreciated.
This is how I configure Apache / PHP-FPM for a virtual host. It's not a public server so I'm only using HTTPS. This is from /etc/httpd/conf.d/ssl.conf:
<VirtualHost _default_:443>
DocumentRoot "/home/myvhost/public_html"
ServerName myvirtualhost.com:443
<Directory "/home/myvhost/public_html">
allow from all
Options FollowSymLinks SymLinksIfOwnerMatch
Require all granted
php_admin_value open_basedir /home/myvhost/public_html
</Directory>
SuexecUserGroup myvhost myvhost
ProxyPassMatch ^(/.*\.php)$ fcgi://127.0.0.1:9000/home/myvhost/public_html/$1
<FilesMatch \.php$>
# SetHandler application/x-httpd-php
SetHandler "proxy:fcgi://127.0.0.1:9000"
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
</FilesMatch>
</VirtualHost>
And this is from /etc/php-fpm.d/myvhost.conf (copied from the default PHP-FPM configuration file):
[myvirtualhost.com]
user = myvhost
group = apache
listen = 127.0.0.1:9000
security.limit_extensions = .php
So basically you could just copy the "ProxyPassMatch" line and change php to html and do the same thing with "FilesMatch". You can also modify the regex but I'm not really good at that.

ProxyPassMatch and Options +Indexes (mod_autoindex)

I have a simple setup with Apache2.4 and PHP-FPM and I am trying to enable +Indexes option but I get 404 "File not found." when trying to access a folder that doesn't have an index file even when autoindex is enabled.
Here's part of my vhost:
#php
ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/var/run/fpm/fatal.sock|fcgi://
#super public directory with Indexes!
<Location /pub>
Options +Indexes
IndexOptions +FancyIndexing
</Location>
When I try to access http://domain.com/pub/ I expected to see a list of files I put there but instead I get error 404 Not Found.
I wonder where this comes from since ProxyPassMatch shouldn't forward the request because there is no .php in the query so next is directory index which looks for index.php which doesn't exists (404) but why then mod_autoindex doesn't work?
When I remove the ProxyPassMatch line the autoindex works just fine and I see the folder content listed.
Any ideas?
I found the answer here http://blog.famillecollet.com/post/2014/03/28/PHP-FPM-and-HTTPD-2.4-improvement
As the ProxyPassMatch directive is evaluated at the very beginning of each request:
AddType (for MultiView) or DirectoryIndex directives are not usable
right management per directory is not available
each Alias directive needs another proxy rule
The SetHandler directive, evaluated later, is much more flexible / usable.
So I changed my vhost to look like this and got rid of the ProxyPassMatch directive.
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/fpm/fatal.sock|fcgi://"
</FilesMatch>
Note: this solution applies to Apache 2.4.9+
(I do wonder if there are any performance difference and in what direction?)

ubuntu server 12.04 - php files downloading

hoping someone can help me, i am having the same problem with the php files trying to download.
i have tried all answers but the:
ln -s /etc/apache2/mods-available/php5.conf /etc/apache2/mods-enabled/php5.conf ln -s /etc/apache2/mods-available/php5.load /etc/apache2/mods-enabled/php5.load
when i try them i get unable to create symbolic link file exists, so i guess thats done all ready, anyone else have anything more i can try, i am new to linux and i followed this guide to set everything up: http://www.howtoforge.com/perfect-server-ubuntu-12.04-lts-apache2-bind-dovecot-ispconfig-3
the strange thing is joomla and drupal sites work, but own php script doesent.
i have tried everything on this page:
apache2 on ubuntu - php files downloading
but none of them work.
when i try to run this command: sudo a2enmod php
i get, module php does not exist. i guess i need to run a command to install it? if so what is it please.
thanks all.
Heres More info as requested:
not sure where isp config stores the folders for each website, but i connected to the ftp site and uploaded into the web folder, same as i did for the working drupal and joomla sites that are working
sites enabled:
/
000-apps.vhost#
000-default#
000-ispconfig.conf#
000-ispconfig.vhost#
100-analytics.cyberglide.co.uk.vhost#
100-cyberglide.co.uk.vhost#
100-hideandcollars.co.uk.vhost#
100-jubileeleather.co.uk.vhost#
100-rookselectrical.co.uk.vhost#
100-wolds-uniforms.co.uk.vhost#
all sites are working (they are running joomla or drupal) but www.jubileeleather.co.uk/index.php
had to put up a html file until i can get it sorted.
php script:
<?php
//Add required class and config files below
require ('_class/cms.php');
require ('config/db.php');
//Get Required Page, if its not set call it the index
$page = (isset($_GET['page'])) ? $_GET['page'] : 'index';
//If the page is set as index
if($page == 'index'){
//Include Index
require ('public/index.php');
}
//Else if the page is contact
elseif ($page == 'contact') {
//Include Contact
require ('public/contact.php');
//If the page is none of the above
} else {
//Include Layout
require ('public/layout.php');
}
?>
i am using putty via ssh, how would i copy content from a file to post on here? also which files would you need? thanks again.
here is the contents of the jubileeleather v host file, not sure what i am looking for in here:
AllowOverride None
Order Deny,Allow
Deny from all
DocumentRoot /var/www/jubileeleather.co.uk/web
ServerName jubileeleather.co.uk
ServerAlias www.jubileeleather.co.uk
ServerAdmin webmaster#jubileeleather.co.uk
ErrorLog /var/log/ispconfig/httpd/jubileeleather.co.uk/error.log
Alias /error/ "/var/www/jubileeleather.co.uk/web/error/"
ErrorDocument 400 /error/400.html
ErrorDocument 401 /error/401.html
ErrorDocument 403 /error/403.html
ErrorDocument 404 /error/404.html
ErrorDocument 405 /error/405.html
ErrorDocument 500 /error/500.html
ErrorDocument 502 /error/502.html
ErrorDocument 503 /error/503.html
<IfModule mod_ssl.c>
</IfModule>
<Directory /var/www/jubileeleather.co.uk/web>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/clients/client3/web4/web>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
# suexec enabled
<IfModule mod_suexec.c>
SuexecUserGroup web4 client3
</IfModule>
# Clear PHP settings of this website
<FilesMatch "\.ph(p3?|tml)$">
SetHandler None
</FilesMatch>
# php as fast-cgi enabled
# For config options see: http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html
<IfModule mod_fcgid.c>
IdleTimeout 300
ProcessLifeTime 3600
# MaxProcessCount 1000
DefaultMinClassProcessCount 0
DefaultMaxClassProcessCount 100
IPCConnectTimeout 3
IPCCommTimeout 360
BusyTimeout 300
</IfModule>
<Directory /var/www/jubileeleather.co.uk/web>
AddHandler fcgid-script .php .php3 .php4 .php5
FCGIWrapper /var/www/php-fcgi-scripts/web4/.php-fcgi-starter .php
Options +ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/clients/client3/web4/web>
AddHandler fcgid-script .php .php3 .php4 .php5
FCGIWrapper /var/www/php-fcgi-scripts/web4/.php-fcgi-starter .php
Options +ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
# add support for apache mpm_itk
<IfModule mpm_itk_module>
AssignUserId web4 client3
</IfModule>
<IfModule mod_dav_fs.c>
# Do not execute PHP files in webdav directory
<Directory /var/www/clients/client3/web4/webdav>
<FilesMatch "\.ph(p3?|tml)$">
SetHandler None
</FilesMatch>
</Directory>
DavLockDB /var/www/clients/client3/web4/tmp/DavLock
# DO NOT REMOVE THE COMMENTS!
# IF YOU REMOVE THEM, WEBDAV WILL NOT WORK ANYMORE!
# WEBDAV BEGIN
# WEBDAV END
</IfModule>
Apart from having the own PHP files in a directory with PHP parsing enabled (can't really say how it's done with Apache as I'm not using for some time), you should check if your script has the correct extension (should be always .php) and script opening tag <?php - don't use short tag <? to avoid possible problems when moving the script to live server.
Tho since your files are returned for download, I think you don't have your scripts in directory with enabled PHP parsing. Posting your Apache and PHP config files could help us to identify the problem.

Categories