How to configure Apache virtual hosting in Ubuntu Karmic 9.10

Edited 3/8/11

Before you do anything, back up your original config files. I used to just create a copy of the files with the suffix “.bak” (sudo cp example.conf example.conf.bak), which works, but creates a lot of clutter. Now I use a great little utility called etckeeper which automatically places all files in /etc under version control using Bazaar. It also automatically performs a commit each day if you forget to. You can get etckeeper by typing this in the terminal: sudo apt-get install etckeeper.

Update, 3/29/10

I just had to do this from scratch after a computer disaster, and I found an omission. Before you get started on this business you need to do what this post says:

http://likesalmon.net/use-your-users-public_html-directory-to-serve-webpages/

Better yet, use this one:

http://likesalmon.net/serve-web-pages-from-your-home-directory-in-ubuntu-10-04/

And we’re off

Now on to virtual hosts. The first thing you need to do is edit /etc/hosts so your computer knows where to look for your website:

  1. In the terminal, enter: $ sudo gedit /etc/hosts
  2. In gedit, add the following line to the hosts file: 127.0.0.1 mysite.dev
    • Note: you can use whatever name you want for your virtual hosted site. I like to use the suffix .dev because it won’t conflict with sites on the actual internet.
    • Note: 127.0.0.1 is the same ip address that is assigned to localhost in that file. If yours is different, use that one.

Next add your site to /etc/apache2/sites-available/ and then link to that file in /etc/apache2/sites-enabled/:

  1. In the terminal, cd to /etc/apache2/sites-available/
  2. Create a new file with: $ sudo touch mysite.dev
  3. Open that file in gedit: $ sudo gedit mysite.dev
  4. Add following to that file:
    <VirtualHost *:80>
    ServerName mysite.dev
    ServerAlias mysite.dev
    ServerAdmin myaddress@email.com
    DocumentRoot /home/yourusername/public_html/mysite/
    </VirtualHost>

Add a pseudo link to that file in /etc/apache2/sites-enabled/:

  1. cd over to /etc/apache2/sites-enabled/
  2. In the terminal, enter: $ sudo ln -s ../sites-enabled/mysite.dev mysite.dev
    • Note on creating pseudo links: $ ln -s target linkname

Finally, restart Apache:

  1. $ sudo service apache2 restart
  2. You’re done!

See! Not so bad…

This entry was posted in Apache, Solutions, Ubuntu, Virtual Hosts and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted March 8 2011 at 1:55 am | Permalink

    just a comment: when trying to link to sites enable, apache would in my case error out with something like: “error, 2 many simbolic links”, so the solution was to copy definitions from /sites-avaliable to /sites-enabled.

    Thanks for the guide.

  2. Posted March 8 2011 at 1:24 pm | Permalink

    Thanks for commenting brotosaurusrex! I haven’t had that exact problem, but I have run into weirdness when I try to create the pseudo link from any directory besides /sites-enabled. No idea why. So I always cd to /sites-enabled first and then run:

    ln -s ../sites-available/example.dev example.dev

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>