Creating Virtual Hosts in Mac OS X

The default location of web root of Apache is “/Library/WebRoot/Documents” so every time you visit “http://localhost/” you are actually querying this folder, but you do not have permission to modify this directory freely such as creating subfolders or files. What Mac OS provides you is the “Sites” folder, both of the two can be found easily via System Settings:

This kind of setting can become uncomfortable to developers sometimes, especially when I tried to develop PHP in Eclipse. Hopefully Apache provides a technique named “Virtual Host”, which is well described here.

Take me for example, I want to have a virtual host “http://wanderseeme/” which uses my “Sites” folder directly so I do not need to type “http://localhost/~myUserName/” any more.

Three files you need to modify step by step to implement this, firstly the “hosts”:

/private/etc/hosts

I added a line “127.0.0.1 wanderseeme” in my case.

The next file is “httpd.conf” I mentioned quite a lot before:

/private/etc/apache2/httpd.conf

you have to uncomment the line below “Virtual hosts”:

Include /private/etc/apache2/extra/httpd-vhosts.conf

yes then the file “httpd-vhosts.conf” mentioned above, add something like this:

 

 

   DocumentRoot “/Users/myUserName/Sites/”

   ServerName wanderseeme

 

Restart httpd, you will get what you want.

 

Advertisements

Quick Start of PHP Developing for Mac OS X

Recently I started a project of PHP. I’ve learnt a little about PHP with Evan serveral monthes ago, and it turned out that I’m really easy to give up while Evan is really an intelligent person with hardworking. Anyway, I returned back to PHP again, and I am much more ambiguous now since I finally have a project to work on and I’m quite free in these days.

I can not give an acurate defination for PHP since I’m also a newbee to it, but what I’m sure is that it is a kind of scripting language which is used to develop dynamic webpages, runs at server-side, just like ASP.NET and JSP. Its homepage is http://www.php.net/. PHP always appear with Apache, MySQL, sometimes even other scripting languages like Python or Perl, so there is also packages that wraps them up, such as AMPPS and MAMP.

When it comes to Mac OS X, you feel just so cool since Mac OS has already installed Apache and PHP, but you have to turn PHP on since it’s disabled by default (Apache is always enabled so that you “Sites” can works properly). You should have a concept that PHP (command line “php”) is kind of a module in Apache (command line “httpd” or “apachectl”). Their versions in Mac OS X Lion 10.7.2 are PHP 5.3.6 and Apache/2.2.20 respectively.

Modify config file “httpd.conf” of Apache to turn PHP on. It’s location is:

/private/etc/apache2/httpd.conf

drag it to your Text Edit and find a commented line:

#LoadModule php5_module        libexec/apache2/libphp5.so

uncomment it turn PHP on (if you do not have permission to modify this file, just resave it to another proper location, modify that new one, then drag the new one back and click “Authenticate” when prompted to replace the protected one), then you have to restart you Apache to take effect:

sudo httpd -k restart

Installing MySQL is also very easy since there is a binary for Mac OS X in http://dev.mysql.com/downloads/, all you need to install are MySQL Community Server and an optional MySQL Workbench (GUI Tool) which is quite helpful and easy to use for a beginer.

Create you webpages in your home’s “Sites” folder (“~/Sites”). For example a .php file “1.php”, use Text Edit or any other good text editors to create a new file and type:

    $link = mysql_connect(“localhost”, “root”, “123456”);

    if (!link)

        echo “Connecting failed.”;

    else

        echo “Connected.”;

    mysql_close();

?>

save it to “1.php”, turn on Safari and type “http://localhost/~YourHomeFolderName/1.php”, you will see the result.

If there are warnings, like on my Mac, it’s because the default location for mysql.sock (“/tmp/mysql.sock”) is different to that php thinks (“/var/mysql/mysql.sock”), modify it here:

/private/etc/php.ini

same trick as you did to modify httpd.conf of Apache, “resave and drag back”, find:

pdo_mysql.default_socket=/var/mysql/mysql.sock

and replace it to:

pdo_mysql.default_socket=/tmp/mysql.sock

For now everything will be fine, then you can develop PHP webpages now. After all, expert for these configurations of Apache and PHP which won’t be concerned again afterwards, developing with PHP is really easy and quick.

 

Here are also some pages you may refer to:

Enabling PHP in Mac OS X 10.5

Stopping and Restarting – Apache HTTP Server

Mac OS X Server 10.4: Issues connecting PHP to MySQL