Apache configuration: how to get quick feedback? - apache

When writing configuration files for Apache web server I would like to have a quick feedback loop.
I, for example have a script that doesn't seem to work. It is either not picked up, or the variables I use are not set, or maybe overriding is not allowed. How to debug this?
I expected to at least print some debug log statements like REQUEST_URI: %{REQUEST_URI}. Can't find such a thing.

apachectl is a front end to the Apache HyperText Transfer Protocol (HTTP) server. It is designed to help the administrator control the functioning of the Apache httpd daemon.
Here is a link to the documentation.
Different platform might use different binary names such as apache, apache2 or apache2ctl. To test the configuration - just run:
apachectl configtest
# or, depending on your OS
httpd -t
EDIT
If you are trying to debug your virtual host configuration, you may find the Apache -S command line switch useful. That is, type the following command:
httpd -S
This command will dump out a description of how Apache parsed the configuration file

Related

apache subprocess can't access files

i'm running into a problem where a program written to be accessed as a web service can't access anything in the local file system. running the program from the command line works correctly, but fails when called via apache as a subprocess exec'd via system() from a php script ; it simply can't find the files it's supposed to work on. i'm convinced this is an apache configuration issue, as the program has no problem reading /var/www/html [the webserver document root] and any/all files/directories under it.
here are some details :
virtual centos 7 server running apache 2.4.6.
the program runs as user apache
it can read / [root dir] and interprets it correctly [i.e. it really is / the system root, not /var/www/html]
it doesn't have access to /tmp at all, nor to /var/tmp
some external files are network mounted on /mnt. the program can access /mnt, but attempts to read /mnt/{anything} fail -- even though remote shares are mounted w/effective uid/gid as apache and all files/directories are readable by world.
selinux is currently disabled
for kicks, i tried setting Require all granted inside <Directory / > and <Directory /tmp > in httpd.conf, but that didn't change anything.
does anyone have any idea what could be causing this and how i could fix it ?
thanks.
Have you checked to see if selinux is restricting access?
You can see if Selinux is causing issues by install selinux troubleshooting
yum install -y setroubleshoot
sealert -a /var/log/audit/audit.log

Where is documentation of switch options for apache2ctl command?

I can run apache2ctl -S on my debian box and it outputs information about the virtual hosts on the server and other information.
However, when I issue man apache2ctl on my debian box, I don't see any documentation for the the -S switch or any other switches for that matter. Where can I find this documentation?
The man page states that apachectl acts either as passthrough frontend script for httpd binary itself or as service control script for starting, stopping, reloading.
In turn You can / should consult the man page for httpd to find the information about such switches.

How to run a bat file in apache server using Rotatelogs configuration ?

I am trying to rotate the log file in apache server. I tried a lot using rotatelog configuration in httpd.conf in apache configuration. But I can't.
while i configuring the rotatelog in httpd.conf, i found that Rotatelog having the option to run a program that can be acheived by using parameter -p. So i wrote a bat file to rotate the logs. And configured with apache server like below command
ErrorLog "|bin/rotatelogs.exe -l -p logs/apachelog.bat errorlogs 1M"
Whether My way of configuration is correct or not. Once error log file reached that particular file size , i want to execute the bat file. So i tried like this.
Thanks in advance.

httpd running internally but not externally

I have a new server running CentOS, and it has httpd running on 192.168.1.100:80.
I can connect to my server through ssh on 192.168.1.100, but when I go to 192.168.1.100 in my browser, it says "Oops! Google Chrome could not connect to 192.168.1.100".
I also tried wget to see if that works, and here is where it gets interesting.
when I run:
wget 192.168.1.100
On my server it gets the index.html file as it should.
but when I run it on my laptop is says "Connecting to 192.168.1.100:80... failed: No route to host."
Does anyone know how to fix this?
Seems like your Apache configuration binds httpd to 192.168.1.100:80
Find line Listen 192.168.1.100:80 in Apache main configuration - something like /etc/httpd/httpd.conf or /etc/apache2/httpd.conf and change this line to Listen 0.0.0.0:80.
Restart Apache and it will probably work.

Where does PHP store the error log? (PHP 5, Apache, FastCGI, and cPanel)

I am on shared hosting and have cPanel, Apache, and PHP is run by FastCGI. Where does PHP store the error log?
Is there another way I can find the error log on a shared hosting environment instead of having to go through entire site structure to look for error_log files?
I have access to the php.ini file (I am using PHP version 5.2.16).
PHP stores error logs in /var/log/apache2 if PHP is an apache2 module.
Shared hosts are often storing log files in your root directory /log subfolder.
But...if you have access to a php.ini file you can do this:
error_log = /var/log/php-scripts.log
According to rinogo's comment: If you're using cPanel, the master log file you're probably looking for is stored (by default) at
/usr/local/apache/logs/error_log
If all else fails you can check the location of the log file using
<?php phpinfo(); ?>
Try phpinfo() and check for "error_log"
Linux
php --info | grep error
The terminal will output the error log location.
Windows
php --info | findstr /r /c:"error_log"
The command prompt will output the error log location.
To set the log location
Open your php.ini and add the following line:
error_log = /log/myCustomLog.log
Thanks chelmertz and Boom for these (comments on the question).
On a LAMP environment the PHP errors are be default directed to this below file.
/var/log/httpd/error_log
All access logs come under:
/var/log/httpd/access_log
How to find your PHP error log on Linux:
sudo updatedb
[sudo] password for eric:
sudo locate error_log
/var/log/httpd/error_log
Another equivalent way:
sudo find / -name "error_log" 2>/dev/null
/var/log/httpd/error_log
It can also be /var/log/apache2/error.log if you are in Google Compute Engine.
And you can view the tail like this:
tail -f /var/log/apache2/error.log
It appears that, by default, PHP does not log errors anywhere. The error_log key in php.ini is commented out in all the installs I've seen.
Generally I:
look for php.ini files. locate php.ini.
Search these files for the error_reporting value;
Which should be set to whatever amalgamation of PHP log levels are enough for you.,
For example: E_ALL & ~E_DEPRECATED & ~E_STRICT
Check the error_log value to make sure it points to an actual place and is not commented out.
The default value doesn't give a full path, only a file name, I don't know where this path resolves to normally. Probably /var/log/.
You should use absolute path when setting error_log variable in your php.ini file, otherwise, error logs will be stored according to your relative path.
error_log = /var/log/php.errors
Other solution would be writing simple script which would list all error logs files from directory tree.
php --info | grep error
This is helpful.
NGINX usually stores it in /var/log/nginx/error.log or access.log (on Ubuntu in any case).
The best way is to look in your httpd.conf file and see what the default is. It could also be overridden by your specific virtual host. I start by looking at /etc/httpd/conf/httpd.conf or /etc/apache2/httpd.conf and search for error_log. It could be listed as either /var/log/httpd/error_log or /var/log/apache2/error_log, but it might also be listed as simply logs/error_log.
In this case it is a relative path, which means it will be under /etc/httpd/logs/error_log. If you still can't find it, check the bottom of your httpd.conf file and see where your virtual hosts are included. It might be in /etc/httpd/conf.d/ <- as "other" or "extra". Your virtual host could override it then with ErrorLog "/path/to/error_log".
If you have build Apache and PHP from source, then the error logs by default is generated at your ${Apache install dir}/logs/error_log, i.e., generally /usr/local/apache2/logs/error_log.
Else, if you have installed it from the repository, you will find it at /var/log/apache2/error_log.
You can set the path in your php.ini also and verify it by invoking phpinfo().
Wherever you want it to, if you set it in your function call:
error_log($errorMessageforLog . "\n", 4, 'somePath/SomeFileName.som');
If you use the php5-fpm log default, it should be under:
/var/log/php5-fpm.log
Search the httpd.conf file for ErrorLog by running cat <file location> | grep ErrorLog on the command line. For example:
cat /etc/apache2/httpd.conf | grep ErrorLog
Output:
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
ErrorLog "/private/var/log/apache2/error_log"
Find the line that starts with ErrorLog and there's your answer.
Note: For virtual hosts, you can edit the virtual hosts file httpd-vhosts.conf to specify a different log file location.
cPanel Error logs are located in:
/usr/local/cpanel/logs/
/usr/local/apache/logs/
By default Apache logs are located inside:
/var/log/apache
or
/var/log/apache2
If anyone is using a custom log location then you can check it by running this command:
cat /etc/apache2/**conf**/httpd.conf | grep ErrorLog
If you are getting an error that the apache2 directory does not exist then you can run this command to find correct location by:
whereis apache
or
whereis apache2
When configuring your error log file in php.ini, you can use an absolute path or a relative path. A relative path will be resolved based on the location of the generating script, and you'll get a log file in each directory you have scripts in. If you want all your error messages to go to the same file, use an absolute path to the file.
See more in Error Handling Functions.
something like this :
sudo locate error.log | xargs -IX grep -iH "errorlog" X
or
sudo locate error_log | xargs -IX grep -iH "errorlog" X
or
sudo find / -iname "error?log" 2>/dev/null | xargs -IX grep -iH "errorlog" X
I can guarantee you, I am not the only person who has been driven to madness at least once in a frustrating search for a log file. It seems like it should be the easiest thing to find in the whole system.
A definitive guide on where the PHP error log is stored would be a complicated bit of work. The official PHP manual does not even try to address the whole topic, because there are dependencies on systems outside PHP, such as the operating system (Linux vs. Windows, which distribution of Linux), including settings within Windows and Linux that affect the name and location of the PHP error log.
Until someone takes the time to write a complete, cross-system guide, the best you are going to get is general directions where you can inquire. Every PHP developer has had to endure agony in this pursuit, with one exception. If you work in one place and the information is provided when you first need it, then you have the information need forever, that is, until you find yourself in a new working environment. There are such fortunate people.
If the information is not given to you on a silver platter, so to speak, you have some hunting to do. The hunt is not the longest you will face in your career, but it is not the simplest either.
As is evident from the many answers already posted, a smart place to begin is the output of phpinfo(). To view it, create a PHP file containing this:
<?php
phpinfo();
Either browse to that file or run it from the command line. If you do both, you likely will find the error_log is in different places, depending on command line vs. web server use of PHP. That is because the PHP interpreter that runs on a web server is not the same PHP interpreter that runs from the command line, even when the command line is on the same machine as the web server. The answers already posted in here mostly are making an unstated assumption that PHP is running as part of a web server.
The default for error_log is no value
Whatever the value is, it comes from the php.ini files used to configure PHP. There can be many php.ini files. Finding your way among them is confusing at first, but you do not need to deal with this to find your PHP log.
If the output from phpinfo() shows a full path to a file, that is where the log is. You are lucky.
The trick is there usually is not a full path indicated in phpinfo(). When there is not a full path, the location depends on:
Whether error_log is no value. If it is, the log file location will depend on the operating system and the mode PHP is running. If PHP is running as an Apache module, on Linux the log often is in /var/log/apache2/error.log. Another likely spot is in a logs directory in your account home directory, ~/logs/error.log.
If there is a file name without a path, the location depends on whether the file name has the value syslog. If it syslog, then the PHP error log is injected into the syslog for the server, which varies by Linux distribution. A common location is /var/log/syslog, but it can be anywhere. Even the name of the syslog varies by distribution.
If the name without a path is not syslog, a frequent home for the file is is the document root of the website (a.k.a., website home directory, not to be confused with the home directory for your account).
This cheat sheet has been helpful in some situations, but I regret to have to admit it is not nearly universal. You have my condolences.
For CentOS 8 it is var/log/httpd/error_log.
You are on a shared environment and cannot find error log. Always check if cPanel has option Errors in your cPanel dashboard. If you are not being able to find the error log, then you can find it there.
In the cPanel search bar, search Error, it will show Error Pages which are basically lists of different HTTP error pages and other Error is where the error logs are displayed.
Other places to look in a shared environment:
/home/yourusername/logs
/home/yourusername/public_html/error_log
You can go in File Manager and check the logs folder.
Check the log file in a public_html folder.
Check "php phpinfo()" file for where the log is stored.
Mine for some reason stored it in:
/var/log/php-errors.log
I'm using Ubuntu Server 16.04 (Xenial Xerus) and PHP 7.1.28.
WordPress
WordPress will direct error_log() messages to /wp-content/debug.log when WP_DEBUG_LOG is set to true.
See WordPress documentation for WP_DEBUG_LOG
For PHP-FPM, just search the configuration file for error_log:
cat /etc/php-fpm.d/www.conf | grep error_log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
In a shared cPanel environment you cannot find the error log, if your hosting provider doesn’t provide any option in the cPanel dashboard. You can search "error" and see if your provider have any.
Otherwise normally you will find a file called "error_log" in your public_html file, which have all the PHP error recorded.
You can see the php errors for an account under:
/home/username/logs/domain_tld.php.error.log
You can set php error log globally and locally.
Globally through WHM:
WHM -->> MultiPHP INI Editor --> "Editor Mode"
Select the version of PHP you would like to set the error log
Edit the parameter error_log.
error_log = "/some/path"
Change the filename to the desired error log file
Save changes
Locally though cPanel:
CPanel -->> MultiPHP INI Editor --> "Editor Mode"
Select the version of PHP you would like to set the error log
Edit the parameter error_log.
error_log = "/some/path"
Change the filename to the desired error log file
Home directory will set the option for all domains, addons, and subdomains configured on the account
A specific domain will limit the change to that domain, or, in the case of an addon domain, the addon and the matching subdomain
Save changes
If PHP-fpm is enabled:
WHM --> MultiPHP Manager
Find the domain you'd like to change the error log location for
Click "Edit PHP-FPM"
Change the log location in "The error log file (error_log)" relative to the folder "logs" within the user's home directory
Save changes