Set up Apache for local development/testing?


Question

I've been impressed by the screencasts for Rails that demonstrate the built-in web server, and database to allow development and testing to occur on the local machine. How can I get an instance of Apache to execute a project directory as its DocumentRoot, and maybe serve up the files on port 8080 (or something similar)?

The reason why I'm asking is because I'm going to be trying out CodeIgniter, and I would like to use it for multiple projects. I would rather not clutter up my machine's DocumentRoot with each one. Suggestions on how to do database migrations are also welcome.


Thank you for your responses so far. I should clarify that I'm on Mac OS X. It looks like WAMP is Windows-only. Also, XAMPP looks like a great way to install Apache and many other web tools, but I don't see a way of loading up an instance to serve up a project directory. Mac OS X has both Apache and PHP installed - I'm just looking for a way to get it to serve up a project on a non-standard port.

I just found MAMP Pro which does what I want, but a more minimalist approach would be better if it's possible. Does anyone have a httpd.conf file that can be edited and dropped into a project directory?

Also, sorry that I just threw in that database migration question. What I'm hoping to find is something that will enable me to push schema changes onto a live server without losing the existing data. I suspect that this is difficult, and highly dependent on environmental factors.

1
12
12/30/2011 1:22:24 AM

Accepted Answer

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.

26
2/27/2019 4:50:57 PM

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 :/


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon