25

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.

8
  • What OS are you running apache on? Can you show us your config files for php / apache vhost? Commented Feb 16, 2012 at 4: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. Commented Feb 16, 2012 at 4: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 <VirtualHost *:8080> Commented Feb 16, 2012 at 5: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 <VirtualHost> tag, as it will format better than this quick comment response. Commented Feb 16, 2012 at 5:11
  • I need to run as domainname:8080 then it may work Commented Feb 16, 2012 at 5:19

11 Answers 11

29

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This sounds like those tags could be enabled. If yes where?
Oh man! This was totally my problem! You can enable them by setting the short_open_tag php.ini setting to On.
Same for me. It was enough to enable this settings in the file located at /etc/php/7.4/apache2/php.ini
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>

2 Comments

I too had to include php_admin_flag engine on because PLESK panel somehow disabled it on every new vhost.
How to adapt it to php7 ?
8

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

apt-get install libapache2-mod-php

Comments

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

Comments

3

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

AddType application/x-httpd-php .php

1 Comment

for those who want to embed php code in .html file can do 'AddType application/x-httpd-php .php .html .htm'
3

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

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

Comments

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>

1 Comment

Please explain a bit more how this solves the issue.
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>

Comments

2

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

LoadModule php5_module libexec/apache2/libphp5.so

Comments

1

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

Comments

1

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

apt-get install libapache2-mod-php

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.