LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-27-2017, 10:59 AM   #1
irish2
LQ Newbie
 
Registered: May 2015
Posts: 6

Rep: Reputation: Disabled
PHP not working with virtual hosts on apache 24


I am running Apache 2.4 on Redhat 7.2, I have also installed PHP 5.6.

I created a vhosts file for a site

<VirtualHost *:80>
DocumentRoot /var/www/html/ex1.mysite.com
ServerName ex1.mysite.com
ErrorLog logs/ex1.mysite.com-error-log
CustomLog logs/ex1.mysite.com-access_log common
<Directory /var/www/html/ex1.mysite.com>
AllowOverride AuthConfig FileInfo
Order allow,deny
allow from all
</Directory>
I am able to view index.html in my browser, however when I attempt to load a PHP file, I get a message "File Not Found". I am able to view a PHP file in the default location (var/www/html).

Is there a way to get PHP working in a virtual host?
 
Old 02-27-2017, 11:48 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
I am able to view index.html in my browser, however when I attempt to load a PHP file, I get a message "File Not Found". I am able to view a PHP file in the default location (var/www/html).

Is there a way to get PHP working in a virtual host?
Since you can view php pages in docroot, you should be able to also view php in the subdirectory /var/www/html/ex1.mysite.com
Perhaps you did a typo giving the "Not Found" error. Check the apache error_log (ex1.mysite.com-error-log) to see what you get from it.

Regards
 
Old 02-27-2017, 12:02 PM   #3
irish2
LQ Newbie
 
Registered: May 2015
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by bathory View Post
Since you can view php pages in docroot, you should be able to also view php in the subdirectory /var/www/html/ex1.mysite.com
Perhaps you did a typo giving the "Not Found" error. Check the apache error_log (ex1.mysite.com-error-log) to see what you get from it.

Regards
Thanks bathory, I am not doing a typo, I have setup a few virtual hosts to test and the html files work fine.

I get this error from the log

"AH01071: Got error 'Primary script unknown"

For some reason, apache seems not to recognise the php files in the virtual host directories. Strange.
 
Old 02-27-2017, 12:26 PM   #4
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
I get this error from the log

"AH01071: Got error 'Primary script unknown"
Huh, this error means that you're running php-fpm and not as the common apache module.
If that's the case, you need to add the code you're using to call the php-fpm script in the main httpd.conf (ProxyMatch or SetHandler), into the vhost stanza too.
 
Old 02-27-2017, 02:26 PM   #5
irish2
LQ Newbie
 
Registered: May 2015
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by bathory View Post
Huh, this error means that you're running php-fpm and not as the common apache module.
If that's the case, you need to add the code you're using to call the php-fpm script in the main httpd.conf (ProxyMatch or SetHandler), into the vhost stanza too.
Thanks for the reply.

The guide I used was

https://aws.amazon.com/premiumsuppor...apache-rhel72/

So, I am using this in the httpd.conf

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1

And in the vhost

<Directory /path/to/blog/public>
<FilesMatch \.php$>
SetHandler None
ForceType text/plain
</FilesMatch>
<FilesMatch \.phps$>
SetHandler application/x-httpd-php-source
</FilesMatch>
</Directory>


I still have the same issue, I guess I have done something wrong
 
Old 02-27-2017, 02:34 PM   #6
irish2
LQ Newbie
 
Registered: May 2015
Posts: 6

Original Poster
Rep: Reputation: Disabled
I am now using this in my vhost

<FilesMatch \.php$>
# 2.4.10+ can proxy to unix socket
# SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost/"

# Else we can just use a tcp socket:
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>


and this in my httpd.conf

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/mysite.com/$1

this works, must I add a new line to my httpd.conf for each vhost?
 
Old 02-27-2017, 02:39 PM   #7
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
Well ideally you should have each site having a separate PHP-FPM pool per vhost with permissions segregating the sites out for PHP-FPM... but you could put it down as 'server config' and then it should apply to all vhosts automatically I believe: https://httpd.apache.org/docs/2.4/mo...tml#sethandler
 
Old 02-28-2017, 12:42 AM   #8
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by irish2 View Post
I am now using this in my vhost

<FilesMatch \.php$>
# 2.4.10+ can proxy to unix socket
# SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost/"

# Else we can just use a tcp socket:
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>


and this in my httpd.conf

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/mysite.com/$1

this works, must I add a new line to my httpd.conf for each vhost?
No, if you add it in the global httpd.conf i't available everywhere, so you don't need to add it in ever vhost configuration too.
BTW you could use a third party repo in order to install php-5.6 as a DSO for apache (e.g. https://webtatic.com/packages/php56/)
 
Old 02-28-2017, 03:23 AM   #9
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Originally Posted by bathory View Post
No, if you add it in the global httpd.conf i't available everywhere, so you don't need to add it in ever vhost configuration too.
BTW you could use a third party repo in order to install php-5.6 as a DSO for apache (e.g. https://webtatic.com/packages/php56/)
mod_php/DSO is horrible tho and should be avoided. It causes apache to consume memory and limits you to MPM-prefork or MPM-ITK, both of which have the performance of a drunken slug. PHP-FPM actually gives a significant performance improvement and doesn't bloat the size of every apache thread to elephant like proportions.

Last edited by r3sistance; 02-28-2017 at 03:26 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Apache Virtual Hosts aren't working pjk1939 Linux - Server 4 07-26-2013 08:31 AM
PHP on some Apache Virtual Hosts Palula Linux - Server 2 06-15-2009 11:09 AM
Turn off (disable) PHP for some virtual hosts in Apache DaveVT5 Linux - Networking 2 03-02-2006 03:51 PM
PHP include() file paths in Apache virtual hosts tawalker Linux - Software 0 09-23-2004 02:16 PM
Apache 2 Suexec PHP and Virtual Hosts codedv Linux - Software 2 02-26-2004 04:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration