#httpd.conf

Hey Apache where’s my httpd.conf ? (2)

Posted by on Aug 20, 2016 in Web & Server

This post follows on from part 1 where i was getting to grips with the new structure of a Debian Jessie install of Apache2.4. It deals with the virtual server file needed to set up a site, activation and some basic canonical issues  such as redirecting a www site to the non-www version.

I hope the following makes sense to anyone setting up a new virtual host in Debian Jessie.   It works for me so theoretically should work for others. There were a couple of things that were different in this installation so i hope this helps anyone with the same hiccups.

For a start everytime i tried to enable the new site  using the “a2ensite” command and pointing to new site’s .conf file i was told by Debian that the site didn’t exist.

For example if i had a Virtual host set up in the file “mylovelysite.conf” inside the folder “sites-available” i couldn’t simply use the command a2ensite  (path to) /my lovelysite.conf

everytime i did… i got an error message saying the site didn’t exist.

Debian Jessie simply requires you to use the command “a2ensite mylovelysite”. It’s already looking in the ‘sites available’ folder and doesn’t  need a path or to be told to look for a .conf file. The process has therefore become a little simpler.

So with the following virtual host set up in a field called “mylovelysite.conf” which resides in the /etc/apache2/sites-available directory…

 

<VirtualHost *:80>

ServerName mylovelysite.com
ServerAlias *.mylovelysite.com
ServerAdmin youremail@mylovelysite.com
DocumentRoot /var/www/mylovelysite/public_html/
ErrorLog /var/www/mylovelysite/logs/error.log
CustomLog /var/www/mylovelysite/logs/access.log combined

</VirtualHost *:80>

I simply used the command  > a2ensite mylovelysite and Debian was able to activate it.

The next problem was that I found that the mod_rewrites, redirects and handling of canonical names that worked in a previous Debian Apache installation simply weren’t working in the new set-up. Amongst other things such as syntax errors i was getting the ‘too many redirects’ error message from some browsers  – indicating that the server was caught in a self referencing loop where it re-wrote a request, then failed to match it so rewrote it again…and so on ad infinitum.

Reading Newer Apache documentation indicated that i could use matching of the HTTP_HOST being asked for to set a permanent redirect without resorting to mod_rewrite and getting into those terrible redirect loops.

To do this you can use <if>

I prefer sites NOT to use the www prefix and to use the non www site name as the canonical name.  so to set the non www sitename as the canonical name and redirect any other call to the site as that clean non prefixed version i wrote the following inside the virtual host

<If “%{HTTP_HOST} !=‘mylovelysite.com'”>
Redirect permanent “/” “http://mylovelysite.com/”
</If>

Effectively it says if the request is NOT for “mylovelysite.com” then redirect to “mylovelysite.com” and serve it up.

Note that i’m not bothered about using any subdomains here so i don’t have that complication. If you want to achieve the reverse (redirecting non-www names to the www version) then i guess you’d write

<If “%{HTTP_HOST} !=‘www.mylovelysite.com'”>
Redirect permanent “/” “http://www.mylovelysite.com/”
</If>

If any pages in your new site have moved permanently this is also where you’d put in any permanent redirects you require.

For example the /samples.html page that exited on a site  was moved to a page called currentwork.html.

To achieve that i wrote

Redirect permanent /samples.html /currentwork.html

So now the whole virtual host file looks like this:

<VirtualHost *:80>

ServerName mylovelysite.com
ServerAlias *.mylovelysite.com
ServerAdmin youremail@mylovelysite.com
DocumentRoot /var/www/mylovelysite/public_html/
ErrorLog /var/www/mylovelysite/logs/error.log
CustomLog /var/www/mylovelysite/logs/access.log combined

<If “%{HTTP_HOST} !=‘mylovelysite.com'”>
Redirect permanent “/” “http://mylovelysite.com/”
</If>

Redirect permanent /samples.html /currentwork.html

</VirtualHost *:80>

It all seems to work, and by using the <if> directive you can achieve some pretty powerful screening of requests that gives you a lot of control over access to the site and what’s served up.

Hey Apache what happened to my httpd.conf ?

Posted by on Aug 18, 2016 in Web & Server

Running Apache 2.4 on Debian Jessie

So there i was casually upgrading a friends server to Apache 2.4 and looking for the httpd.conf file in Debian Jessie when i realised it had been effectively ‘done away with’.

RIP…… HTTPD.CONF

No only that but, because of syntax changes, a lot of older server-wide configurations and directives using mod-rewrite and redirect were effectively not going to work this time around wherever i put them.

The new /etc/apache2 folder looks like this;

etc:apache2

That little apache2.conf file is important because it now sets a framework to form the main configuration and draws in virtual servers available and other directives and instructions according to what you ask it to pull in.

Here’s what it tells you at the head when you open it up;
apach2conf-01

So you now have a hierarchy set out – controlled by apache2.conf which ‘sucks in’ other configurations set by the user such as which virtual hosts to set, server-wide security configurations etc. Heck it’s pretty much set it out in black and white in the file itself;

apach2conf-02

The apache2 file itself takes care of including the virtual host configurations and whatever generic directives or snippets you care to set..

apache includes

Setting up those virtual hosts and instructing Debian to enable modules and enable the sites via symlinks isn’t too hard – even for a numb-nuts like me.

In Part 2 i’ll take a look a working set up and how to get there. Also how my handling of things like canonical names and permanent re-directs changed to accommodate Apache on Debian Jessie.