Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to set PHP configuration specific per virtualhost?

Status
Not open for further replies.

shadedecho

Programmer
Oct 4, 2002
336
US
Is there a way to either:

1. have a different php.ini for each virtualhost in apache, or

2. in the virtualhost configuration in apache, manually specify some different php environment/config variables?

What I am trying to do is be able to have EACH virtual host have it's own PHP error log, in their own directory, instead of all php errors going into one master php error log file.

I know I can set "error_log=..." in php.ini, but that sets a global value which applies to all php scripts, and I want to have each virtualhost set their own value for that.

I know each php script in each virtualhost's directory COULD set:

Code:
ini_set("error_log","/path/to/error.log");

HOWEVER, I do not want to force those virtual hosts to set that, I just want it to default to the correct location for each virtualhost, which I can control when setting up the virtual host in apache, for instance.

Any way to do this?
 
I don't think php can do this since it is running entirely in a cgi runtime environment.

Modperl could though since it actually interacts at all phases of the server setup and request. I dynamically configure about 500 sites at apache startup and it works well.

Another approach would be to have a script that you run when you add new sites that updates the httpd.conf or an included file.
 
PHP on my system is running as an apache module, and being loaded dynamically at runtime, not as a CGI program. You CAN install PHP as CGI, but I consider it less efficient and less secure, so I always go the route of having a built in Apache Module.

I just thought maybe there was some way to redirect PHP errors to a particular file PER VIRTUAL HOST (like the other options you specify in the VirtualHost directives)... and that configuration I can manually do when setting up a client in Apache, for instance.
 
Even as an apache module it RUNS IN CGI SPACE.

There are many many different phases of apache configuration, setup and runtime execution. Since php runs in the cgi space (even as a module) it can not modify how apache is fundamentally configured.

 
ok... but i don't want PHP to modify apache, i want the other way around... i want for an apache configuration (via .htaccess, or the httpd.conf file VirtualHost directives, or whatever) to override a global php.ini constant (error_log=...) so that each client has their own error log. Apache lets you specify each VirtualHost to have its own APACHE error and access logs, why then couldn't there be a way to have virtualhost specific PHP error logs?
 
Once again, ERRORLOGS are an APACHE VHOST directive and they are set at startup.

Since php can not access the startup phase of apache they can not be set by php.

The logging diretive are not available in the .htaccess (since htaccess is a vhost dependant item), only in the vhost conf and thus they are only configurable at apache startup. A phase of apache that php can not access.

 
I think I'm still failing to correctly identify what I want... do you agree that php has a directive in it's php.ini called "error_log"? Do you agree that THIS error_log is COMPLETELY separate from Apache's error log. The PHP error log ONLY HOLDS php errors, while the Apache error log just holds apache errors, such as "page not found" stuff.

SOOOOO.... I want for Apache's httpd.conf (or .htaccess) to be able to override PHP.ini's error_log setting... I do NOT want for PHP to be able to override Apache's setting.

Since Apache loads first, and THEN loads PHP, it seems reasonable that Apache could override PHP settings on a per virtual-host basis.
 
I would say 'No'. php.ini is a strange beast and as far as I know is read at apache startup as well, especially in a module setting.

Good luck though.

 
i'm posting another reply here, because i've found new information, but I still can't quite get what i need done. maybe this will spark some more discussion.

Apparently, you can use "php_value" directive inside of a <VirtualHost> configuration, to override certain php.ini settings on a per-host basis. Sounds exactly like what I wanted to do.

It works for some php values, like "auto_prepend_file" and such, but not apparently, or at least to my testing yet, for error_log, which is what I need it for. This seems very curious to me.

I also found a site who says they put the "php_value error_log" directive successfully in their .htaccess files to do per host config. So, why would it work in a .htaccess file and not in the apache config? Can anyone confirm, is this a bug in Apache's use of "php_value"?
 
On my LAMP (RH9/2.0.52/4.1.17/5.0.2) box, I have a test site, the Apache setup for which is:

Code:
<VirtualHost [removed]:80>
        DocumentRoot /home/sites/test/html
        ServerName [removed]
        ServerAlias [removed]
        ExpiresDefault now
        ExpiresActive on
        CustomLog /home/sites/test/logs/access.log combined
        ErrorLog /home/sites/test/logs/error.log

        <Directory /home/sites/test/html/val_test>
                php_value error_log "/home/sites/test/html/val_test/foo.txt"
        </Directory>
</VirtualHost>

A "phpinfo()" script in the main directory shows that error_log has no value. Running a "phpinfo()" script in the val_test subdirectory shows that the value for error_log is /home/sites/test/html/val_test/foo.txt


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
(FYI, I am using Apache 2.0.52, and PHP 5.0.3, on a Debian 3.x linux machine)

I appreciate the benefit of that post, I see that you put it into a "directory" sub-directive under your <virtualhost>.... I had just tried specifying the php_value directive right there in the main part of the virtualhost. I tried your way, to see if it would work.

It did not. then i decided to see if the phpinfo() would report the value i was setting in my apache conf, as you say yours does, and it did! so, now i'm really confused. the value is getting set, but it's not being respected by apache/php.

The errors ARE getting put into the APACHE error log file for that virtualhost, which is very strange to me. Here's the appropriate <virutalhost> directive:

Code:
<VirtualHost xxx.xxx.xxx.xxx>
        ServerAdmin webmaster@somedomain.net
        DocumentRoot /[URL unfurl="true"]www/somedomain_net[/URL]
        ServerName [URL unfurl="true"]www.somedomain.net[/URL]
        ErrorLog /home/someuser/logs/somedomain_net-error.log
        CustomLog /home/someuser/logs/somedomain_net-access.log common
        <Directory /[URL unfurl="true"]www/somedomain_net>[/URL]
                php_value error_log "/home/someuser/logs/somedomain_net-php-error.log"
        </Directory>
</VirtualHost>

Notice that I am changing what the PHP error log file name should be slightly from the apache error log file specified above it.

I should also mention that I AM able to correctly override, using php_value, other php.ini settings, like auto_prepend_file, and those settings ARE respected, as well as printed in phpinfo's output. But this one seems to get set in PHP but not respected by it when an error occurs. What gives?
 
Let me add, also, to this discussion, that, it appears there is something else greater wrong....

I also cannot change the value of the error_log IN a script, using ini_set(), and have it be respected. I change it, and print out phpinfo(), and then throw a php error by calling an undefined function...

phpinfo's output shows it has changed, even over what i specified in the httpd.conf, but... the error still doesn't get into the correct log file, it still goes into the apache error log file.

Is there some overridding thing that Apache is doing which is trapping ALL errors or something, by default? or is there some other reason why PHP would not be able to change its own error_log variable at run time?

I know i've changed it at run time before, on other servers and in other configurations. Maybe this is a php 5 bug?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top