Simone Nigro
Simone Nigro

Reputation: 4887

Apache: include htaccess in conf with AllowOverride None, better performance?

Suppose we have the /home/example.org/public_html/ directory on the filesystem, which serves as the document root of my virtualhost.

The relevant httpd configuration for that vhost would look like this:

<VirtualHost *:80>
  ServerName example.org:80
  ...
  DocumentRoot /home/example.org/public_html
  <Directory /home/example.org/public_html>
    AllowOverride All
    ...
  </Directory>
  ...
</VirtualHost>

In order to prevent the htaccess lookups on the filesystem without losing the htaccess functionality – at least at the DocumentRoot level- I transformed the configuration to the following:

<VirtualHost *:80>
  ServerName example.org:80
  ...
  DocumentRoot /home/example.org/public_html
  <Directory /home/example.org/public_html>
    AllowOverride None
    Include /home/example.org/public_html/.htaccess
    ...
  </Directory>
  ...
</VirtualHost>

Difference

AllowOverride None
Include /home/example.org/public_html/.htaccess

Let’s see what we have accomplished with this:

httpd does not waste any time looking for and parsing htaccess files resulting in faster request processing

Questions:

  1. Using Include directive, Apache load htaccess only on service start or for each request?
  2. If point 1 it's true, how do refresh apache conf without httpd.exe -k restart?

Upvotes: 3

Views: 7810

Answers (2)

mc0e
mc0e

Reputation: 2820

Firstly, note that checking for .htaccess is commonly not all that big an issue, since the relevant bits of the disk are cached in memory. It becomes an issue where for example you have a very large number of directories under your web root directory or directories, and the hits are scattered amongst them so that the hit rate on cached disk blocks is low. You might be better dealing with that by disabling .htaccess selectively for directory trees where it creates a problem. Parsing the .htaccess directives creates a little CPU load of course, but CPU should generally not be your server's bottleneck.

Answering your question as posed though; Yes, you will need to run a command as root to load the new configuration. Rather than using restart though, use reload or (better) graceful.

httpd.exe -k graceful

You could (but probably shouldn't) write a cron job to periodically check whether this needs to be run. Without a lot of testing, I think something like this should work, run as a regular root cron job:

#!/bin/bash
[ /var/run/httpd/http.pid -nt /home/example.org/public_html/.htaccess ] \
  && httpd.exe -k graceful

This creates a bit of disk load itself of course. This load doesn't increase with traffic volume, but might be an issue if you have many such included files.

SECURITY WARNING: It sounds like you are setting up a situation where a non root user is likely to be able to get Apache to Include directives at will. This is much more powerful than what can be done with a .htaccess file, and amounts to a root exploit. E.g. it gives access to things like the User and LoadModule directives, which .htaccess directives can never do.

I recommend that you should put Included directives in a file inside your Apache configuration directory, and have it accessible only by root. There are other ways to make sure that only root can edit the .htaccess file, but getting these files out of the user-owned area makes it less likely you'll inadvertently open access again later.

While the .htaccess mechanism does incur extra disk load, it is the mechanism that's designed for use by non-root users. It would be nice to have a mechanism for untrusted users to modify configuration with a limit on how often the .htaccess file would be checked for, but if it exists, I don't know it.

Upvotes: 4

hjpotter92
hjpotter92

Reputation: 80649

Apache accesses and processes the htaccess files on each request. This is why one does not need to restart the server every time to check their current configurations.

You do need to restart the server/service for testing any changes made to apache.conf, httpd.conf or the vhost configurations.


Quoting from Apache's tutorial on htaccess file:

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.


Since you already are trying to Include the htaccess from inside a <Directory> module block, the performance would be better if you include everything from the file to this block itself instead. There is, although no difference; apart from having to maintain configurations in two places simultaneously.

The htaccess file will get processed just once, at the time of server start.

Upvotes: 3

Related Questions