Jul 25

Apache 2 configuration

by in Linux, Open Source, Techie :: Techno ::

This site is now running on Apache 2 on Debian. The last time I used apache 2 it was an early release and the configuration structure and methods was almost identical to the 1.* series.
The current version uses similar directives, so implementing vhosts (virtual hosts) has not changed that dramatically, however the actual method for configuring them has.
Under the Apache 1.3.29, for example, all directives were contained in httpd.conf, whereas with apache 2 that file is completely blank. I found that a little bit confusing at first :)
So how do you actually do it?



It's not that hard :)
The configuration directory (on my system /etc/apache2/conf ) contains a number of files and directories.
In apache2.conf , which in debian is in /etc/apache2, there is an include directive:
Include /etc/apache2/sites-enabled/[^.#]*
If you look in the same directory you will see that there are two sub-directories:
sites-available
sites-enabled

The idea is very simple. You create a small configuration file for every site you wish to host and place them in sites-available. To make the site active you simply symlink it in sites-enabled and restart apache (a graceful restart might be better, but it caused problems the last time I tried it)
If you want to disable a vhost simply remove the symlink and restart apache2.
This is an interesting way of doing things in my opinion, as you can easily "play with" configuration directives and activate/deactivate them very quickly.
The various apache modules are handled in exactly the same way:
/etc/apache2/mods-available - apache modules installed on the system
/etc/apache2/mods-enabled - apache modules in use
You no longer need to worry about fiddling with the load directives in the .conf, you simply symlink in a modules .load file eg. foo.load, to enable it and delete the symlink to disable it.
Apache: The Definitive Guide

Related Posts:

  • No Related Posts

14 Responses to “Apache 2 configuration”

  1. From Jaime:

    Saluti dalla Spagna

    Posted on April 13, 2005 at 11:09 pm #
  2. From Jaime:

    I am upgrading to Apache2 on my local testing-purpouse webserver. I’m in a hurry now and have no time to read the Apache docs, but found this article very userful. Nice weblog,
    Saluti dalla Spagna

    Posted on April 13, 2005 at 11:14 pm #
  3. From Santiago:

    Can someone please send me a copy of a “small configuration file” as an example… and explain how I may make something like this on the [sites available] directory – thank god I was able to find that.
    Also how do I sim link this to sites enabled? step by step. Some kind soul please help I need to set up a web server before my paid domained finishes..
    Many thanks!! Santiago Argentina..

    Posted on April 10, 2006 at 8:50 pm #
  4. From michele:

    Santiago
    Do you mean a vhost config file?
    Symlinking is quite easy :)
    as root or using sudo:
    ln -s /etc/apache2/sites-available/sitename /etc/apache2/sites-enabled/sitename
    the syntax is:
    ln -s source destination
    or if you think of it as:
    source = the real file
    destination = where you want the link to appear
    hope that helps
    Michele

    Posted on April 10, 2006 at 8:56 pm #
  5. From deko:

    a complete example file wd be nice

    Posted on May 31, 2007 at 12:46 pm #
  6. From michele:

    Deko
    For a vhost or for the entire thing? :)
    Michele

    Posted on May 31, 2007 at 12:48 pm #
  7. From ironcitadel:

    “So how do you actually do it?”
    “You create a small configuration file for every site you wish to host and place them in sites-available.”
    Ooooh. That explains everything. Except how in the blue blazes do you ‘create a small configuration file’? I’ve tried like heck to understand the official Apache documentation. Those guys have their heads so far up in the clouds they couldn’t explain how to make ice cubes.
    Now if only someone could explain (in simple English) how to ‘create a small configuration file’ I’d be all set.
    Peace

    Posted on July 21, 2007 at 2:57 am #
  8. From michele:

    Ironcitadel
    Which part of the documentation are you finding confusing?
    Would it be helpful if I posted a simple howto?
    Michele

    Posted on July 21, 2007 at 2:29 pm #
  9. From Glen:

    yes please :)
    what needs to be in this configuration file and where do I place the configuration file?
    where does the actual web content go?

    Posted on July 21, 2007 at 5:17 pm #
  10. From michele:

    I’ll see if I can put something together this weekend
    Keep an eye on my main page or subscribe to the RSS feed :)

    Posted on July 21, 2007 at 5:47 pm #
  11. From ironcitadel:

    Michele, you are dreadfully hard to contact by email. Nonetheless, after spending days trying to set up a simple web site, I eventually stumbled/fumbled my way to success. I imagine the official Apache documentation is an excellent resource for an MS in CS, but for perhaps a BA in English person, not so good. Here is the step-by-step that worked for me. (I tried to send it to you via email but to no avail. )
    Of course all the rest of the Apache documentation is excellent to accomodate professional systems administrators with complex websites; multiple servers, cascading levels of access, etc. But if all you want to do is share a directory over the internet, here is how to do it.
    1. Decide which directory you want to make available. Let’s use “/shared” as an example. I want to put my vacation photos in there and make them available to download, view or even be added to or changed. (Use “chmod” and “chown” to set file permissions as needed.)
    2. Install Apache and its documentation (you may want this later if your needs grow). In Ubuntu/Debian, “apt-get install apache2 apache2-doc” . For other distros you’re on your own.
    3. The “apache2″ installation creates a directory “/etc/apache2″ and creates several files and subdirectories.
    4. The configuration file we need is “/etc/apache2/sites-available/default” so put yourself in that directory, “cd /etc/apache2/sites-available” and we’ll make a new “default” file but first make the original a backup in case disaster strikes: “sudo mv default default.bak”.
    5. Use your favorite text editor to make a new “default” file: “sudo gedit default” or “sudo vi default” for example. If you use emacs, look it up in your old WordStar manual.
    6. We’ll construct a new “default” line by line so you’ll know what we’re doing. On the first line we tell Apache we’re using a single “virtual host” . A virtual host is the term used for a specific website. You can have more than one website on a single computer (or “host”) as long as you set them up properly. Check the official Apache documentation for more information or for an explanation that’s even somewhat comprehensible, go to “http://www.devshed.com/c/a/Apache/Configuring-and-Using-Virtual-Hosts-in-Apache/” if you’re interested.
    For a simple installation you only need one and we’ll call it “*” . That’s simple enough, right? So the first line will be “NameVirtualHost *” .
    7. On the next line we tell Apache the name of our webserver like this “ServerName OurWebServer” . Of course you can use whatever name you want.
    8. The next line introduces Apache to our first virtual host like this “” . Make sure you don’t forget the angle brackets .
    9. Next we designate who will receive notices from Apache in case of errors. Use the network mail address. “ServerAdmin bigdude@hostname” . The host name is whatever you decided when you installed Linux and is usually the last half of your terminal prompt. Maybe “bigdude@hostname:~$” or something like that.
    10. Next line names the directory you plan to share to the world “DocumentRoot /shared” .
    11. The preliminaries are now done and it’s time to get specific. We tell Apache that we’re going to describe what to do with our shared directory like this: “” .
    12. Now drop down a line and indent to make things real purty. Then put “Options Index FollowSymLinks MultiViews”. The “Index” option means that if there is no “index.html” file that makes a fancy webpage (“html”, right?) then show a list of the files in the directory. “FollowSymLinks” means that if you “ln -s /morefiles here” then the symbolic directory “here” will show and let the person open it up to see “/morefiles”. “MultiViews” makes Apache display a file in the shared directory even if the person makes a bit of a mistake typing its name. At least that’s what I understand when the Apache people say “The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client’s requirements.” Your choice: sanskrit or urdu.
    13. Next line, “AllowOverrides None” . If you want to set up a website directory to have some specific characteristics different from those of the whole website (the overrides part), you make a hidden file called “.htaccess” and explain it there. Otherwise, the “None” says to not look for an “.htaccess” file.
    14. Tell Apache the manner in which you want it to apply access permissions by making the next line “Order allow,deny” . This means first apply any “allow” rules you make up and then apply any “deny” rules. For example you could say “allow anna bill charlie, deny all” and the three folks would be the only ones to get in. On the other hand, if you said “Order deny,allow” then said “deny all, allow anna bill charlie” then first everyone would be denied access and only the three folks would be allowed in. If you’re still not sufficiently confused, read “http://httpd.apache.org/docs/1.3/mod/mod_access.html” and your head will probably explode.
    15. Next line, put “allow from all” . Now don’t panic. We’re going to set up a login system next, so your weird Uncle Phil won’t get your photo collection unless you say so. But this line goes hand in hand with the previous one, so check out the link above or just go along with what we have here.
    16. We tell Apache we’re finished with this directory like this “” . Notice the “/” symbol. Critical. Don’t leave it out.
    17. Now we’ll set up the authentication or login section. On this line enter “” .
    18. Next line will be “AuthType Basic” which means we’re going to use the same username/password scheme as we normally use in Linux.
    19. On the next line type “AuthName “Restricted Files”” . You can put whatever you want in the “Restricted Files” bit. It’s what will appear on the username and password box in the browser of the person trying to login to your webserver. Maybe you like “By Invitation Only” or something like that.
    20. “AuthUserFile /etc/apache2/htpasswds” goes on the next line. This means the “htpasswds” file holds the list of the folks permitted to login to your system. We’ll create this file in a few minutes.
    21. Next we put “Require valid-user” . Ah hah! See? Here’s where we keep out Weird Uncle Phil. Only valid users can log in; those in our “htpasswds” file we defined above.
    22. We next tell Apache we’re done with this location with the line “” .
    23. Finally, we tell it we’re done with this whole shebang by putting on the next line “” . Save and you’re done here.
    24. We need to set up our valid users who can log in to your server. Go to “/etc/apache2″ and we’ll make the “htpasswds” file. (You don’t need to call it that, but whatever you call it make sure it’s the same in the defaults file we just made.) At your command prompt, type “htpasswd -c htpasswds yournamehere” and press enter.
    This tells the htpasswd program to “-c” create a file called “htpasswds” and set up the user “yournamehere” . It will ask you for a password twice, so respond with whatever you like, maybe “mypassword” or even something hard to guess. We next add all the other users one by one with the command “htpasswd htpasswds nextguy” . Note we only use the “-c” the first time, otherwise each time you’ll create a new “htpasswds” file with only one name. At least it won’t be crowded.
    25. Now we’ll test our setup like this: “sudo apachectl configtest” . If it says “Syntax OK” then you’re in like Flynn. Otherwise it will tell you which line in “default” it doesn’t like and you can fix it as needed.
    26. Here’s where you make your money. Restart Apache with the command “sudo /etc/init.d/apache2 restart” and you’re done. Happy webbing!
    In my ignorance I’ve probably made several errors here but I trust the lovely and talented Ms Neylon will set me straight. Thanks, y’all!

    Posted on July 21, 2007 at 8:38 pm #
  12. From ironcitadel:

    Ooops and Egad! Apparently my cut-and-paste left out a bunch of stuff. Here are the corrected steps. Sorry.
    8. The next line introduces Apache to our first virtual host like this “” . Make sure you don’t forget the angle brackets .
    11. The preliminaries are now done and it’s time to get specific. We tell Apache that we’re going to describe what to do with our shared directory like this: “” .
    16. We tell Apache we’re finished with this directory like this “” . Notice the “/” symbol. Critical. Don’t leave it out.
    17. Now we’ll set up the authentication or login section. On this line enter “” .
    23. Finally, we tell it we’re done with this whole shebang by putting on the next line “” .

    Posted on July 21, 2007 at 8:53 pm #
  13. From ironcitadel:

    Nope, it wasn’t my cut and paste, it’s this blog client dropping things. Michelle, it you send me your email address, I’ll send this to you in a txt attachment. Sorry about using up all your blog page.
    Peace
    Dan Coit (ironcitadel)

    Posted on July 21, 2007 at 8:55 pm #
  14. From michele:

    My email address is listed on this site on the “contact” page :)

    Posted on July 22, 2007 at 8:19 am #

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.

css.php