How to configure Apache virtual hosting in Ubuntu Karmic 9.10

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://www.likesalmondesign.com/use-your-users-public_html-directory-to-serve-webpages/

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 /etc/apache2/sites-enabled/mysite.dev mysite.dev
    • Note: $ 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. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

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>