ubuntu

Set Up Virtual Host For Local Development With Ubuntu

Wednesday, September 24th, 2008

ubuntulogo1

Quick and easy virtual host configuration. This is for development on a local machine only, not for use in the wild. You can check out the ubuntu documentation here.

Assuming you have the following all set up, if not check out the link above to get set up.

  • Apache
  • MySQL
  • PHP

I keep all of my projects in my /home folder under /webroot. Its easier to backup and /home lives on its own partition. I’m starting a new project called, ‘example.com’. I like to work locally and use version control to update the live site. My local copy will be called ‘devexample.com’.

When we’re done, if we type in ‘http://devexample.com’ into our browser it will read the local version.

The full path to my new dev site will be:
/home/darren/webroot/example

Lets create our directories:
mkdir webroot
cd webroot
mkdir example

Set up some read/write. Again this is not the way to do it on a live server:
sudo chmod 777 -R webroot

Fire up your text editor with root privileges.
gksude gedit

Enter in your new virtual host information:

<VirtualHost *>
	ServerName devexample.com
	ServerAlias www.devexample.com
	DocumentRoot /home/darren/webroot/example/
	DirectoryIndex index.php
	<Directory />
		Options Indexes FollowSymLinks MultiViews
		AllowOverride all
		Order deny,allow
		Deny from all
	      	Allow from 127.0.0.0/255.0.0.0 ::1/128
	</Directory>
</VirtualHost>

Again this is not a safe virtual host configuration for a live site.

Save this file under /etc/apache2/sites-available

Now we are going to make this site active in the terminal type:
sudo a2ensite mysite

Almost there, now lets edit the host file on your local machine. Use your text editor with root privileges and open /etc/hosts

Append your new site ‘example.com’ to this list.

Restart apache:
sudo /etc/init.d/apache2 restart

Put a test file in your new root directory and Navigate your browser to nhttp://devexample.com.

That’s it.