configure apache run run php in html pages - apache

How can I make apache parse php code inside my html pages? I don't wanna use .htaccess. I've added:
AddType application/x-httpd-php .html
AddType application/x-httpd-php .php
in my vhost file for one of the websites but it won't work.

Did you build your Apache binary with PHP? Or if you built a SO, reference it in the Apache config (e.g.: LoadModule php5_module modules/libphp5.so)?
When using .html for PHP, ensure 'AddType text/html .html' is removed, so it doesn't conflict with the x-httpd-php reference.
FYI -- you can create one entry 'AddType application/x-httpd-php .html .php'

I have the same problem in the latest versions (Debian 10 , Apache 2.4.25, PHP 7.0.33)
My solution is edit the php.config file, to find it I put:
find -iname php*.conf
...and in my debian instalation I found it in:
/etc/apache2/mods-available/php7.0.conf
Then, I introduce those lines at begining:
<FilesMatch ".+\.html$">
SetHandler application/x-httpd-php
</FilesMatch>
And it work for me.

Related

apache2 and php5-fpm, how to correctly process *.phps files?

I've setup my server using apache2 and php5-fpm, pretty much like in this guide:
http://www.versatilewebsolutions.com/blog/2012/11/installing-apache-2-with-php-fastcgi-on-ubuntu-12-10.html
The only question left is, how can I configure it to correctly process the *.phps files?
I want to show the highlighted code of them.
There are guides for nginx and php-fpm ( https://blog.kamalnasser.net/post/highlighting-phps-files-in-nginx/ ), but what should we do with apache2 ?
Caveats: Ubuntu 14.10, Apache/2.4.10
In the file /etc/apache2/mods-available/php5.conf:
<FilesMatch ".+\.phps$">
SetHandler application/x-httpd-php-source
# Deny access to raw php sources by default
# To re-enable it's recommended to enable access to the files
# only in specific virtual host or directory
Order Deny,Allow
Deny from all
</FilesMatch>
Add a with "Allow from all" (or something more appropriate to your paranoia-level) to you host config, and you'll be all set.
My config:
<Directory /my/very/long/and/winding/server/path/public_html/phpsource/>
<FilesMatch ".+\.phps$">
Order Deny,Allow
Allow from all
</FilesMatch>
</Directory>
Php Source files in other directories get denied, the ones in that directory gets displayed. It works like a charm...
This should do it:
AddType application/x-httpd-php-source .phps
Add it to your apache config file. There should be a similar line like this:
AddType application/x-httpd-php .php
So just add it below.

Deploying Mono MVC in shared hosting environment

I'm not posting on serverfault because the application is under development and I need to test it in Mono, but it could be suitable there too...
I would like not to ask if it's possible to deploy an ASP.NET MVC website on Apache2 VirtualHost, because it is, but rather if it's possible without interfering with PHP configuration.
The long question
Apache's default PHP configuration looks like this
<IfModule mod_php5.c>
AddHandler application/x-httpd-php .php4
AddHandler application/x-httpd-php .php5
AddHandler application/x-httpd-php .php
AddHandler application/x-httpd-php-source .php4s
AddHandler application/x-httpd-php-source .php5s
AddHandler application/x-httpd-php-source .phps
DirectoryIndex index.php4
DirectoryIndex index.php5
DirectoryIndex index.php
</IfModule>
And this tells Apache that .php files are PHP scripts.
OK...
Now if I enable mod_mono on server, it maps .aspx files to Mono. And that's OK.
But when I enable the test host VirtualHost with a barebone configuration that is perfect for PHP (it's based on mod_macro if you mind the dollars)
<VirtualHost *:80>
ServerName www.$name
ServerAlias $name
<IfModule mpm_itk_module>
AssignUserID $user $user
</IfModule>
ErrorLog /path/tp/$name/logs/error_log
CustomLog /path/to/$name/logs/access_log combined
DocumentRoot /path/to/$name/htdocs
ServerAdmin email#email
</VirtualHost>
Then I get a 404 error for the default controller.
I know I can, on my own test server, simply say SetHandler mono and voilĂ . But the question affects general-purpose hosting. Also, I could have the answer by myself: since there is no .aspx file in Razor-MVC (or better paths are mapped to controllers via routers) then I obviously get a 404 error for everything that Apache doesn't recognize as a file.
If I want to deploy this Mono application on a server run by a generalistic hosting provider that supports Mono, or vice versa if I would like to offer (on a production server) hosting for PHP and Mono too without having to change configuration according to customer's demands, could I use an Apache configuration that both satisfies Mono and runs PHP?
The shortened question
How can I configure Apache2 once in order to run both PHP and Mono MVC on the same VirtualHost without changing the configuration according to language the application is written in?
go to this configurator
http://go-mono.com/config-mod-mono/
it provide auto make config for Apache2, based on virtualHost.
Now i'm using mod_mono and mod_php5 jointly.

AddType / AddHandler Not Working

I'm working on a mac running OS X Lion and PHP 5.3.6 and have tried both AddType, AddHander, and AllowOverride has been set to ALL in the httpd.conf; however, the PHP codes in HTML/JS/CSS files are still parsed as text. Files ending with .php are all good. I'm now getting really desperate after hours of googling.
Here are the contents of my .htaccess file:
AddType application/x-httpd-php5 .html .js .css
AddType application/x-httpd-php .html .js .css
AddHandler application/x-httpd-php5 .html .js .css
AddHandler application/x-httpd-php.html .js .css
If your site is stored in your user's Sites folder (i.e. /Users/username/Sites/) then you also need to set AllowOveride in the user-specific configuration file in /etc/apache2/users/username.conf. After making the change restart apache by disabling and re-enabling Web Sharing in the Sharing preference pane.
The AddType procedure (at least on Mac OS X Server) requires:
one and only extension per line
no dots before the extension
AddType application/x-httpd-php php
Have a look at /private/etc/apache2/httpd.conf for running examples.
(You should also remove the AddHandler procedures.)
I ran into same problem and fixed with:
<Files ~ "\.(gif|htm|txt|css|jpg|png)$">
SetHandler default-handler
</Files>

How do I conditionally add a line to Apache .htaccess based on the domain?

My local development PC is set up to use PHP5 by default, my client's host (1and1.co.uk) is set up to use PHP4 by default.
To enable PHP5 on a 1and1 account, you must add the following line to your .htaccess file:
AddType x-mapp-php5 .php
If I add this line to my local .htaccess file, it breaks my PHP.
How do I add this line conditionally based on the domain (or some other parameter?) so it is only executed by the live site and not my dev site?
I'd like to be able to just upload (FTP) my entire source tree without worrying about having to remember to edit the .htaccess on the server ever time.
e.g.
<IfDomain www.example.com>
AddType x-mapp-php5 .php
</IfDomain>
... rest of .htaccess
Is this possible?
I don't have access to the server config file, only .htaccess
Thanks in advance.
To others looking at this, and using a local server with virtualhost directives: took me a while, and none of the methods found online worked (probably because my MAMP apache version is not 2.4 yet or something), but this one did.
Forget about adding 'conditional' lines to your .htaccess file: you can define which .htaccess file to use in your virtualhost setting!
So just create a file .htaccess_dev, and add this line to your virtualhost definition on your local / dev machine:
AccessFileName .htaccess_dev
So it becomes something like this:
<virtualhost>
ServerName www.example.local
DirectoryRoot /var/www/www.example.local
AccessFileName .htaccess_dev
</virtualhost>
Try this. I have no idea whether it'll work. The terminology used in the documentation suggests that it should, but since no one ever does what you're trying to do, it's a little up in the air.
<Location http://www.example.com/>
AddType x-mapp-php5 .php
</Location>
I know that you can do server-specific commands in httpd.conf using the VirtualHost tag. (http://httpd.apache.org/docs/1.3/vhosts/ip-based.html)
While I'm not 100% sure that you can use this in a .htaccess file, I would suggest trying the following:
<VirtualHost www.example.com>
AddType x-mapp-php5 .php
</VirtualHost>
You can use the VirtualHost directive to accomplish this. I just did the same when I wanted django with mod_python to run only at a subdomain
<VirtualHost *:80>
DocumentRoot /www/docs/path
ServerName www.example.com
....
AddType x-mapp-php5 .php
</VirtualHost>

How do you configure the Apache server which ships Mac OS X?

Mac OS X ships with apache pre-installed, but the files are in non-standard locations. This question is a place to collect information about where configuration files live, and how to tweak the apache installation to do things like serve php pages.
Apache Config file is: /private/etc/apache2/httpd.conf
Default DocumentRoot is: /Library/Webserver/Documents/
To enable PHP, at around line 114 (maybe) in the /private/etc/apache2/httpd.conf file is the following line:
#LoadModule php5_module libexec/apache2/libphp5.so
Remove the pound sign to uncomment the line so now it looks like this:
LoadModule php5_module libexec/apache2/libphp5.so
Restart Apache: System Preferences -> Sharing -> Un-check "Web Sharing" and re-check it.
OR
$ sudo apachectl restart
Running
$ httpd -V
will show you lots of useful server information, including where the httpd.conf file can be found.
To get SSI/includes (mod_include) to work I found I had to edit /private/etc/apache2/users/myusername.conf and change AllowOverride None to AllowOverride All.
Then add the following in a .htaccess file in the root of your site:
Options +Includes
AddType text/html .html
AddOutputFilter INCLUDES .html
httpd.conf is in /private/etc/apache2
Enable PHP by uncommenting line:
LoadModule php5_module libexec/apache2/libphp5.so
/etc/httpd/users contains user-specific configuration files which can be used to override the global configuration. For example, adding "AddHandler server-parsed html" to the <Directory> block in the /etc/httpd/users/*.conf file that corresponds to one user will enable mod_include parsing of HTML files for that particular user's $HOME/Sites directory, but nowhere else.