Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Local Web Server for Lion

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:

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


Download the MySQL Perl driver.


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

Posted on Jul 27, 2011 7:47 AM

Reply
49 replies

Nov 2, 2011 7:04 PM in response to Greg Parks

Greg Parks wrote:


[Wed Nov 02 17:08:14 2011] [error] [client 127.0.0.1] client denied by server configuration: /usr/Library

It looks like you are trying to serve files out of /usr/Library. That's not right. The best thing to do is serve them out of the Sites folder in your home directory. In some circumstances, you may want to use the system location at /Library/WebServer/Documents, but I don't like to.

Nov 2, 2011 7:11 PM in response to etresoft

Here's what I have in /private/etc/apache2/extra/httpd-vhosts.conf :


NameVirtualHost *:80


<VirtualHost *:80>

DocumentRoot "/Library/WebServer/Documents"

</VirtualHost>



<VirtualHost *:80>

DocumentRoot "Library/WebServer/Documents/mysiteone/"

ServerName www.mysiteone.dev

ErrorLog "/private/var/log/apache2/mysiteone.dev-error_log"

CustomLog "/private/var/log/apache2/mysiteone.dev-access_log" common

</VirtualHost>


Do I also need to specify the DocumentRoot somewhere in /private/etc/apache2/httpd.conf ?

Nov 2, 2011 7:42 PM in response to etresoft

I just tried that and got the same error. I searched httpd.conf for "usr" and found this:


# Configuration and logfile names: If the filenames you specify for many

# of the server's control files begin with "/" (or "drive:/" for Win32), the

# server will use that explicit path. If the filenames do *not* begin

# with "/", the value of ServerRoot is prepended -- so "log/foo_log"

# with ServerRoot set to "/usr" will be interpreted by the

# server as "/usr/log/foo_log".



#

# ServerRoot: The top of the directory tree under which the server's

# configuration, error, and log files are kept.

#

# Do not add a slash at the end of the directory path. If you point

# ServerRoot at a non-local disk, be sure to point the LockFile directive

# at a local disk. If you wish to share the same ServerRoot for multiple

# httpd daemons, you will need to change at least LockFile and PidFile.

#

ServerRoot "/usr"


I then tried this in :


<VirtualHost *:80>

DocumentRoot "/Library/WebServer/Documents"

</VirtualHost>



<VirtualHost *:80>

DocumentRoot "/Library/WebServer/Documents/mysiteone"

ServerName www.mysiteone.dev

ErrorLog "/private/var/log/apache2/mysiteone.dev-error_log"

CustomLog "/private/var/log/apache2/mysiteone.dev-access_log" common

</VirtualHost>


AND IT WORKS!!!


Thank you so much for your help! I am quite relieved!

Jan 29, 2012 3:33 AM in response to etresoft

I have a problem with my localhost. I get a 403 Forbidden error when I'm trying to access a sub-directory into my localhost. I read this post and so I tried to modify my document Root because I had it moved out of the Library. But I'm still getting this error even when I'm just trying to access the php info page. I'm not using virtual hosts. I tried but I got lots of errors and so I had to replace everything. So anyway how can I get my localhost up and running?

Jan 29, 2012 1:07 PM in response to etresoft

I just saw the error logs and the line says "Client denied by server configuration /Users/myusername/WebServer" but this was my last document root before I replaced the httpd.conf file with the original file (I just uncommented the php load module line and nothing else). I don't know if this is useful but I'm sure I didn't change anything else

Jan 30, 2012 7:45 AM in response to eaglex690

We are all experts here - with varying levels of expertise. It would be best to avoid hacking up the Apache config files until you better understand them. Try to put things back the way they were originally and/or restore from backup. This might be an opportunity to learn some Terminal commands like chown and chmod. I can't really tell you what to do because I don't know what you have done. The document root must be world readable and executable. All of your web server files must be readable too. Scripts must be executable.

Local Web Server for Lion

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.