Set up virtual host on mac os x
Alec Jacobson
September 06, 2011
We're using SVN to version control our group's webpage which is all php scripts. If everything were html files then making edits on a locally checked out copy would be easy since I can just open the files in a browser and check my edits. But, being PHP scripts, to properly see the sites I need a php server running which can host the local copy of the site and I can see the edits I make live. Then finally when I'm sure everything looks and executes correctly I can check in the edited version and update the version on the real server. Here's how I constructed my local setup.
Check out svn to some directory, from now on referred to as "path/to/htdocs-igl":
svn co https://private.svn.server.com/trunk/htdocs-igl path/to/htdocs-igl
Be sure that no apache servers are not running. The following in order should to do that:
sudo apachectl stop
Use ps and grep to determine if they really stopped. This should return nothing:
ps -ef | grep httpd | grep -v grep
If it returns some lines, then kill each replacing [PID] with each row's process ID:
sudo kill [PID]
If the process didn't really die then try:
sudo kill -9 [PID]
And after you think you've done enough killing, issuing this again for good measure:
sudo apachectl stop
Now that we're sure no apache servers are running. Start one:
sudo apachectl start
To check that it's working browse to http://localhost/. You should see something fully load other than "Not found, blah blah".
Now, add the following as a new line at the bottom of the file /etc/hosts
:
127.0.0.1 localigl
Now you should be able to go to http://localigl/ and see the same thing as you did with http://localhost/.
To have http://localigl/ point to your checked out version of the website, you need to edit the file /etc/apache2/users/[your mac username].conf
:
NameVirtualHost *:80
<Directory "/Users/[your mac username]/Sites/">
Options Indexes MultiViews Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
ServerName localhost
DocumentRoot /Users/[your mac username]/Sites/
</VirtualHost>
<Directory "/absolute/path/to/htdocs-igl/">
Options Indexes MultiViews Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
ServerName localigl
DocumentRoot /absolute/path/to/htdocs-igl
</VirtualHost>
Replacing, in the file name and the file contents, [your mac username]
with, uh, your mac username.
You must give execute access to parent directory and the root directory of the checked out website:
chmod 755 path/to/htdocs-igl/..
chmod 755 path/to/htdocs-igl
Finally, restart the server with:
sudo apachectl graceful
If you now go to http://localigl/ you should see a listing of the checked out website's root directory contents (or if you're using index.html, you'll see that rendered file). Since we're using PHP for everything we must enable php scripts.
Uncomment the following line in /etc/apache2/httpd.conf
to look like this:
LoadModule php5_module libexec/apache2/libphp5.so
And again restart the server:
sudo apachectl graceful
Now when you go to http://localigl/ you should see your php scripts executed and rendered.
Source This document is well written but did not enough stress that you must stop all servers (that you may have accidentally started when fooling around) before starting the steps. And it's nice to repeat his steps with exact names closer to my current set up.