23.6.12

Local Web Server for Lion: Apple Support Communities

Local Web Server for Lion: Apple Support Communities

1 2 3 4 Previous Next
etresoft Level 6 Level 6 (18,165 points)
Jul 27, 2011 7:47 AM 
 
Since the User Tips aren't ready yet, I thought I would go ahead and post my definitive guide to getting a local web server running on Lion. This is meant to be a development platform so that you can build and test your sites locally, then deploy to an internet server. I have instructions for configuring the Apache, PHP, MySQL, and Perl. Now that Lion is released, I wanted to consolidate and update all of the random bits and pieces here on Apple Support Communities.

Requirements:
  1. Basic understanding of Terminal.app and how to run command-line programs.
  2. Basic understanding of web servers.
  3. Basic usage of vi. You can substitute nano if you want.

Optional:
  1. Xcode is required for the MySQL driver and for adding PHP modules.

Lines in bold are what you will have to type in at the Terminal.
Replace <your local host> with the name of your machine. Ideally, it should be a one-word name with no spaces or punctuation. It just makes life easier.
Replace <your short user name> with your short user name.

Here goes... Enjoy!

Lion no longer creates personal web sites by default. To create one manually, enter the following:
mkdir ~/Sites
echo "<html><body><h1>My site works</h1></body></html>" > ~/Sites/index.html.en

PHP is not enabled in Lion. To enable it, do:
sudo vi /etc/apache2/httpd.conf

Uncomment line 111 that reads:
#LoadModule php5_module libexec/apache2/libphp5.so
to
LoadModule php5_module libexec/apache2/libphp5.so

Edit the launchd config file for Apache:
sudo vi /System/Library/LaunchDaemons/org.apache.httpd.plist 

Restart Apache:
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

To turn on Apache, go to System Preferences > Sharing and enable Web Sharing.

NOTE: There appears to be a bug in Lion for which I haven't found a workaround. If web sharing doesn't start, just keep trying.
This might help. Might not. Remove the following from /System/Library/LaunchDaemons/org.apache.httpd.plist:
         <string>-D</string>
         <string>WEBSHARING_ON</string>

In Safari, navigate to your web site with the following address:
http://<your local host>/

It should say:
It works!
Now try your user home directory:
http://<your local host>/~<your short user name>

It should say:
My site works
Now try PHP. Create a PHP info file with:
echo "<?php echo phpinfo(); ?>" > ~/Sites/info.php

And test it by entering the following into Safari's address bar:
http://<your local host>/~<your short user name>/info.php

You should see your PHP configuration information. This will be important for setting up MySQL later.

Download MySQL from a local mirror. You want the Mac OS X ver. 10.6 (x86, 64-bit), DMG Archive. Open the archive mysql-5.5.14-osx10.6-x86_64.dmg. Install only the mysql-5.5.14-osx10.6-x86_64.pkg package. Ignore everything else.

Create the launchd config file for MySQL:
sudo vi /Library/LaunchDaemons/com.mysql.mysql.plist 

Use the following content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>com.mysql.mysqld</string>
    <key>ProgramArguments</key>
    <array>
    <string>/usr/local/mysql/bin/mysqld_safe</string>
    <string>--user=mysql</string>
    </array>
  </dict>
</plist>

Create a config file for MySQL that matches the Apple PHP build:
sudo vi /etc/my.conf 

Use the following content:
[client]
socket=/var/mysql/mysql.sock
[mysqld]
socket=/var/mysql/mysql.sock

Start MySQL:
sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist

Extract the archive with:
tar zxvf DBD-mysql-4.019.tar.gz

Move into the directory:
cd DBD-mysql-4.019

First, fix the MySQL client library. (credit)

For Lion, type:
sudo install_name_tool -id /usr/local/mysql-5.5.14-osx10.6-x86_64/lib/libmysqlclient.18.dylib /usr/local/mysql-5.5.14-osx10.6-x86_64/lib/libmysqlclient.18.dylib

Next, build DBD::mysql with:
perl Makefile.PL --mysql_config=/usr/local/mysql/bin/mysql_config --testsocket=/var/mysql/mysql.sock --testuser=root
make
make test
sudo make install
Now that MySQL is tested and installed, don't forget to set a root password and configure your users.

If you want to add modules to PHP, I suggest the following site. I can't explain it any better. I had planned to update those instructions for Lion and just give him credit, but he beat me to it.
MacBook 2007 (white), Mac OS X (10.7), + iMac 27" + iPad + MacBook Pro