I'm looking for a command that checks the validity of the config files in Apache server on both Debian and RHEL distros. I need to do this prior to restart, so there will be no downtime.
Another way is
httpd -t. Therefore, it's available in Windows-version of Apache. Check http://httpd.apache.org/docs/2.4/programs/httpd.html
The Apache config test (
apachectl configtest, or its equivalents) only tests the config file (and the files it recursively includes) for valid syntax. However, the original question asked for preventing downtime. Even when
apachectl configtest does not return an error, an actual restart may still fail, causing downtime.
Common causes for such failures include missing or inaccessible SSL certificates, missing directories for log files or a missing website root directory. Often, such errors are caused by removing a vhost's directory without removing the vhost Apache config file. It is highly recommended to use a tool like puppet or ansible to prevent such inconsistencies.
Seeing that this question is the number one hit when googling "apache config lint" I thought I'd mention this little detail...
I have actually tried before:
We can actually see the status code to know the error:
/etc/init.d/apache2 restart; systemctl status apache2.service
● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Sun 2021-05-30 17:16:45 +08; 41ms ago Docs: https://httpd.apache.org/docs/2.4/ Process: 168391 ExecStart=/usr/sbin/apachectl start (code=exited, status=1/FAILURE) CPU: 67ms
May 30 17:16:45 kali systemd: Starting The Apache HTTP Server... May 30 17:16:45 kali apachectl: AH00526: Syntax error on line 13 of /etc/apache2/mods-enabled/security2.conf