294

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.

ivanleoncz
  • 1,861
Sigtran
  • 3,043

8 Answers8

448

Check: http://httpd.apache.org/docs/2.2/programs/apachectl.html

apachectl configtest
keatch
  • 4,746
63

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

guest
  • 631
15

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...

BertD
  • 291
8

apachectl configtest is the correct answer. Unfortunately I've got a windows installation where apachectl is missing. Here calling httpd also helps.

7

What I usually do is

apache2ctl -t && apache2ctl graceful
mivk
  • 4,924
1

There is always a good idea to take a look at the error logs.

less /var/log/httpd/error_log

-- Paul

YateBTS
  • 11
0

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

Alvin567
  • 111
  • 1
  • 7
0

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.

user2342558
  • 101
  • 5