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.localhost
127.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 virtual host 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