Set up Apache for local development/testing? Set up Apache for local development/testing? codeigniter codeigniter

Set up Apache for local development/testing?


Your Mac comes with both an Apache Web Server and a build of PHP. It's one of the big reasons the platform is well loved by web developers.

Since you're using Code Igniter, you'll want PHP 5, which is the default version of PHP shipped with 10.5. If you're on a previous version of the OS hop on over to entropy.ch and install the provided PHP5 package.

Next, you'll want to turn Apache on. In the sharing preferences panel, turn on personal web sharing. This will start up apache on your local machine.

Next, you'll want to setup some fake development URLs to use for your sites. Long standing tradition was that we'd use the fake TLD .dev for this (ex. stackoverflow.dev). However, .dev is now an actual TLD so you probably don't want to do this -- .localhost seems like an emerging defacto standard. Edit your /etc/hosts file and add the following lines

127.0.0.1    www.example.localhost127.0.0.1    example.localhost

This points the above URLs at your local machine. The last step is configuring apache. Specifically, enabling named virtual hosting, enabling PHP and setting up a few virtual hosts. If you used the entropy PHP package, enabling PHP will already be done. If not, you'll need to edit your http.conf file as described here. Basically, you're uncommenting the lines that will load the PHP module.

Whenever you make a change to your apache config, you'll need to restart apache for the changes to take effect. At a terminal window, type the following command

sudo apachectl graceful

This will gracefully restart apache. If you've made a syntax error in the config file apache won't restart. You can highlight config problems with

sudo apachectl configtest

So,with PHP enabled, you'll want to turn on NamedVirtualHosts. This will let apache respond to multiple URLs. Look for the following (or similar) line in your http.conf file and uncomment it.

#NameVirtualHost *  

Finally, you'll need to tell apache where it should look for the files for your new virtual hosts. You can do so by adding the following to your http.conf file. NOTE: I find it's a good best practice to break out config rules like this into a separate file and use the include directive to include your changes. This will stop any automatic updates from wiping out your changes.

<VirtualHost *>    DocumentRoot /Users/username/Sites/example.localhost    ServerName example.localhost    ServerAlias www.example.localhost</VirtualHost>

You can specify any folder as the DocumentRoot, but I find it convenient to use your personal Sites folder, as it's already been configured with the correct permissions to include files.


Sorry Kyle, I don't have enough cred to respond directly to your comment. But if you want to have each project be served on a different port, try setting up your virtual host config exactly like Kelly's above (minus the dns stuff) except instead of 80, give each virtualhost its own port number, assuming that you've added this port to your ports.conf file.

NameVirtualHost *<virtualhost *:80>DocumentRoot /site1/documentroot</virtualhost><virtualhost *:81>DocumentRoot /site2/documentroot</virtualhost><virtualhost *:82>DocumentRoot /site3/documentroot</virtualhost><virtualhost *:83>DocumentRoot /site4/documentroot</virtualhost>

Hope that helps :/


I also download the latest binaries for each and set them up manually. I've found it to be a painless process, as long as you know a little bit about configuring Apache.

On my development machine, I have apache setup with name-based virtual hosting. I also have a dyndns.org account which maps my development domain to my local machine. DynDNS provides a wildcard domain, and therefore using name based virtual hosts I can easily create as many sites (with separate document roots) for as many development domains as I want, all running off the one Apache instance.

e.g. Apache config for the virtual hosts might be something like

NameVirtualHost *:80<virtualhost *:80>ServerName site1.mydyndns.dyndns.orgDocumentRoot /site1/documentroot</virtualhost><virtualhost *:80>ServerName site2.mydyndns.dyndns.orgDocumentRoot /site2/documentroot</virtualhost>

This has been the quickest and easiest way for me to easily maintain many development sites on my local machine.

I hope that makes sense.

Cheers,Kelly.