If you find yourself in a situation that requires changes to your site, and your developer isn't available to help, DreamHost's skilled support team may be able to assist you for a small fee. You can find more information about supported services and associated costs in the DreamHost Professional Services article.
Overview
When PHP changes are made on your site (usually from upgrading the PHP version), you may find that PHP throws errors and warnings. In the past, these would display on your website and you could turn them off using a phprc file.
DreamHost now disables all PHP warnings by default. This means you should never see them on your website when making changes to PHP.
How can I display the warnings on my site?
To display the warnings on your site, you just need to add the following line to your phprc file:
display_errors = on
If any PHP errors are thrown, they will now display on your site.
This is NOT recommended on a live website since visitors will see the errors. The better option is to specify which types of errors you'd like to log and enable PHP error logging as described below.
Controlling what type of errors display
PHP allows you to specify which type of errors you would like displayed, or to save to your PHP error log file. For example:
PHP directive | Explanation |
---|---|
error_reporting = E_ALL | Logs all errors and warnings |
error_reporting = E_ERROR | Fatal errors that occur during PHP's initial startup. |
error_reporting = E_WARNING | Run-time warnings (non-fatal errors). Execution of the script is not halted. |
error_reporting = E_NOTICE | Logs runtime notices (usually bugs in your website code) |
A full list of options can be found here:
Additionally, you can exclude certain types of errors by using the bitwise operator ~. For example, this will show all errors except NOTICE and DEPRECATED warnings.
error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED
Setting error reporting in your script
As mentioned above, the phprc file controls which errors are displayed. As such, the following lines will not function in your PHP script to display errors:
ini_set('display_errors',1); error_reporting(E_ALL);
The only way to display errors is to edit your phprc file.
Configuring custom error logging
You can add code to your phprc file on the server to log all PHP errors in a new file named php.log.
Decide where the php.log file should go
You must first decide where you want your php.log file to be created. This can be in any directory you like as long as your user is able to write to that directory. The most simple location would be in your user's home directory. For example:
/home/username/php.log
It's not possible to create your php.log file in your /home/username/logs directory. This directory is created by DreamHost and owned by dhapache, therefore it's not possible for your user to write to this directory.
Editing the phprc file
Add these lines to your phprc file:
log_errors = 1
error_log = /home/username/php.log
Replace username with your SHELL user.
Kill off all running php processes
Make sure to kill running php processes to ensure your new settings take effect.
Confirming your changes
After you've edited the file and killed off all PHP processes, you should check to confirm the values have updated. You can do this by creating a phpinfo.php file.
If you do not see your changes have updated, try killing off your PHP processes again:
Testing the php.log file
To test the php.log file, create a PHP file that throws an error. For example, create a file named error.php with the following content and upload it to your site.
<?php $file=fopen("welcome.txt","r"); ?>
Visit the example.com/error.php page in a browser. An error is thrown. If you then check your user's home directory, you'll see the php.log file has been created and that it logged this PHP error. Run the following to check it:
[server]$ cat ~/php.log
Summary
At first, the new php.log file will not exist. Only after a PHP error is thrown is the php.log file automatically created. This file will then continue to log any future PHP errors.