htaccess custom php.ini vs php_value or php_flag

machineaddict picture machineaddict · Jun 24, 2013 · Viewed 8.8k times · Source

I want to setup xdebug php extension to my project. I have a linux enviroment and there are others who work on the same server. I want to do this via custom php.ini file setup in .htaccess file. I have some questions about this:

  1. Will my custom php.ini file has to be a copy of the original php.ini file in order to modify only what I need? Or it will be enough to have in that file only the settings I want to change?
  2. As I have experimented, it is good to have the setting SetEnv PHPRC /path/to/my/custom/phpinifile above all other settings in .htaccess file. What about the manual settings php_value or php_flag below SetEnv? Will they overwrite my php.ini file settings?

Answer

Mehran picture Mehran · Jul 18, 2013

First of all you need to know there are different ways your webserver can be configured to make use of PHP. Personally I have experienced with mod_php and FastCGI so I can brief you on these two. These are called handlers and they affect hugely how you can configure PHP.

mod_php

If you've installed PHP as a module of Apache, the php.ini file is read when the Apache starts. In this case, all the major PHP directives like zend_extension and extension will be applied when Apache is starting thus they can not be set within a .htaccess file. In my experience the SetEnv PHPRC in the .htaccess does not work either.

FastCGI

In this case the php.ini file is loaded each time a request arrives. So if you change PHP's configuration, you don't need to restart Apache. Yet you can not set major PHP directives like zend_extension and extension in a .htaccess file and they need to be set in php.ini file. This is because when .htaccess is applied, the PHP extensions are already listed and loaded and changing those directives will have no effect (it will result in no error either). But the good news is that in this mode you can use SetEnv PHPRC in .htaccess file. So you can point to a custom php.ini file which will load xdebug indeed.

There's another way you can load user specific php.ini files too. But I haven't tested it yet so I can not comment either it's useful in your case or not. Perhaps you can take a look at it yourself. But it is mentioned in the page that it works only in CGI / FastCGI too.

Bottom line

If you are using mod_php, you've got no choice but to modify the original php.ini to load xdebug. But you can enable it per each virtual host putting enable commands in appropriate .htaccess files.

If you are using FastCGI, you can either load xdebug in original php.ini or you can have your own php.ini file and point to it in .htaccess file (yet in either case xdebug is loaded in a php.ini file). But if you're using the later case, your original php.ini will not be applied at all. So you need to mention whatever directive you want in you own version of php.ini file. Again you can turn on / off xdebug for each virtual host within .htaccess files.

Whichever solution you'll choose, you need to enter this in your php.ini file to load the xdebug:

zend_extension = /path/to/your/xdebug.so

And this one in your virtual host's .htaccess to enable debugging:

php_value xdebug.profiler_enable 1

[UPDATE]

How to check if your server is running PHP in mod_php or FastCGI:

Look for Server API in phpinfo's output. If it reads Apache 2.0 Handler it's mod_php while in FastCGI it will read CGI/FastCGI.