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

Contents

Determine which PHP Version your website uses

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

  • PHP 5.3 - The newest 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 - A lot harder to customize, no longer being developed, and eventually will no longer be supported at DreamHost.

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.3 (FastCGI) if you can, or 5.2 if you need to use the older version.

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/conf.d/php.local.ini
  • For PHP 5.3 changes, make/edit the file:
/etc/php53/conf.d/php.local.ini
  • For PHP 5.2 changes, make/edit the file:
/etc/php5/cgi/ini.d/php.local.ini

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

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.
  2. Inside that create another folder named 5.3.
    The shell command for this is:
    mkdir -p ~/.php/5.3
  3. Inside all those folders, /home/username/.php/5.3/, create a new text file named phprc.

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/ named php.ini.
    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 as a starting point. In most cases, this file is not necessary. Please use a phprc if at all possible.
  2. 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

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.

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.

Run the php script to override your php.ini using the DreamHost Custom PHP.INI auto-installer. With SXi, you no longer need to hassle with SSH or edit htaccess files.

Set up your domain for FastCGI

These instructions work when you select PHP 5 FastCG on the domain management page. If you only see php-wrapper.ini contents, then PHP 5 FastCG is not selected.

Important: Before you begin, we highly recommend that you copy ALL the commands that are needed to a text editor and perform the changes before you enter them via SSH.

Custom php.ini commands for a Single domain

Creating a cgi-bin folder for your domain

mkdir $HOME/example.com/cgi-bin

Copying the default php.ini file

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

Note: Make modifications to this file as necessary. 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.

Creating the script wrapper

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

Alternate method to create the script wrapper

If you are having problems creating the script wrapper, here is an alternate way:

  1. 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 $*
    

    Note: You can do this with any linux text editor (nano, vi, vim, etc.) in the shell, or you can 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.

  2. Upload it to the /home/yourusername/example.com/cgi-bin directory.

DreamHost script from SXi (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.

Edit your own php.ini on DreamHost.

To use, install the dh-phpini.php into the domain root folder.

Notes:

  • 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.

Setting 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

Setting up the .htaccess file

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

If you do not see the file, perform the following to create:

  1. Create a text file in the root and name it .htaccess.
  2. Add the following 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/example.com/cgi-bin/php.ini as needed.

Note: As with the text file you created for the script wrapper, the text editor you used 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.

If you've done everything and it still doesn't work

The following are tips and suggestions to help you resolve persistent PHP.ini issues.

  • If you 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). 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 $*
    
  • Open the DreamHost admin panel and ensure that your domain has FastCGI support? enabled.
  • If all else fails:
    1. Remove everything from your cgi-bin folder.
    2. Revert all the changes you made to your .htacces file.
    3. Copy the ini file:
      cp /etc/php5/cgi/php.ini $HOME/example.com/cgi-bin/php.ini
    4. Copy the cgi file:
      cp /dh/cgi-system/php5.cgi $HOME/example.com/cgi-bin/php5.cgi
    5. 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 commands for Multiple domains

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

Creating a cgi-bin folder in your Home directory

 mkdir $HOME/cgi-bin

Copying the default php.ini file

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

Note: You can make modifications to this file as necessary. 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.

Creating the script wrapper

You can reate 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 $*

Note: 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.

Setting permissions

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

Leveraging the custom php.ini

You must perform these two steps within each domain or sub-domain you wish to leverage the custom php.ini.

Important: Any domains that do not use these steps will remain on DreamHost's global php.ini.

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

    Note: You can use a webroot folder to coral all of your domains, which might look like this:

    ln -s /home/USERNAME/cgi-bin/ /home/USERNAME/NEW.DOMAIN.com/cgi-bin
  2. Set up the .htaccess file by adding the following to run .php files with your custom PHP wrapper:
  3. Note: The .htaccess file is located in your document root at $HOME/example.com/.htaccess.

     
    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 as needed.

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, 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 Restart 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

Errors when using Apache mod_rewrite

If you're receiving an error while using mod_rewrite on your website(s), you must add the following line to your .htaccess (after "RewriteEngine on"):

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

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.

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.

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

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.

See also

External links

Personal tools