Turn on the PHP error log reporting in the same folder Where the script is?

WebHostingTurn on the PHP error log reporting in the same folder Where...

Error reporting plays a crucial role in the development and maintenance of PHP applications. It allows developers to identify and fix issues quickly, ensuring the smooth functioning of their code. By enabling PHP error reporting in the same folder where the script is located, developers can conveniently access error messages without the need for additional configurations. This technical blog will explore the importance of having error reporting in the script’s folder and provide an effective and 100% working method to enable it for your web applications.

This article will be very helpful for those who are working on server-side scripting which processes the request received from the front end. In these scenarios, they do not access the real script on the front end and so can not see the error directly on the web page, but can only check it from the error log file.

Why Enable Error Log Reporting in the Same Folder?

By default, the error reporting is already turned on for PHP in most of the web servers, but usually, it is a central location (such as /var/logs/error.log, or /var/logs/php/error.logs, /var/log/nginx/error.log, etc.) where all the PHP errors are logged, it could have errors related to a simple warning, Notice, or fatal error. This single error log file could have errors generated by many websites hosted on the same servers or errors caused by many scripts hosted in different folders. So, in such a case debugging a particular script becomes very complex and time-consuming as you need to go through a long log file to see the error thrown by the script you are working on.

To overcome this complexity and to make it simple, most developers prefer to enable error reporting in the same folder where the PHP scripts are located so that they can easily find and read through the error log file related to their script.

Enabling PHP error reporting in the same folder as the script offers several advantages. Firstly, it provides developers with immediate visibility of errors occurring within their scripts, allowing them to promptly address any issues. This can greatly accelerate the debugging process and enhance the overall development workflow. Additionally, having error reporting in the script’s folder helps maintain a clean and organized code structure, making it easier to locate and troubleshoot errors within the appropriate context.

You may also like to Read: How to Automatically Restart MariaDB on Failure in Bitnami?

How to Enable PHP Error Log Reporting in the Same Folder?

To enable error reporting and to set the location of error log file you need to edit the php.ini file. In order to do that, you need to first know the php.ini file location on your server. If you already know the file location you can skip Step 1 and move on to Step 2.

Step 1. Find the location of the php.ini file

#1. To find the php.ini file location, simply create a PHP file and name is anything you want. Put the following code in the file. I created the file named phpinfo.php.

<?php
phpinfo();
?>

#2. Now just upload this file to your website/webserver, and access it via a URL such as https://domain.com/phpinfo.php. I uploaded it to the root directory of my website and accessed the file in the browser. This will load a webpage containing all the important information related to PHP configuration on your web server. See the screenshot below for reference.

PHP.INI File Config in Apache
PHP.INI File Config in Apache/Nginx
PHP.ini in OpenLiteSpeed server
PHP.ini in OpenLiteSpeed server
php.ini configuration and Location in Bitnami
php.ini configuration and Location in Bitnami

#3. Once the page loads, look for the loaded configuration file location as highlighted in the image above. Normally the PHP.ini file is located in the following folders.

In Apache/Nginx FPM: /etc/php/{php_version}/fpm/php.ini

In Some Apache Config: /etc/php/8.1/apache2/php.ini

In Bitnami Application: /opt/bitnami/php/etc/php.ini

In OpenLiteSpeed: /usr/local/lsws/lsphp74/etc/php/{php_version}/litespeed/php.ini

#1. Enable Error Reporting

Step 2. Edit the php.ini file to enable the error log and set the log file location

Open the php.ini file with your favorite text editor such as Nano or vim editor and look for the following directives. If they are available or commented on, uncomment them to enable error reporting ot each types.

error_reporting = E_ALL & ~E_NOTICE
error_reporting = E_ALL & ~E_NOTICE | E_STRICT
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ER… _ERROR
error_reporting = E_ALL & ~E_NOTICE

#2. Set the Error log file’s name and location

Now look for the error_log directive in the php.ini file and set its value to the name and location of the log file you want. As we want the file to be located in the same folder where the script is running so we will just set its value as the name of the error log file without any folder location path.

So simply, Set the error_log value to the desired name of the log file without including any slashes. This configuration ensures that the file is saved in the script’s directory. In our case we want the file name to be php_error.log. Now this is the file that will appear in each directory of your website and shows the error generated by all the scripts that run inside the directory. Also, turn on the log_errors value to enabled error logging.

log_errors = On
error_log = "php_error.log"
set Error log file in php.ini file
Setting the name and location of PHP error log file

Step 3. Restart the Web Server

Finally, restart the webserver for the changes to take effect. Use the following commands to restart the Webserver as per your server type.

//To Restart Apache Webserver
systemctl restart apache2

// To Restart nginx
systemctl restart nginx

// Retstart OpenLiteSpeed WebServer
/usr/local/lsws/bin/lswsctrl restart
or 
systemctl restart lsws

//Restart Apache in Bitnami Application 
sudo /opt/bitnami/ctlscript.sh restart apache

How to clean these error log files from each folder easily?

Having error logs reported in the same folder where scripts are executed brings several advantages. When an error occurs, the corresponding error log is created in the same directory, maintaining a clear association between the error and the script that triggered it. This simplifies the process of identifying which script is problematic and enables faster debugging. However, these logs can accumulate over time and consume significant disk space. You can read my other article to easily delete all the PHP error logs files from a folder and all of its subfolders using a single command.

I hope this article helped you fix the bug in your script and you were able to diagnose the issue effectively and quickly. By adopting the aforementioned method, developers can gain immediate access to error messages and ensure that their PHP applications are running smoothly. Error reporting in the script’s folder promotes efficient troubleshooting and contributes to the overall maintenance and quality of PHP codebases.

Nasir Sohail
Nasir Sohail
Nasir is a software engineer with an M.Sc. degree in software engineering and various certifications related to computer hardware and networking, such as MCSE, CCNA, RHCE. He has more than 15 years of mixed industry experience mostly related to IT Support, Web development and Server administration. He also offers his freelancing gig for IT support and consultancy and has more than 300 five-star reviews across platforms like Fiverr, Google, TrustPilot, etc.
Watch & Subscribe Our YouTube Channel
YouTube Subscribe Button

Latest From Hawkdive

You May like these Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.