6

How do I enable PHP error logging?

 2 years ago
source link: https://help.dreamhost.com/hc/en-us/articles/214199378-How-do-I-enable-PHP-error-logging-
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How do I enable PHP error logging?

Overview

Every installation of PHP comes with default settings which are usually fine for most websites. Occasionally, the needs of a website makes it necessary to adjust these PHP settings. To view any PHP errors or warnings that could result, you can view your error.log file:

You can also create your own PHP error.log file to store these errors in a different location.

Creating your own custom PHP error.log file will override the default location of your error.log file. When you create the file as shown below, you will no longer see future PHP errors/warnings in your error.log file. You will only see future PHP errors/warnings within your custom PHP error.log file.

However, this only applies to PHP errors. Any other website errors such as Apache webserver errors will continue to show in your regular error.log file.

Error logging

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.

Creating the phprc file

The standard term for a PHP configuration file is php.ini. However, DreamHost uses the term "phprc" file instead. This acts the same way – only the name is different. In this article, only the term phprc is used.

By default, DreamHost logs PHP errors in your domain's error.log file, but you can change the PHP error logging location using a phprc file. Changing the location of PHP error logging is a simple step that allows custom access to PHP errors to diagnose problems with your website.

View the following articles for instructions on how to create a phprc file.

If the file already exists, view the following articles for instructions on how to update it (depending on if you're using an FTP client or SSH):

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 $HOME/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.

See also

Did this article answer your questions?

Sending feedback...

This form is only for article feedback. If you require assistance, please contact support.
How can we improve this page?

Thank you for your feedback!

We'll use it to improve the Knowledge Base.

If you still require assistance, and you're an existing DreamHost customer, please contact support.

If you're not a customer yet, please contact our sales team for further assistance.

Article last updated September 21, 2021 22:05 PST.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK