PHP.ini

From DreamHost
(Redirected from Custom PHP.ini)
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 performed by DreamHost may break your modifications. You, as the user, are responsible for troubleshooting and resolving issues with your customizations.

Overview

A php.ini file is a simple text file that allows you to adjust your PHP settings. For example, the server comes with a few default settings such as a PHP memory_limit of 90MB. Other limits that are often changed are for PHP execution time and upload size. This wiki walks you through how to set up a php.ini (phprc) file and add your custom adjustments.

Note2 icon.png Note: The standard term for this file is php.ini. However, as of PHP 5.3, DreamHost uses a "phprc" file instead. This acts the same way – only the name is different. In this wiki, we’ll use the term phprc.


Determine which PHP Version your website uses

Before you create a phprc file, you’ll need to know which version of PHP your site is running. The PHP version for each site is displayed on the (Panel > ‘Domains’ > ‘Manage Domains’) page.

You’ll see the version assigned to any domain to the right under the "Web Hosting" column:
01 phpini PHP Version.fw.png
Important icon.png Important: Please be aware that DreamHost recommends you use PHP version 5.5. You can find more info about PHP here:

DreamHost pre-installs the following versions of PHP on its servers:

  • PHP 5.6 - The latest version of PHP that DreamHost has to offer. Allows for settings to be customized in the same way as PHP 5.3 through 5.5.
  • PHP 5.5 - A widely supported version of PHP.
  • PHP 5.4 - DreamHost’s default PHP version. Also a widely supported version of PHP.
  • PHP 5.3 - Officially End-Of-Lifed by php.net as of August 14th, 2014. A widely supported version of PHP which introduces a new and easy way to customize the settings (e.g., Runtime Configuration, phprc, and so on).
  • PHP 5.2 or older versions No longer supported and installed on the shared hosting platform. VPS and Dedicated hosting accounts not utilizing an “admin user” will lose PHP 5.2 - support starting February 16, 2015.

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 may even notice error messages or deprecated warnings on your site if the code is out of date.

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 has been End-Of-Lifed (EOL’ed) which means the product is no longer getting security updates from the developers and php.net,
  • PHP 5.4 will be EOL'ed this year (September 2015),
  • PHP 5.5 is well tested, stable, and has been in production on sites around the world since June 2013.


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

Important icon.png Important: As a reminder, PHP 5.2 has been EOL'ed by php.net and is no longer supported by DreamHost for Shared customers as of November 3, 2014 and for VPS and Dedicated customers starting February 16, 2015 (not including admin customers)


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:

  1. Check your current PHP version in the panel as described above.
  2. If you're running an older version, upgrade to at least PHP 5.3.
  3. Log into your site via FTP.
    • You can read more in the FTP wiki.
  4. Edit your .htaccess file.
    • Comment out any custom PHP references to ensure that your site loads without issue.
    • For example, add a # symbol at the beginning of any line referencing "php.ini" or "dispatch".
    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 in the sections that follow.

How to add a phprc file

You can create a phprc file through an FTP client, or by logging into the server via SSH. Since creating a phprc file is simpler through an FTP client, these steps are covered first.

Adding a phprc file through Filezilla

  1. Log into your web server via FTP. Visit our wiki for detailed instructions:
  2. Make sure you’re in your users directory.
    You can see this in Filezilla in the ‘Remote site:’ field. It will say "/home/username".
    02 phpini Filezilla Create php folder.fw.png
  3. In the users directory, right-click your mouse and select Create directory.
    03 phpini Filezilla name php folder.fw.png
  4. Name this directory .php. Make sure you add the period at the beginning of this folder name or it will not work. Visit the following wiki on how to enable hidden files if you can’t see this:
    04 phpini Filezilla create php55 folder.fw.png
  5. Click into this new .php directory, and then create another directory.
    05 phpini Filezilla name php55 folder.fw.png
  6. Create a folder named ‘5.5’.
    The name of this directory is the version of PHP you’re using. In this example, we’ll say our site is running PHP 5.5.
    06 phpini Filezilla create phprc file.fw.png
  7. Click into this 5.5 folder, and then right-click and choose Create new file.
    07 phpini Filezilla name phprc file.fw.png
  8. Name the new file 'phprc'. DO NOT add a file extension, as the phprc file should never have an extension.
    08 phpini Filezilla edit phprc file.fw.png
  9. Right-click on the phprc file, and then choose View/Edit.
    A default text editor opens for you to add your customizations. These examples are very general – adjust as needed for your site.
    09 phpini Filezilla add content to phprc.fw.png
    When you try to save this file in Filezilla, the following popup box opens.
    10 phpini Filezilla save content to phprc.fw.png
  10. Check the box which says ‘Finish editing and delete local file’, and then click the Yes button to save.

The phprc file is created on your web server for any domains under this specific username using this specific version of PHP.

Adding a phprc file via SSH

  1. Log into your server via SSH. Visit our wiki for instructions on how to do this based on which operating system you’re using:
  2. Make sure you’re in your users directory by typing in ‘pwd’ to confirm:
    [horsetail]$ pwd
    
    /home/dhwikiuser
    
    Note2 icon.png Note: In this directory, you can run a single command to create the /.php folder as well as the /5.5 folder that goes inside of it. Remember to first check which version of PHP your site is using. In the following example, we’ll assume the site is using PHP 5.5.


  3. Run the following command:
    mkdir -p ~/.php/5.5
    
  4. Change your directory into these two new folders you just created, by running the following:
    cd .php/5.5
    
    When you run pwd, you now see the entire filepath you just created:
    [horsetail]$ pwd
    
    /home/dhwikiuser/.php/5.5
    
  5. In this folder, create the phprc file without an extension.
    Note2 icon.png Note: There are a few programs you can run to create this file, and in this example we’ll use a program called nano.


  6. Run the following to create the phprc file.
    nano phprc
    
    A text editor pops up.
  7. In the editor, enter all the PHP customizations.
  8. When finished, press CTRL + X on your keyboard.
    You are prompted to save the file.
  9. Type in the letter y to confirm, and then click the Enter button to save.

The phprc file is created on your web server for any domains under this specific username using this specific version of PHP.

If you don't see the changes made to your phprc file

It’s possible that even if you’ve correctly added the phprc file, you may not notice its customizations resolving online.

There are two suggestions to get this file updated:

  • Update your phprc file via SSH
  1. Log into your server via SSH.
  2. Once you log in, run the following command based on your version of PHP to kill off all running PHP processes:
    For PHP 5.3:
    killall -9 php53.cgi

    For PHP 5.4:

    killall -9 php54.cgi

    For PHP 5.5:

    killall -9 php55.cgi

    For PHP 5.6:

    killall -9 php56.cgi

    Killing off your PHP processes in this way updates your phprc file.

  • Update your phprc file in your panel

You can also update your phprc file by saving your domain configuration in your panel.

  1. Navigate to (Panel > 'Domains' > 'Manage Domains').
  2. Click the 'DNS' link under your domain name.
  3. In the first section, click the blue button titled Change settings.
    This will update your phprc file within 10 minutes.

Customization Options

The examples above explain how to add a phprc file with some basic contents. This section goes a little further into what you can add to this file.

Note2 icon.png 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

By default, DreamHost does not log PHP errors. However, you can enable PHP error logging through your phprc file. 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 phprc file:
     log_errors = 1
     error_log = /home/USERNAME/php.log
    
  2. Replace USERNAME with your SHELL user.

Increasing the Filesize Upload Limit

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

To increase that limit to 100MB, use the following directives:

upload_max_filesize = 100M
post_max_size = 105M
max_execution_time = 500
max_input_time = 500
Note2 icon.png Note: To upload large files, post_max_size should be larger than upload_max_filesize.


Increasing the PHP Memory Limit

Note2 icon.png Note: 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 add the following:

memory_limit = 128M

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

PHP Loaders are extensions to PHP which load special types of PHP files. You must modify your phprc file 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 Extensions

Depending on which version of PHP you choose to use, you may notice certain extensions are not enabled by default (for example, phar).

To load an extension, add the following line your phprc file:

extension = phar.so

Compiling PHP extensions

For steps on how to compile and load your own extensions, please visit the link below:

Adding global custom changes (VPS or Dedicated ONLY)

If you have a VPS or Dedicated server it’s also possible to set a single php.ini file for the entire server, rather than set them up individually as explained above.

The following steps walk you through how to enable this.

Note2 icon.png Note: If you still have a custom phprc file setup under your user, this will override the global php.ini file for that specific user for the version of PHP for which the custom phprc file was created.


  1. Log into your server using your admin user. You can create an admin user at (Panel > ‘VPS’ > ‘Admin Users’).
    Lightbulb wiki.png Tip: Visit the SSH wiki for information on how to log into your server based on the operating system you’re using:


  2. Once you log into your server, navigate to the directory for the version of PHP you wish to edit:
    For PHP 5.3
    /etc/php53
    For PHP 5.4
    /etc/php54
    For PHP 5.5
    /etc/php55
    For PHP 5.6
    /etc/php56
    Once you are in that directory, you’ll see the php.ini file.
  3. Run the following command to edit it:
    sudo nano php.ini
    
  4. Make any adjustments and save.
  5. Kill off any PHP processes to update the file.

Once you finish, the global php.ini is now used for all sites on your server which do not have a custom phprc file enabled.

Note2 icon.png Note: If your server is running NGINX, you must stop NGINX before killing PHP, then restart it.


To stop/kill/restart NGINX:

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

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

On a DreamHost VPS or Dedicated server, you can find the directory at /etc/php54/conf.d/ (just make sure to change the php# folder to the actual version you’re updating). Any files in this directory are loaded by all PHP interpreters for this version of PHP.

Nginx

All custom phprc files can be created with the instructions above for your NGINX machine. However, if you need to update your changes, run the following command under your admin user:

sudo /etc/init.d/nginx restart
Note2 icon.png Note: If you receive a 504 timeout on your website, you must stop nginx, kill PHP, and then finally restart nginx. Run the following commands under your admin user to update your settings:
sudo /etc/init.d/nginx stop
pkill -9 php
sudo /etc/init.d/nginx start


Troubleshooting

This section will help to resolve common errors you may encounter on your PHP website.

Turning off PHP errors and warnings

If you upgrade the PHP version on your site, you may suddenly see various error messages. DreamHost always recommends that you check each message, but if you’re sure they are not causing any issues with the functionality of your site then you can remove them.

To remove the error messages, add these lines to your phprc file.

display_errors = off
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED

Cannot modify header information

You may notice the following error either when visiting your site or logging in to it:

Warning: Cannot modify header information - headers already sent by (output started at...'''

This is essentially an issue with how the site is coded. Most often, this simply means something was sent to the browser before the header() was called. Technically speaking, once the header block has already been sent, you cannot add

additional header lines. Most often this is caused by your code sending output to the browser before the header() function. Make sure your header() function is set in your code before any output.


If you do not want to make any adjustments to your code, you can add the following in your phprc file to resolve this.

output_buffering = 4096

See also