PHP.ini

From DreamHost
Revision as of 18:14, 17 April 2012 by Edow (Talk | contribs)

Jump to: navigation, search
The instructions provided in this article or section are considered advanced.

You are expected to be knowledgeable in the UNIX shell.
Support for these instructions is not available from DreamHost tech support.
Server changes may cause this to break. Be prepared to troubleshoot this yourself if this happens.
We seriously aren't kidding about this.

Below are instructions for using your own modified version of php.ini on a domain called "example.com".

PHP 5.3

PHP 5.3 supports several new configuration hooks which make custom php.ini configuration super-easy! DreamHost recommends that all users needing custom PHP setup use PHP 5.3 or later, as the configuration changes required to set it up are much simpler than for PHP 5.2.

  1. Log into the panel at https://panel.dreamhost.com/index.cgi?tree=domain.manage& and change your domain to use php5.3 (probably fast cgi)
  2. Create a directory under your user called .php, with a subdirectory called 5.3. Files under this directory will be used by all domains under that user which are set to use PHP 5.3.

Example for the terminal

mkdir -p ~/.php/5.3
  1. Once this directory exists, there are two files you can create in it:
  • To add custom directives to PHP, create a file under .php/5.3/ called "phprc" and add configuration directives to it. You do not need to copy the default php.ini to this file, as it is used in addition to the system php.ini; if a directive appears in both files, the one in this file will take precedence.
cd ~/.php/5.3
nano phprc

and then type in the commands you need.

  • Sample directive which turns off error reporting level Notice and Deprecated
error_reporting = E_ALL & ~E_DEPRECATED & ~E_NOTICE

Replace the system php.ini entirely

  1. To replace the system php.ini entirely, create a file under .php/5.3/ called "phpini". If this file exists, PHP will not read the system php.ini at all, so you should probably start by copying /etc/php53/php.ini here as a starting point. In most cases, this file is not necessary — please use a phprc if at all possible.
  2. You may need to force PHP to reload its configuration file for changes to take effect. You can do this by running "killall php53.cgi" from a shell, or by waiting several minutes.

Turning on PHP Error Logging

Debugging PHP is much easier when you have a log of all the errors.

Add these options to your custom php.ini file:

log_errors = 1
error_log = /home/username/php.log

Loading PHP 5.3 extensions

Our build of PHP 5.3 contains a number of additional extensions which are not loaded by default (for instance: soap). You can load them by simply adding the following line to a file at /home/username/.php/5.3/phprc:

extension = soap.so

if the path/file for phprc are not yet there, create them. Might need to restart server after changes.

It's that easy!

Loading PHP 5.3 extensions on all domains (on VPS or dedicated)

On a DreamHost VPS or dedicated server, you can also create the directory /etc/php53/conf.d/. Any files in this directory will be loaded by all PHP 5.3 interpreters in addition to the default file, /etc/php53/php.ini.

  1. Create a root/admin user and login.
  2. Upload your file to your FTP server.
  3. Through shell, copy the files into the /etc/php53/conf.d/ directory:
sudo cp filename /etc/php53/conf.d/
Machine will ask for your admin user password.

Compiling PHP 5.3 extensions

You can also compile and load your own extensions. Here's how:

  1. Download and unpack the extension (from PECL, for instance).
  2. If the extension is already compiled (most binary PHP loaders will be, for instance), skip to step 6.
  3. /usr/local/php53/bin/phpize
  4. ./configure --with-php-config=/usr/local/php53/bin/php-config
  5. make
  6. Copy the module to your .php/5.3/ directory.
  7. Assuming your user is called "username" and your module is named "mymodule.so", add the following to your .php/5.3/phprc:
    extension = /home/username/.php/5.3/mymodule.so
  8. That's all there is to it! Your new extension is now enabled, and will be loaded by all domains using PHP 5.3 under that user.

Passing basic browser credentials to your PHP 5.3 script

I've had a few glitches with my configuration, but it appears that maybe when using CGI mode, the following .htaccess file doesn't seem to be working without tweaking the phprc file.

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{HTTP:Authorization} ^(.*)
  RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php [L]
</IfModule>

In the end, it just seemed easier to pass the credentials using a variable.

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{HTTP:Authorization} ^(.*)
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?__HTTP_AUTH=%{HTTP:Authorization} [L]

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php [L]
</IfModule>

Obviously, be careful that you are properly sanitizing this before relying on it, because otherwise your attacker may write an alternative value into there using http://www.example.com/somefile/?__HTTP_AUTH=blahblah

PHP 5.2

PHP 5.2 is considerably harder to configure than PHP 5.3. DreamHost recommends that all users needing custom PHP setup use PHP 5.3 or later, as the configuration changes required to set it up are much simpler than for PHP 5.2.

SXi's Easy Auto Copy Script

Notice icon 100px.png
Warning: This script has not been tested by the Dreamhost team and may pose a possible risk, please keep this in mind before you run this script.

Simply run the php script to override your php.ini. No hassle of messing with SSH or editing htaccess files. | DreamHost Custom PHP.INI auto-installer

Set up your domain for FastCGI

These instructions work for "PHP 5 FastCGI" selection at domain management page. If you face with seeing only php-wrapper.ini contents, thats because you didn't choose this option.

Note: Before you start, I suggest you copy out ALL the commands that you will need to a text editor and do the changes before you enter them via SSH.

Custom php.ini for a Single domain

Create a cgi-bin folder for your domain

mkdir $HOME/example.com/cgi-bin

Copy the default php.ini file

cp /etc/php5/cgi/php.ini $HOME/example.com/cgi-bin/php.ini

Make modifications to this file as necessary. Note that some variables (in particular, post_max_size and upload_max_filesize) are subject to internal limitations; increasing them significantly beyond the default values will not work correctly.

Create the script wrapper

cat << EOF > $HOME/example.com/cgi-bin/php-wrapper.fcgi
#!/bin/sh
exec /dh/cgi-system/php5.cgi \$*
EOF
Alternate method

Since many seem to have a problem with this step, here is an alternate way to Create the script wrapper: Just create a text file in the cgi-bin directory that you created above (/home/yourusername/example.com/cgi-bin) named php-wrapper.fcgi, containing the following lines:

#!/bin/sh
exec /dh/cgi-system/php5.cgi $*

You can do this with any linux text editor (nano, vi, vim, etc.) in the shell. You can also create this file using your own computer and a *text editor* (not a word processor). Make sure your editor can save the file with unix-style line endings (otherwise the #!/bin/sh line will cause an error!), and then upload it to the /home/yourusername/example.com/cgi-bin directory.

Lazy method
Notice icon 100px.png
Warning: This script is from the same site as "SXi's Easy Auto Copy Script" above, and so should carry the same disclaimer. It also may be confusing to include this script here, in the middle of a different set of instructions.

Install the dh-phpini.php into domain root.

  • Installation runtime ~ 1 sec.
  • Works on x86, x86-64, Sarge, Etch, and VPS servers.
  • Your editable php.ini will be created in the domain's cgi-bin folder.

Edit your own php.ini on DreamHost.

Set permissions

chmod 755 $HOME/example.com/cgi-bin
chmod 755 $HOME/example.com/cgi-bin/php-wrapper.fcgi
chmod 640 $HOME/example.com/cgi-bin/php.ini

Set up the .htaccess file

The .htaccess file is located in your document root at $HOME/example.com/.htaccess. If you do not yet have one, you can create a text file there and name it .htaccess.

Add this to run .php files with your custom PHP wrapper:

Options +ExecCGI
AddHandler php5-cgi .php
Action php-cgi /cgi-bin/php-wrapper.fcgi
Action php5-cgi /cgi-bin/php-wrapper.fcgi

Please note that as with the text file you created for the script wrapper, the text editor you use to create or edit your .htaccess file should support unix-style line endings -- otherwise you may get an Internal Server Error when you visit your site.

You can now modify $HOME/example.com/cgi-bin/php.ini to your heart's content!

Done everything and it still doesn't work?

If you've done everything but you just can't get PHP to load the custom php.ini file you may want to edit your php-wrapper.fcgi (in $HOME/example.com/cgi-bin) as follows.

Add the following after the first line:

export PHPRC=/home/yourusername/example.com/cgi-bin

Result:

#!/bin/sh
export PHPRC=/home/yourusername/example.com/cgi-bin
exec /dh/cgi-system/php5.cgi $*

Done that and it STILL doesn't work?

Go into the Dreamhost admin panel and ensure that your domain has "FastCGI support?" ticked. May sound obvious but had me puzzled for half an hour!

If all else fails...

remove everything from your cgi-bin folder, and revert all the changes you made to your .htacces file and...

Copy the ini file:

cp /etc/php5/cgi/php.ini $HOME/example.com/cgi-bin/php.ini

Copy the cgi file:

cp /dh/cgi-system/php5.cgi $HOME/example.com/cgi-bin/php5.cgi

Add the following to the top of your .htaccess file:

Options +ExecCGI
AddHandler php-cgi .php
Action php-cgi /cgi-bin/php5.cgi

<FilesMatch "^php5?\.(ini|cgi)$">
Order Deny,Allow
Deny from All
Allow from env=REDIRECT_STATUS
</FilesMatch>

Custom php.ini across Multiple domains

The steps below are essentially the same as above but only create one cgi-bin folder that is shared across multiple domains.

Create a cgi-bin folder in your Home directory

 mkdir $HOME/cgi-bin

Copy the default php.ini file

 cp /etc/php5/cgi/php.ini $HOME/cgi-bin/php.ini

Make modifications to this file as necessary. Note that some variables (in particular, post_max_size and upload_max_filesize) are subject to internal limitations; increasing them significantly beyond the default values will not work correctly.

Create the script wrapper

Create a text file in the cgi-bin directory that you created above (/home/yourusername/cgi-bin) named php-wrapper.fcgi, containing the following lines:

 #!/bin/sh
 exec /dh/cgi-system/php5.cgi $*

You can do this with any linux text editor (nano, vi, vim, etc.) in the shell. You can also create this file using your own computer and a *text editor* (not a word processor). Make sure your editor can save the file with unix-style line endings, and then upload it to the /home/yourusername/cgi-bin directory.

Set permissions

 
 chmod 755 $HOME/cgi-bin
 chmod 755 $HOME/cgi-bin/php-wrapper.fcgi
 chmod 640 $HOME/cgi-bin/php.ini

Repeating Steps

These two (2) steps must be done for, and within each domain or sub-domain you wish to leverage the custom php.ini. Any domains that do not use these steps will remain on DreamHost's global php.ini.

Create a Soft Link to your central cgi-bin
ln -s /home/USERNAME/cgi-bin/ /home/USERNAME/webroot/NEW.DOMAIN.com/cgi-bin

I use a webroot folder to coral all of my domains, in your case it might instead be;

ln -s /home/USERNAME/cgi-bin/ /home/USERNAME/NEW.DOMAIN.com/cgi-bin
Set up the .htaccess file

The .htaccess file is located in your document root at $HOME/example.com/.htaccess.

Add this to run .php files with your custom PHP wrapper:

 
 Options +ExecCGI
 AddHandler php5-cgi .php
 Action php-cgi /cgi-bin/php-wrapper.fcgi
 Action php5-cgi /cgi-bin/php-wrapper.fcgi

You can now modify $HOME/cgi-bin/php.ini to your heart's content!


Custom PHP.ini for nginx servers

So you are using super fast nginx on your PS server ay? Smart lad (or lass). But if you want to tweak your php settings, or xcache settings on nginx, then read on!

Custom Global php.ini on Nginx

To completely replace DH default php config file for all yer domains *under a particular user* just create a file .php.ini in your home.

/home/madamimadam/.php-ini

(if you compiled a custom binary you can use .php-launcher to specify that as well - unlikely since DH is on php5 now. )

I recommend you start by copying the configuration in use and just change what wasn't meeting your needs.

 cp /etc/php5/cgi/php.ini ~/.php-ini 

Custom php.ini per domain on Nginx

now because nginx runs an instance of PHP for each user, you can't specify php settings at a domain level.

BUT!

Because each user gets their own instance - you can create specific configurations for specific domains or groups of domain by migrating those sites under a different user.

madamimadam
--> www.domain1.com
--> www.domain2.com
hannah
--> www.domain3.com
--> www.dmoain4.com

Now admittedly this will make maintenance a bit more segregated, but you can still use a single user for all your cron jobs and backup operations etc. So the new users are nothing more that "groupings" for configuration specific domains. Also there will be 2 instances of PHP running instead of 1, so your memory consumption will rise.

Thanks Andrew F!

Reload Nginx

we can know about the new files by reloading Nginx web server (requires admin / sudo user)

sudo /etc/init.d/nginx restart

NOTE: This restart gooeyed up my php - support resolved the 504 timeouts by killing php and restarting it.

Modified existing php.ini but not seeing the changes?

See the FastCGI page for instructions on how to either restart FastCGI or force a recache.

Restarting FastCGI from the shell:

killall -9 php5.cgi

Or, for PHP 5.3:

killall -9 php53.cgi

If you are using PHP 5.2 with a PHP wrapper, the following may also be necessary:

touch $HOME/example.com/cgi-bin/php-wrapper.fcgi

Apache mod_rewrite

For those who are getting an error while uses mod_rewrite on your websites, you must add the follow line to your .htaccess: After "RewriteEngine on"

RewriteCond %{REQUEST_URI} !^/cgi-bin/.*

See also

External links