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:
- In the terminal, enter:
$ sudo gedit /etc/hosts - 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/:
- In the terminal, cd to
/etc/apache2/sites-available/ - Create a new file with:
$ sudo touch mysite.dev - Open that file in gedit:
$ sudo gedit mysite.dev - 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/:
- cd over to
/etc/apache2/sites-enabled/ - In the terminal, enter:
$ sudo ln -s ../sites-enabled/mysite.dev mysite.dev- Note on creating pseudo links:
$ ln -s target linkname
- Note on creating pseudo links:
Finally, restart Apache:
- $ sudo service apache2 restart
- You’re done!
See! Not so bad…
2 Comments
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.
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.devOne Trackback
[...] http://likesalmon.net/how-to-configure-apache-virtual-hosting-in-ubuntu-karmic-9-10/ [...]