How to enable access log for php-fpm in docker container? - php

Note: I am NOT talking about nginx' own access log. That one works fine. This questions is about enabling the php-fpm access.log.
Currently, I am trying to debug the FastCGI sent in stderr: “Primary script unknown” while reading response header from upstream error
One proposed option is to set the access.log entry in the php-fpm.d/www.conf.
access.log = /var/log/$pool.access.log
Now, I have a docker stack and my php-fpm container is build from the php:7.3-fpm-alpine image.
While I have figured out that it stores its php config files at:
/usr/local/etc/php
and I also found the www.conf at:
/usr/local/etc/php-fpm.d/www.conf
So in a local file I add the default content as provided by the image and add at the end:
php_flag[display_errors] = on
php_admin_value[error_log] = /var/log/fpm-php.www.log
php_admin_flag[log_errors] = on
access.log = /var/log/$pool.access.log
and I copy that file during build into the docker container.
COPY ./.docker/php/www.conf /usr/local/etc/php-fpm.d/www.conf
Yet when I try to access my server, I don't see any log file being created on incoming requests.
What am I missing to activate the php-fpm access log? How can I figure out why I don't see any log?

The way to do this is to send the error and the access logs to the following address:
/proc/self/fd/2
So replace the "file" locations like so:
access.log = /proc/self/fd/2
error_log = /proc/self/fd/2
Then you should be able to inspect the fpm-logs with docker logs [container-id]

Related

AWS Elastic Beanstalk PHP logs not in CloudWatch

I have a PHP application running via AWS Elastic Beanstalk. But the PHP error logs don't seem to be included in CloudWatch alongside the access logs, etc. How do I send them to CloudWatch?
Based on some spelunking, the php error logs seem to be sent to /var/logs/php-fpm/www-error.log, decided by the setting in /etc/php-fpm.d/www.conf:
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
The only logs sent to CloudWatch for PHP based on the info here are:
/var/log/eb-engine.log
/var/log/eb-hooks.log
/var/log/httpd/access_log
/var/log/httpd/error_log
/var/log/nginx/access.log
/var/log/nginx/error.log
You could add custom configuration to have the CloudWatch agent pick up the correct file. Or, you could just add the php error messages to a file already being sent. This can be done via the following in a file .ebextensions/my.config:
/etc/php-fpm.d/www-my-overrides.conf:
mode: "000644"
owner: root
group: root
# For some reason, EB configures the php errors to go to /var/log/php-fpm/www-error.log,
# but doesn't include that file in the default log files sent to CloudWatch. This directs
# the log files to the error file that is being sent to CloudWatch
content: |
[www]
php_admin_value[error_log] = /var/log/httpd/error_log
I'm not sure but I think the www-my-overrides.conf file name needs to be alphabetically after www.confg in the same directory.
If you are using nginx, then you need to use /var/log/nginx/error.log as the error log target -- CloudWatch seems to ignore /var/log/httpd unless you use Apache, so even if you write to it, the changes won't show up in CloudWatch.
files:
/etc/php-fpm.d/www-my-overrides.conf:
mode: "000644"
owner: root
group: root
# For some reason, EB configures the php errors to go to /var/log/php-fpm/www-error.log,
# but doesn't include that file in the default log files sent to CloudWatch. This directs
# the log files to the error file that is being sent to CloudWatch
content: |
[www]
php_admin_value[error_log] = /var/log/nginx/error.log
Additionally, you need to make that file writeable by the php-fpm process which is run as webapp by default, plus you want to make sure it exists ... which it won't yet on new instance creation, so it's very important to do both commands:
container_commands:
01-command:
command: echo "-- DEPLOYMENT --" >> /var/log/nginx/error.log
02-command:
command: chmod 666 /var/log/nginx/error.log

How do you set the PHP error log location in the Apache configuration file?

We have an out-of-the-box PHP application running on a subdomain, and it's logging errors in its DocumentRoot:
/var/www/appname/path/to/document/root/php-errors.log
...instead of where we want the error logs to go:
/var/www/appname/logs/php-errors.log
Since I don't want to change the code in the out-of-the-box application, and I don't want to change the error log location for all the subdomains (read: file php.ini), how can I do this in just the Apache configuration file that relates to the subdomain?
From error_log per Virtual Host?:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/domains/example.com/html
ErrorLog /var/www/domains/example.com/apache.error.log
CustomLog /var/www/domains/example.com/apache.access.log common
php_flag log_errors on
php_flag display_errors on
php_value error_reporting 2147483647
php_value error_log /var/www/domains/example.com/php.error.log
</VirtualHost>
For those wishing to do this using php-fpm (which I meant to originally post about), here's how you do it:
Ensure you're logged in as root, or using sudo for all your commands.
Go into your php-fpm directory*, using the command below:
cd /etc/php/fpm/
In that directory, edit your php.ini file and add the following to the end:
If you want to set error log by host
[HOST=www.example.com]
error_log=/var/www/myapplication/path/to/my/error.log
[HOST=sub.example.com]
error_log=/var/www/mysubapplication/path/to/my/error.log
If you want to set error log by path
(handy if you're working on a server with an IP address but no domain)
[PATH=/var/www/myapplication]
error_log=/var/www/myapplication/path/to/my/error.log
[PATH=/var/www/mysubapplication]
error_log=/var/www/mysubapplication/path/to/my/error.log
You now need to go into the pool directory*, using the command below:
cd /etc/php/fpm/pool.d/
In that directory, edit the file www.conf. Note the values for user and group in that file, whose out-of-the-box setting is www-data for both. Look for the term catch_workers_output and make sure it is uncommented and set to yes, like so:
catch_workers_output = yes
Now you need to create the error log file(s) and ensure that php-fpm has access to it. This is why you needed to note the values for user and group from the previous file edit. Create an error log file for each HOST/PATH you set when editing php.ini and give them the appropriate permissions and ownership, for example:
touch /var/www/myapplication/path/to/my/error.log
chmod 0660 /var/www/myapplication/path/to/my/error.log
chown www-data:www-data /var/www/myapplication/path/to/my/error.log
Finally, restart your php-fpm service using the command** below:
service php-fpm restart
* Note: If, like me, you install declared versions of php-fpm, the directory paths will change to (for example) the following:
/etc/php/5.6/fpm/
/etc/php/5.6/fpm/pool.d/
/etc/php/7.1/fpm/
/etc/php/7.1/fpm/pool.d/
** The service takes a specific versioned name if you installed declared versions, and you will need use (for example) the following:
service php5.6-fpm restart
service php7.1-fpm restart
I configure it like this:
File /etc/apache2/envvars
# For supporting multiple Apache 2 instances
if [ "${APACHE_CONFDIR##/etc/apache2-}" != "${APACHE_CONFDIR}" ] ; then
SUFFIX="-${APACHE_CONFDIR##/etc/apache2-}"
else
SUFFIX=
fi
export APACHE_LOG_DIR=/var/log/apache2$SUFFIX
File /etc/apache2/sites-available/000-default.conf
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

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