3

When starting off, I used .htaccess files to instruct Apache how to handle a request. Not only have I read that doing so was inefficient, I found having a multitude of .htaccess files located throughout my file system was next to impossible to manage.

Then, I abandoned .htaccess, and put everything in httpd.conf. For awhile, everything was great, but then I added another virtual server and another, and had a huge file which was difficult to view in one sitting. Furthermore, the changes I made to httpd.conf had nothing to do with the overall webserver, but only to the applications in the individual websites. This made using a git repository on the websites and on the configuration file disjointed.

Now, I am contemplating moving the virtual server configuration from the main httpd.conf to a file in each virtual server root (not the public root, but one below, and I will probably confusingly call it httpd.conf), and putting an include statement in the main httpd.conf file to point to it. At least, when troubleshooting, I don't have to look for .htaccess files in each directory, but have one main httpd.conf file which points to them.

What is the best way to deal with httpd configuration files when dealing with multiple virtual servers?

4

You can easily split your http.conf file up into multiple files. Debian based systems (such as Ubuntu) come configured that way by default.

To make that happen you can include lines like this in your main conf file:

IncludeOptional sites-enabled/*.conf

Then you can put each of your site's virtual host files into a separate file like sites-enabled/example.com.conf:

<VirtualHost *:80>
    Servername example.com
    DocumentRoot /var/www/example.com
    <Directory /var/www/example.com/>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

The virtual host files will be loaded in alphabetical order. Since the first one is the "default" handler for when the server name isn't otherwise matched, you'll probably want to name one something like 000default.conf so that you know it will come first.

  • Thanks Stephen. The IncludeOptional with wildcard makes it nice! Would you recommend not putting the VirtualHost configuration in a .htaccess file? – user1032531 Jan 22 '15 at 11:54
  • closetnoc's answer addresses that pretty well. It depends on whether you want to make changes without restarting the webserver and how much control you are willing to give to users that have access to the web files directories. – Stephen Ostermiller Jan 22 '15 at 13:35
  • Don't think so per httpd.apache.org/docs/2.2/mod/core.html#virtualhost – user1032531 Jan 22 '15 at 13:36
  • 1
    I always suggest putting as much long-term configuration stuff into the various .conf files and keeping the more day to day stuff in the .htaccess. Think back to what you are changing most often. I like redhat. Each OS creates their own install package and may not have updated the package to use the newer virtual hosting configuration configuration that, say, Ubuntu does. Keep asking questions. – closetnoc Jan 22 '15 at 15:57
  • "...putting the VirtualHost configuration in a .htaccess file" - Perhaps just terminology, but you can't put a "VirtualHost configuration in a .htaccess file". – MrWhite Jan 23 '15 at 2:34
4

It seems to me that you are doing things the hard way. Your httpd.conf file should not be large and there should only be one .htaccess file per site. As well, you are under the impression that .htaccess files are inefficient which is not really the case.

Using config files requires the Apache server to be restarted in order for the configuration change to take effect. For some features, such as redirects, where changes can be frequent, this is not recommended. Httpd.conf and the various site configuration files are not the best place for some configuration work.

Traditionally, this would be left up to the .htaccess file. Only one per site is necessary since references to sub-directories and such can be made from the one file. If you were creating multiple .htaccess files per directory within the www directory, while this is fine to do, it really is unnecessary and harder to manage as you said.

As far as .htaccess files being inefficient, this is a bit of a misnomer. There is a slight additional burden of accessing an additional file for configuration, however, this burden is extraordinarily light and should not be a consideration. Other than that, the .htaccess file is no more inefficient that any httpd.conf file. The process is exactly the same except that using an .htaccess file does not require restarting Apache. This file is poled periodically and configuration changes applied automatically. This happens rather efficiently and quickly.

You used the term virtual server. This may mean something different to you than it does to me so I am not exactly clear what you are referring to. For this reason, I am assuming you mean another website using virtual hosting methods.

Traditionally, virtual websites are created under the home directory each with it's own directory. Lately, some configurations like to create one main site and other site directories under that. I do not recommend this for security reasons. It is possible to secure this, but it comes at a cost. In the traditional method, you would create your web space under the home directory for each site you have. Then you would create under that a www, log, and any other directory you need. This is where I create any cgi-bin style directory for example. This avoids other security issues with having it in the www directory. You would then use the sites-available directory in /etc/apache2/ to create your virtual hosted sites. I will not get into the details of this since it is a broad topic and if you have a control panel, much of this should be handled for you though some control panels configure Apache sites differently.

My recommendations are simple. Static configurations go either in the httpd.conf file or in the case of virtual hosting within a site configuration file using the sites-available directory. As well, all ad-hoc configuration changes would be made to the .htaccess file within the www directory and that file would contain all the configuration for the site.

  • Thanks closetnoc. Sorry, VirtualHost, not virtual server. That is really what I am asking. Putting the VirtuahHost directive and associated settings (SSL, etc) in a separate file than the main one (for Redhat, /etc/httpd/conf/httpd.conf. – user1032531 Jan 22 '15 at 11:59
  • It appears that you may be running an older version of Apache or your package install still uses /etc/httpd/. What is the version of Apache? Is there a sites-available directory under /etc/httpd/? – closetnoc Jan 22 '15 at 15:49
  • Using old 2.2. Yes, I do have /etc/httpd/. No, there is no sites-available directory under it. By the way, I really liked your response, and but didn't know how it solved my problem. Maybe because of my old Apache? – user1032531 Jan 23 '15 at 2:04
  • Apache 2.2 is not that old. It appears that Redhat did not update the build to include the virtual hosting configuration files. Do you still need help in creating the various configuration files? Or did the link that Stephen Ostermiller provide help you solve your problem?? – closetnoc Jan 23 '15 at 2:08
  • "This file is poled periodically and configuration changes applied automatically." - Are you referring to .htaccess? – MrWhite Jan 23 '15 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.