January 2, 2012

XAMPP, CodeIgniter and virtual hosts

This is a short tutorial for using CodeIgniter with XAMPP and setting up virtual hosts.

For developing web applications with php I use XAMPP wich is a free and easy to install Apache distribution containing MySQL, PHP and Perl. I used CakePHP for a few years but CodeIgniter is my favorite MVC php framework. 

It's very easy to install XAMPP, you just download it and than next-next-next. CodeIgniter is just the same, download, unzip and you are ready to go. The tricky part is to use virtual hosts. If you don't like to pile up all your projects in the XAMPP htdocs directory than you need to set up some virtual hosts.

I have dowloaded CodeIgniter and I've unzipped all the files to D:\temp\mydocs. To run the application I need to modify two files (XAMPP is installed on my C drive: C:\xampp\):

1. httpd-vhosts.conf from the apache (C:\xampp\apache\conf\extrahttpd-vhosts.conf)
after
NameVirtualHost *
  <VirtualHost *>
    DocumentRoot "C:\xampp\htdocs"
    ServerName localhost
  </VirtualHost>
add this:
  <VirtualHost *>
    DocumentRoot "d:\temp\mydocs"
    ServerName mydocs.local
    <Directory "D:\temp\mydocs">
      Order allow,deny
      Allow from all
    </Directory>
  </VirtualHost>

2. the host file from Windows (C:\Windows\System32\drivers\etc\hosts)
add this to the end of the file:
127.0.0.1       mydocs.local

After restarting apache I can run my project if I type http:// mydocs .local in my browser.

You can use whatever name you want for your projects (I like to use projectname.local), you only need to add the ServerName to your hosts file. You can store your project wherever you want, specify the location in the Directory.

No comments:

Post a Comment