user1075581
user1075581

Reputation: 505

Apache Virtual Host not parsing PHP

I decided to enable virtual hosts on my Apache server, and chose to make it port-based.

First thing I did, of course, was RTM. I followed the instructions found here. Well, it worked -- kind of. As far as the virtual host running, it does. The content pulled from :80 is different from :8080.

But PHP isn't working. The "original site", (port 80) is running PHP just great. The port 8080 site, however, sends the PHP to the browser. I see nothing in the browser, but the source code shows:

<?php
echo "It worked!";
?>

This topic seems to be very loosely documented on a few websites, but either I can't find a solution in them, or the solution listed isn't working for me.

Again, the virtual host itself is running fine. PHP, on the other hand, is not.

Any ideas on what it could be? What content from my httpd.conf file should I provide so I don't blow up my question by copy/pasting the whole thing?

(Sorry I forgot to post that I had these in place, Phil. Adding to avoid further confusion)

Listen 80
Listen 8080

NameVirtualHost *:80
NameVirtualHost *:8080

<VirtualHost *:80>
    ServerName mysite.com
    DocumentRoot /var/www/vhosts/Site1/httpdocs
</VirtualHost>

<VirtualHost *:8080>
    ServerName mysite.com
    DocumentRoot /var/www/vhosts/Site2/httpdocs
</VirtualHost>

I tried adding this inside the tags:

AddHandler php5-script .php
AddType text/html .php

...but to no avail.

Upvotes: 25

Views: 91943

Answers (11)

Muhammad Qasim
Muhammad Qasim

Reputation: 11

This command works very well on GNU/Linux 4.4.0-105-generic x86_64

apt-get install libapache2-mod-php

Upvotes: 1

LostMyGlasses
LostMyGlasses

Reputation: 3144

In my case, the problem was fixed by running the following command:

apt-get install libapache2-mod-php

Upvotes: 8

Edwin Chaidir
Edwin Chaidir

Reputation: 31

In my case it was a default setting in php.conf. It says:

# Running PHP scripts in user directories is disabled by default

Check your php.conf (for PHP 7.1 it's accordingly /etc/apache2/mods-enabled/php7.1.conf) and comment the mentioned lines:

   root@zxxxx:/home/pxxx/public_html# vi /etc/apache2/mods-enabled/php5.conf
   # To re-enable PHP in user directories comment the following lines
   # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
   # prevents .htaccess files from disabling it.
       <IfModule mod_userdir.c>
        <Directory /home/*/public_html>
            ***php_admin_flag engine On*** -> Turn this option ON
        </Directory>
    </IfModule>

Upvotes: 3

the only thing that helped me after all tried add apache2.conf

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Upvotes: 3

Tmute
Tmute

Reputation: 43

This helped my out a2enmod php5, if the module does not exist reinstall lamp-server by typing apt-get install lamp-server^

Upvotes: 1

Jes&#250;s Carrera
Jes&#250;s Carrera

Reputation: 11425

For my configuration I had to add this line to the virtualhost (inside <Directory>):

AddType application/x-httpd-php .php

Upvotes: 3

agaase
agaase

Reputation: 1612

Make sure the following line which loads the php module is not commented out -

LoadModule php5_module libexec/apache2/libphp5.so

Upvotes: 2

Lazik
Lazik

Reputation: 2520

Your answer did not work for me.

For Ubuntu 12.04:

sudo a2enmod php5
sudo service apache2 restart

This did it.

source: https://help.ubuntu.com/community/ApacheMySQLPHP

Upvotes: 7

Ricky Theil
Ricky Theil

Reputation: 391

This could also be due to php files using short php tags <? instead of <?php. By default, short tags are not enabled in the php.ini config file.

Upvotes: 29

user1075581
user1075581

Reputation: 505

This finally put me on the right path:

http://www.linuxquestions.org/questions/linux-server-73/php-not-working-on-one-vhost-but-works-on-all-others-851093/

Here's the solution:

In the <Directory> section, I included these lines:

<IfModule sapi_apache2.c>
    php_admin_flag engine on
</IfModule>
<IfModule mod_php5.c>
    php_admin_flag engine on
</IfModule>

Or, a redacted copy/paste of the solution on my server:

<Directory "/var/www/vhosts/A2/httpdocs">
    <IfModule sapi_apache2.c>
        php_admin_flag engine on
    </IfModule>
    <IfModule mod_php5.c>
        php_admin_flag engine on
    </IfModule>

    (Other configuration parameters)

</Directory>

Upvotes: 18

Erik
Erik

Reputation: 222

I'll have to load up a centos vm to check the apache conf but on ubuntu I have a lot more info in my config under the virtualHost

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
    </Directory>

Upvotes: 2

Related Questions