Mobile app version of vmapp.org
Login or Join
Gail5422790

: Alternatives to multiple .htaccess files or a gigantic httpd.conf file When starting off, I used .htaccess files to instruct Apache how to handle a request. Not only have I read that doing so

@Gail5422790

Posted in: #Apache #Git

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?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Gail5422790

2 Comments

Sorted by latest first Latest Oldest Best

 

@BetL925

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.

10% popularity Vote Up Vote Down


 

@Jamie184

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme