Part1: Setting up PHP and virtual directories

Oops – this one has apparently been lying around for quite a while. After getting a new laptop I remember doing all this work, but could not find the article anywhere because it just was still a draft version on this system. Although it probably needs a little updating (I have Mavericks on this Laptop), I thought it might be better to just get it out there.

This is the first of three articles detailing the steps needed to get a complete WordPress development environment up and running on a MacBook with the Leopard Operating System.

Apache Webserver

The Apache WebServer is already running when the MacBook is switched on. PHP is actually also preconfigured in Leopard, but need to be enabled before it is ready for use. Enabling is easy though – it is done by removing a comment sign in the /etc/apache2/httpd.conf file, around line 114. The segment from this config file with comment sign, looks like this:

# LoadModule php5_module

After removing the comment sign (#) above, you will have to restart the apache server. This can be done from a terminal window, by issuing this command:

>sudo /usr/sbin/apachectl restart

Now, do not forget to enable sharing of the web server from preferences. when that is done, you can type in http://localhost/index.html and the standard Apache page comes up nicely thereby confirming everything is working as planned. Promising :-)

See if PHP is working

Now, use terminal and go to the web server directory /Library/WebServer/Documents and use your favorite editor to create your very first php file. Call the file info.php, add the following content and save.

<?php phpinfo();?>

If you have done this correctly, you should now be able to type http://localhost/info.php in your browser and you should see the PHP logo and a comprehensive list of PHP configuration information.

Setting up Virtual hosting

I would like to have all my websites hosted in the sites directory and be able to access them easily from the browser. To get this going let’s start with creating three sub directories called brams, ltk and sonnal in the sites directory. Create an index.php file just like the one you used above in each of these to give us something to check against when we are done…

In the ideal world it would be nice being able to type in http://brams, http://sonnal and http://ltk in the browser to get access to each of these websites. Fortunately setting this up is really very simple if you know the procedure.

There are really just two steps involved in this. First of all, we need to configure the local /etc/hosts file and add each of the sites with the 127.0.0.1 address. That will redirect request for e.g. brams to localhost instead of an unresolved name.

In this case I have added a couple of lines, and the file looks like:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1 localhost
# -----------------------
# my sub sites goes here
# -----------------------
127.0.0.1 brams
127.0.0.1 ltk
127.0.0.1 sonnal
# -----------------------
255.255.255.255 broadcasthost
::1                  localhost
fe80::1%lo0    localhost

Finally we need to configure is the Apache server. The Apache configuration file is located in /etc/apache2/users and named your_username.conf. Mine is tbrams.conf and  looked like this:

<Directory "/Users/tbrams/Sites/">
   Options Indexes MultiViews
   AllowOverride None
   Order allow,deny
   Allow from all
<Directory>

This is the standard settings where Apache is told not to allow serving web pages from the sites directory. Because I want to run several sub sites in sub directories, I changed all this to:

<directory "/Users/tbrams/Sites/*/>
   Options Indexes MultiViews FollowSymLinks
   AllowOverride All
   Order allow,deny
   Allow from all
<directory>

Now we should be able to serve web pages even in subsirectories from here. Then we need to add a virtual host entry for each of these. Add the following and save:

NameVirtualHost *:80
<virtualhost *:80>
   DocumentRoot /Users/tbrams/Sites/brams
   ServerName brams
<virtualhost>
<virtualhost *:80>
   DocumentRoot /Users/tbrams/Sites/ltk
   ServerName ltk
<virtualhost>
<virtualhost *:80>
   DocumentRoot /Users/tbrams/Sites/sonnal
   ServerName sonnal
<virtualhost>

After saving the configuration file, reboot the Apache server from the terminal:

#sudo apachectl restart

Now, try it out by typing http://brams, http://ltk or http://sonnal in the browser. Your php file should display nicely in each of these cases.

What is next

Read about how to install and configure the MySQL database server in the next part of this article series.

References:

Here is one article shedding some more light on using the build in php http://www.procata.com/blog/archives/2007/10/28/working-with-php-5-in-mac-os-x-105/

One Reply to “Part1: Setting up PHP and virtual directories”

Leave a Reply

Your email address will not be published. Required fields are marked *