Force defined php.ini log file - php

php.ini:
error_log = /var/log/php.log
Running on shell:
php#> error_log("test");
// WRITES TO /var/log/php.log
Running on apache index.php:
error_log("test");
// WRITES to apache/error_log
Apache 2.4.6 / Php 5.4.16
I already checked for alternative php.ini files
What i need is the apache directive that controls it, already looked in the man page.
Already checked permissions and file ownership

You may set this path directly from the PHP code. For example:
ini_set("log_errors", 1);
ini_set("error_log",$_SERVER['DOCUMENT_ROOT']."/logs/php-error.log");

My new vps have SELinux enabled so i had to add this attr to the file
sudo chcon -t httpd_log_t php.log

Related

phpinfo shows allow_url_fopen is Off. It is On in php.ini

I'm trying to run a php website with nginx using php8.1 and php8.1-fpm, in a Ubuntu 20.04 vps.
phpinfo reports that the config file in use is: /etc/php/8.1/fpm/php.ini
It also reports that allow_url_fopen is Off (both Local Value and Master Value).
Examining /etc/php/8.1/fpm/php.ini shows:
allow_url_fopen = On
I suppose that's the default setting. But I need this value to reflect in phpinfo and I can't get that to work.
I've tried changing the value, restarting nginx and fpm, changing it back and restarting again, but nothing works. Feels like phpinfo is getting its values elsewhere. I've checked all files in /etc/php/8.1/fpm/conf.d (the config folder reported by phpinfo) and there is no allow_url_fopen in any of those.
How do I get allow_url_fopen to be On?
Type “php --ini” command to find the location of the PHP configuration file.
On the above server, you can see that the PHP configuration file is in location “/usr/local/lib/php.ini”
Edit the php.ini file to enable allow_url_fopen.
root#server [~]# vi /usr/local/lib/php.ini
allow_url_fopen = On
Change the line “allow_url_fopen = Off” to “allow_url_fopen = On”
Save the php.ini file after changing allow_url_fopen to On.
Restart the apache service after enabling allow_url_fopen.
Now, allow_url_fopen is enabled globally for all domains on your Server. allow_url_include = On
On some systems there's a file /etc/php.d/security.ini or /etc/php/8.1/fpm/conf.d/99-security.conf in which allow_url_fopen is turned of. If present it overrides the settings in php.ini system wide.
Unfortunately this file isn't listed by php_ini_scanned_files().

auto_prepend_file not working in php.ini

I can't get the auto_prepend_file setting in php.ini to work. I tested the path directly in the project folder I'm working on and it works seamlessly. Based off everything below, does anyone have some ideas as to what to try next? Any help would be greatly appreciated!
// This did nothing
ini_set("auto_prepend_file","/Applications/MAMP/bin/php/php5.5.10/prepend.php");
// This allowed it to work by including it in the index.php but it's not using the auto_append_file setting
require_once '/Applications/MAMP/bin/php/php5.5.10/conf/prepend.php';
Bash Commands Used:
# When I did this, it gave me the correct path
$ cat /Applications/MAMP/conf/php5.5.10/php.ini | grep auto_prepend_file
auto_prepend_file ="/Applications/MAMP/bin/php/php5.5.10/conf/prepend.php"
# But when I ran this command, it shows that there are no values (just like it does when running phpinfo() )
$ php -i | grep auto_prepend_file
auto_prepend_file => no value => no value
# Nothing is output when I run this command
$ php -r "echo ini_get('auto_prepend_file');"
The php.ini paths from phpinfo()
Configuration File (php.ini) Path /Applications/MAMP/bin/php/php5.5.10/conf
Loaded Configuration File /Library/Application Support/appsolute/MAMP PRO/conf/php.ini
I found a ton of php.ini files on my system
/Library/Application Support/appsolute/MAMP PRO/conf/php.ini
/Applications/MAMP/conf/php5.5.10/php.ini
/Users/alexcory/jarvis/tmp/backup/php.ini~
// Not sure if these have any effect
/private/etc/php-fpm.conf.default
/private/etc/php.ini.default
Contents of prepend.php
<?php
die('Hello World'); // Haven't been able to get "Hello World" to show
require_once dirname( _file_ ) . '/kint/Kint.class.php';
StackOverflow questions referenced.
auto_prepend_file is not working in cli mode
Why would auto_prepend_file works in .htaccess but not in php.ini?
php version issue in auto_prepend_file
MAMP allows you to open the php.ini file from within the application by going to File ▶ Edit Template ▶PHP ▶ php {version} php.ini.
That should open the php.ini in an editor such as a TextEdit. I hope that helps!

Can't get PHP errors to go to a log file (other than the Apache log)

On a Ubuntu web server [LAMP], I'm trying to get PHP errors to write into a php_error file, but no matter what I do, they keep going to the apache log.
Here's what I have tried/done:
Edited the php.ini file:
error_reporting = E_ALL | E_STRICT
display_error = Off
log_errors = On
error_log = /var/log/php_errors.log
Restarted Apache
Checked the phpinfo() output to verify that the changes I made to the php.ini file took -- they did.
After verifying that the errors were still going to the Apache log, I physically created the php_errors.log and tried again. Still going to the Apache log!
Rebooted the web server! Still...!
Anybody have a solution?
Check the directory permissions for /var/log. Ensure that the user your Web service is running as has write permissions to that folder. Alternatively, create a subfolder (/var/log/phplogs?) and assign explicit permissions on that for the user in question then change the error_log value to be a file in that folder
It had to do with ownership. One or the other has worked [for reasons that are not clear to me]:
chown www-data:www-data /var/log/php_error.log
chown same-user-as-www-home:same-user-as-web-home /var/log/php_error.log
Also, the following has made a difference:
chmod 664 /var/log/php_error.log
[as opposed to chmod 644...again for reasons that are not clear to me]
For the record, Ubuntu uses AppArmor and it limits what Apache does.
Additionally, to change the permissions to the file and editing the php.ini file, you must do the next steps:
Go to the next file
/etc/apparmor.d/abstractions
Edit this file
apache2-common
Add the next line
/var/log/php_errors.log rw,
where rw means the process could read and write this file
or you could also do:
sudo nano /etc/apparmor.d/abstractions/apache2-common
And finally, reload the configuration of apparmor
systemctl reload apparmor
Note: Centos/Redhat/Oracle Linux uses SELinux and it requires the same step but the configuration is different.

Where can I find error log files for PHP?

Where can I find error log files?
I need to check them for solving an internal server error shown after installing suPHP.
You can use lsof to find open logfiles on your system. lsof just gives you a list of all open files.
Use grep for "log" ... use grep again for "php" (if the filename contains the strings "log" and "php" like in "php_error_log" and you are the root user you will find the files without knowing the configuration).
lsof | grep log
... snip
gmain 12148 12274 user 13r REG 252,1 32768 661814 /home/user/.local/share/gvfs-metadata/home-11ab0393.log
gmain 12148 12274 user 21r REG 252,1 32768 662622 /home/user/.local/share/gvfs-metadata/root-56222fe2.log
gvfs-udis 12246 user mem REG 252,1 55384 790567 /lib/x86_64-linux-gnu/libsystemd-login.so.0.7.1
==> apache 12333 user mem REG 252,1 55384 790367 /var/log/http/php_error_log**
... snip
lsof | grep log | grep php
**apache 12333 user mem REG 252,1 55384 790367 /var/log/http/php_error_log**
... snip
Also see this article on finding open logfiles: Find open logfiles on a Linux system
It works for me. How can we log all PHP errors to a log file?
Just add the following line to file /etc/php.ini to log errors to specified file – file /var/log/php-scripts.log
vi /etc/php.ini
Modify the error_log directive:
error_log = /var/log/php-scripts.log
Make sure display_errors is set to Off (no errors to end users):
display_errors = Off
Save and close the file. Restart the web server:
/etc/init.d/httpd restart
How do I log errors to syslog or Windows Server Event Log?
Modify error_log as follows:
error_log = syslog
How can we see logs?
Login using ssh or download a log file /var/log/php-scripts.log using SFTP:
sudo tail -f /var/log/php-scripts.log
On CentOS with cPanel installed, my logs were in:
/usr/local/apache/logs/error_log
To watch: tail -f /usr/local/apache/logs/error_log
It depends on what OS you are using and which web server.
On Linux and Apache, you can find the Apache error_log in folder /var/log/apache2/.
This will definitely help you,
Enable PHP error logging
Or
In php.ini (vim /etc/php.ini or sudo vim /usr/local/etc/php/7.1/php.ini)
display_errors = Off
log_errors = On
error_log = /var/log/php-errors.log
Make the log file, and writable by user www-data:
sudo touch /var/log/php-errors.log
/var/log/php-errors.log
sudo chown <owner>:www
I am using CentOS 6.6 with Apache and for me error log files are in:
/usr/local/apache/log
This is a preferable answer in most use cases, because it allows you to decouple execution of the software from direct knowledge of the server platform, which keeps your code much more portable. If you are doing a lot of cron or CGI, this may not help directly, but it can be set into a configuration at web runtime that the cron and CGI scripts pull from to keep the log location consistent in that case.
You can get the current log file assigned natively to PHP on any platform at runtime by using:
ini_get('error_log');
This returns the value distributed directly to the PHP binary by the web server, which is what you want in 90% of use cases (with the glaring exception being CGI). CGI will often log to this same location as the HTTP web server client, but not always.
You will also want to check that it is writeable before committing anything to it to avoid errors. The configuration file that defines its location (typically either file apache.conf globally or vhosts.conf on a per-domain basis), but the configuration does not ensure that file permissions allow write access at runtime.
For Unix CLI users:
Most probably the error_log ini file entry isn't set. To verify:
php -i | grep error_log
// error_log => no value => no value
You can either set it in your php.ini CLI file, or just simply quickly pipe all standard error yourself to a file:
./myprog 2> myerror.log
Then quickly:
tail -f myerror.log

Store PHP error log file in the script directory without adding PHP code to existing scripts

I have been busy setting up my own VPS after being used to cPanel, but I can't seem to find out how to let PHP create an error_log file in the same directory as the script that throws the errors.
I would like this to happen without me having to add a line of code to each .php file. In cPanel this works out of the box somehow.
Example:
Error in: /var/www/webapp1/index.php
Logfile location: /var/www/webapp1/error_log
Error in: /var/www/info/system/test.php
Logfile location: /var/www/info/system/error_log
Basically, I want PHP to store an error_log file in each directory for the scripts in that directory.
Additional information:
Single VPS account
Debian 6.0 (Squeeze) GNU/Linux
Apache 2.2.16
Set the error_log value to the name of the error log you want to appear in the directory, but do not put any slashes. The file will be saved in the directory from which the script is ran, so the same directory.
error_log = "php_error.log"
For this, there is the error_log directive in php.ini like:
error_log string
Where string represents the name of the file where script errors should be logged. The file should be writable by the web server's user. If the special value syslog is used, the errors are sent to the system logger instead. On Unix, this means syslog(3) and on Windows NT it means the event log. The system logger is not supported on Windows 95. See also: syslog(). If this directive is not set, errors are sent to the SAPI error logger. For example, it is an error log in Apache or stderr in CLI.
Edit your php.ini file and uncomment the line with error_log:
error_log = php_errors.log
Save the changes, restart Apache, and be happy.
If you're using Linux, open a terminal and type this to restart:
sudo services apache2 restart
If you have access to WebHost Manager (WHM) you can search for SERVICES and then restart HTTPD or Apache.
If you're using EasyPHP or Vertrigo, you can do that through the program it self.
basically I want php to store an error_log file in each directory for the scripts in that directory
Assuming you're using Apache, you can use Apache's error_log directive in the VirtualHost to accomplish this behaviour. If memory serves me right, PHP itself doesn't decide where it stores its errors.

Categories