24

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
user1075581
  • 495
  • 1
  • 4
  • 14
  • What OS are you running apache on? Can you show us your config files for php / apache vhost? – Erik Feb 16 '12 at 04:51
  • OS is CentOS 5.5. As far as config files go, I don't have any .htaccess files in place, so the only governing config files (I think) are the httpd.conf and php.ini files. Are there any particular files aside from these I should be looking at? What configuration parameters within them? I think my VirtualHost lines are fine because the different ports serve different content, but PHP isn't working - so I could very well be quite wrong about that. – user1075581 Feb 16 '12 at 04:55
  • You should have a `something.vhost` files in your `sites-available` folder `/etc/apache2/sites-available/` do you have a specific vhost file for every port or do you have one single config file? If you only have a single file e.g: `default` or maybe you only use `httpd.confg` post what it says below `` – Erik Feb 16 '12 at 05:06
  • I searched (from root) for any files named *.vhost and came up with nothing -- same with searching for "sites-available". The apache2 directory you referenced doesn't exist either (different distros, I suppose?) I'll edit my original post to include the `` tag, as it will format better than this quick comment response. – user1075581 Feb 16 '12 at 05:11
  • I need to run as http://domainname:8080/ then it may work – Poonam Feb 16 '12 at 05:19
  • Have you added the appropriate `Listen` directives, eg `Listen 8080`? Also, you aren't using named virtual hosts so there's no reason to use `NameVirtualHost` – Phil Feb 16 '12 at 05:19
  • @Poonam -- Sorry, that's not it. Tried it again, and a no-go. @Phil -- Yes, the appropriate Listen directives are in place. Again, the virtual hosts are working, but PHP isn't parsing on the second one. As far as the `NameVirtualHost` directive, I'm just following the instructions in the Apache document I linked in my post. – user1075581 Feb 16 '12 at 05:26
  • Be sure you are restarting the httpd service every time you make a config change. – Mike Purcell Feb 16 '12 at 06:16

11 Answers11

28

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.

Ricky Theil
  • 381
  • 3
  • 2
18

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>
Peter O.
  • 32,158
  • 14
  • 82
  • 96
user1075581
  • 495
  • 1
  • 4
  • 14
8

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

apt-get install libapache2-mod-php
LostMyGlasses
  • 3,074
  • 20
  • 28
7

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

Lazik
  • 2,480
  • 2
  • 25
  • 31
3

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>
Grimm
  • 103
  • 3
3

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

AddType application/x-httpd-php .php
Jesús Carrera
  • 11,275
  • 4
  • 63
  • 55
2

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

LoadModule php5_module libexec/apache2/libphp5.so
agaase
  • 1,562
  • 1
  • 15
  • 24
2

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

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

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>
Erik
  • 222
  • 1
  • 5
1

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

Tmute
  • 43
  • 1
  • 8
1

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

apt-get install libapache2-mod-php

atiquratik
  • 1,296
  • 3
  • 27
  • 34