PHP.ini

From DreamHost
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".

Determine which PHP Version your website uses

There are different methods to customize PHP, based upon which version you use:

  • PHP 5.4 - The newest version, has an all-new and easy way to customize the settings (e.g., Runtime Configuration, phprc, and so on).
  • PHP 5.3 - A very common version, has an all-new and easy way to customize the settings (e.g., Runtime Configuration, phprc, and so on).
  • PHP 5.2 or older versions - Customizations not supported. Upgrade to PHP 5.3 or later.

Your website may require a specific PHP version. Some PHP modules exist for 5.2 but not 5.3, such as Zend Optimizer. Some plugins or other parts of your website application may only work with a specific PHP version.

You can check which version of PHP your website is using from your DreamHost panel (on the Manage Domains page):

  1. Click Edit to view the hosting settings and check the PHP mode setting.
  2. Set it to PHP 5.4 (FastCGI) if you can, or 5.3 if you need to use the older version.
Note2 icon.png Note: You should be aware of the following:
  • PHP.net is no longer releasing security updates for version 5.2,
  • PHP 5.3 will be unsupported by php.net by the middle of 2014, and most importantly...
  • PHP 5.4 is well tested, stable, and has been in production on sites around the world since March 2012.


Upgrading your site (if you're moving to Ubuntu from Debian servers)

If your site was recently moved to Ubuntu (which requires PHP 5.3), you may need to make some changes especially if you set up a custom PHP.ini file, manually edited your .htaccess file, or used a script to force PHP 5.2 on your site

Important icon.png Important: As a reminder, PHP 5.2 has been EOLed by php.net and will no longer be supported by DreamHost as of November 3, 2014.


  1. Check your current PHP version.
  2. If you're running an older version, remove it and see the phprc section below to set up a new one.
  3. Comment out any custom PHP references on your site's .htaccess files to ensure that your site loads without issue, by using a # symbol for anything referencing "php.ini" or "dispatch" at the beginning of the line.
    The scripts look like this:
    AddHandler fastcgi-script fcg fcgi fpl
    AddHandler php-fastcgi .php
    Action php-fastcgi /cgi-bin/dispatch.fcgi
    
    or
    AddHandler php-cgi .php
    Action php-cgi /cgi-bin/dispatch.cgi
    

If you'd like to add back any custom PHP settings, you may do so by following the steps outlined here.

Adding global custom changes (VPS or Dedicated ONLY)

If you have:

  • a VPS or dedicated server and want to customize php.ini for the entire server, and
  • you don't want to manage PHP yourself

Then you can add any values you'd like to customize to the following file (similar to the phprc method below) using your admin user and sudo.

  • For PHP 5.4 changes, make/edit the file:
 /etc/php54/php.ini
  • For PHP 5.3 changes, make/edit the file:
/etc/php53/php.ini
  • For PHP 5.2 changes, make/edit the file:
/etc/php5/cgi/php.ini
NOTE: (8/6/13 - If you're running in mod_php mode, you should make/edit the file /etc/php5/php.ini)

Once you've made/edited that file, simply end all active PHP processes, and it should work.

Note2 icon.png Note: If you're on NGINX, you must stop NGINX before killing PHP, then restart it.


sudo /etc/init.d/nginx stop
pkill -9 php
sudo /etc/init.d/nginx start

PHP 5.3 / 5.4

phprc

Setting up a phprc file that is used by all the sites hosted under a user is easy to do.

To setup a phprc file, start by creating the folders for the custom settings file using the shell or an FTP program.

  1. Create a new folder named .php in your user's home directory /home/USERNAME/.
    Note: This folder name starts with a '.' or dot, and if you're using an FTP program you may need to turn on "view hidden files and folders" to see it after you create it (for more information about that check out FTP#How do I show hidden files?.
  2. Inside that create another folder based on what version of PHP you're using (either 5.3 or 5.4).
    The shell command for 5.3 is:
    mkdir -p ~/.php/5.3
    The shell command for 5.4 is:
    mkdir -p ~/.php/5.4
  3. Inside all those folders, /home/USERNAME/.php/PHP_VERSION_NUM/, create a new text file named phprc.
    Note: Make sure that's the exact name, with no file extension. This phprc file is where you will put all your custom PHP settings.

phprc and php.ini

Anything set in both phprc and php.ini:

  • uses the phprc values, while
  • everything else uses the values set in php.ini.

It's an easy way to change what you want and ignore the rest.

You can also:

  • use different phprcs for each domain.
  • use a phprc to test out new configurations and eventually move them to php.ini.
  • edit php.ini directly.

Replacing the system php.ini entirely

To replace the system php.ini entirely:

  1. Create a file under .php/5.3/ (or .php/5.4/) named php.ini (make sure you know which version of PHP you're working with).
    Note: 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 or /etc/php54/php.ini as a starting point. In most cases, this file is not necessary. Please use a phprc if at all possible.

Force PHP to reload

If needed, force PHP to reload its configuration file for changes to take effect using the following shell code, or by waiting several minutes.

killall php53.cgi
or
killall php54.cgi

Custom php.ini for nginx servers

The following are instructions and tips to tweak your php settings or xcache settings on nginx.

Custom Global php.ini commands on Nginx

  • To completely replace DH default php config file for all your domains under a particular user, create a file .php.ini in your home:
    /home/madamimadam/.php-ini
    Note: If you compiled a custom binary, you can use .php-launcher to specify that as well, though unlikely since DH is on PHP 5 now.
  • To change what wasn't meeting your needs, we recommend you start by copying the configuration in use :
     cp /etc/php5/cgi/php.ini ~/.php-ini 

Custom php.ini commands per domain on Nginx

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

However, because each user gets their own instance, you can create specific configurations for specific domains or groups of domains by migrating those sites under a different user:

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

Notes:

  • This makes maintenance a bit more segregated, but you can still use a single user for all your cron jobs, backup operations, and so on. The new users are nothing more than "groupings" for configuration specific domains.
  • There are now two instances of PHP running instead of one, so your memory consumption increases.

Reloading Nginx

You can reload then Nginx web server to view new files (requires admin / sudo user):

sudo /etc/init.d/nginx restart

Note: If you receive a 504 timeout, then kill and restart PHP.

I modified the existing php.ini/phprc, but I'm not seeing the changes?

Here a few suggestions:

  • See the FastCGI page for instructions on how to either restart FastCGI or force a recache.
  • To restart PHP 5.2 FastCGI from the shell:
    killall -9 php5.cgi
    For PHP 5.3:
    killall -9 php53.cgi
    For PHP 5.4:
    killall -9 php54.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

How to Customize your Settings

Now that you've got a custom php.ini (or phprc), you can make some suggested changes to customize how things run.

Note: Some variables (in particular, memory_limit, post_max_size and upload_max_filesize) are subject to internal limitations; increasing them significantly beyond the default values will not work correctly and will cause issues with your site.

Error Logging

Enabling PHP error logging is a simple step that allows easier diagnosis of problems with your website:

To enable:

  1. Add these lines to your php.ini or phprc:
     log_errors = 1
     error_log = /home/USERNAME/php.log
    
  2. Replace USERNAME with your server user.
  3. You may need to force PHP to reload its configuration file for changes to take effect using the following shell code, or by waiting several minutes.
    killall php53.cgi
    or
    killall php54.cgi

Increasing the Filesize Upload Limit

The default size limit for uploading files is 7MB, which may be far too small for your needs.

To increase that limit to 64MB, use the following command:

upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 500
max_input_time = 500

Increasing the PHP Memory Limit

If you find you're increasing the memory a large amount, it's time to consider moving off of shared hosting, as your website has greater needs than shared hosting can reliably provide.


The default memory limit is 90M and this is usually more than sufficient for most needs.

If you wish to increase this limit, you'll want to use the following:

memory_limit = 128M

Using PHP Loaders (Zend Optimizer, IonCube Loader, and so on)

PHP Loaders are extensions to PHP to load special types of PHP files. You must modify your php.ini or phprc to activate the loader required by your special PHP files.

Below are links to pages that provide further instructions on installing and using the various PHP loaders:

Loading PHP 5.3 Extensions

The current DreamHost build of PHP 5.3 contains a number of additional extensions which are not loaded by default (for example, soap).

  1. To load, add the following line to a file at /home/username/.php/5.3/phprc:
    extension = soap.so
  2. Create the path/file for phprc if not present.
  3. Restart the server when finished.

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 are loaded by all PHP 5.3 interpreters in addition to the default file, /etc/php53/php.ini.

  1. Create a root/admin user and log in.
  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/
  4. When prompted by the machine, enter your admin user password.

Compiling PHP 5.3 extensions

To compile and load your own extensions:

  1. Download and unpack the extension (from PECL, for example).
  2. If the extension is already compiled (most binary PHP loaders are already compiled), skip to step 4.
  3. Enter the following commands:
    /usr/local/php53/bin/phpize
    ./configure --with-php-config=/usr/local/php53/bin/php-config
    make
  4. Copy the module to your .php/5.3/ directory.
  5. 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

Your new extension is now enabled, and is loaded by all domains using PHP 5.3 under that user.

External links