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.
8 Answers
Check: http://httpd.apache.org/docs/2.2/programs/apachectl.html
apachectl configtest - 1I am getting this message running the above command
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this messageCiasto piekarz– Ciasto piekarz2016-09-15 16:18:37 +00:00Commented Sep 15, 2016 at 16:18 - 2Add a line "ServerName whateveryoulike" to your apache config. Replace whateveryoulike with what ever you like to name your server.Pit– Pit2016-09-26 06:48:16 +00:00Commented Sep 26, 2016 at 6:48
- 10
- Check everything apache with helpful suggestions
curl -L https://raw.githubusercontent.com/richardforth/apache2buddy/master/apache2buddy.pl | perlRichard Tyler Miles– Richard Tyler Miles2022-05-29 23:22:36 +00:00Commented May 29, 2022 at 23:22 - If you have letsencrypt certificates you need
sudoor will complain about missing certificate files. This applies to all files readable only by root.PeterM– PeterM2024-05-13 13:21:36 +00:00Commented May 13, 2024 at 13:21
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
- 5Oddly, on Ubuntu, when I run
apachectl configtestI getSyntax OK, but when I runapache2 -tI getAH00526: Syntax error on line 74 of /etc/apache2/apache2.conf(among other errors)Buttle Butkus– Buttle Butkus2018-12-08 02:02:57 +00:00Commented Dec 8, 2018 at 2:02 - @ButtleButkus but the server when you systemctl restart apache2 is ok right?Benyamin Limanto– Benyamin Limanto2021-10-06 04:16:58 +00:00Commented Oct 6, 2021 at 4:16
- @ButtleButkus use
apache2ctl -tas per askubuntu.com/questions/146688/…jcomeau_ictx– jcomeau_ictx2024-05-16 17:39:39 +00:00Commented May 16, 2024 at 17:39
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...
- Is there a way to test for common causes of failure per above?Vishal– Vishal2020-09-22 00:59:41 +00:00Commented Sep 22, 2020 at 0:59
- That would require a script, that parses the apache config and implements the missing checks. I'm not aware of any freely available scripts that does so.BertD– BertD2020-09-22 12:26:19 +00:00Commented Sep 22, 2020 at 12:26
- 3I have written such a script. If I took the time to put it on github, would anyone use it? It specifically checks SSL configurations, that the files are in the correct location and that the modulos match using openssl, among other things.James M. Lay– James M. Lay2021-09-27 19:09:40 +00:00Commented Sep 27, 2021 at 19:09
- 1I would! Putting it on github definitely is a bonus.BertD– BertD2021-09-28 20:40:48 +00:00Commented Sep 28, 2021 at 20:40
- 2@Jakke PRs are welcome. github.com/jlmgtech/apache-config-auditorJames M. Lay– James M. Lay2022-07-20 16:02:47 +00:00Commented Jul 20, 2022 at 16:02
apachectl configtest is the correct answer. Unfortunately I've got a windows installation where apachectl is missing. Here calling httpd also helps.
What I usually do is
apache2ctl -t && apache2ctl graceful - 7Your answer would be more useful if you explained why this works.crafter– crafter2021-07-30 10:56:44 +00:00Commented Jul 30, 2021 at 10:56
- 2I'll hazard a guess. The "-t" runs a syntax test for configuration files only. If that fails, the && prevents the next command from running. The next command, 'apache2ctl graceful' will restart the httpd service in a way that won't disconnect existing connections (hence, graceful).Joseph Van Riper– Joseph Van Riper2022-01-27 16:06:56 +00:00Commented Jan 27, 2022 at 16:06
There is always a good idea to take a look at the error logs.
less /var/log/httpd/error_log -- Paul
- This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Reviewbjoster– bjoster2022-12-10 18:15:23 +00:00Commented Dec 10, 2022 at 18:15
I have actually tried before:
apachectl configtest
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[1]: Starting The Apache HTTP Server... May 30 17:16:45 kali apachectl[168394]: AH00526: Syntax error on line 13 of /etc/apache2/mods-enabled/security2.conf
For me, this works (maybe because I had set different permissions on files and/or I have installed mod security2):
sudo apachectl -t
Without sudo it can't access some config files so it'll throw an error.