You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

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

Aug 11, 2011 2:04 AM in response to etresoft

Hi,


Thanks for the write-up, very helpful. However, it's this line that bothers me: "To turn on Apache, go to System Preferences > Sharing and enable Web Sharing."


I don't like the fact that in orde to let Apache run, I must turn on Web sharing. I'm not always on local networks in which I know all computers, and basically, I'm giving everyone free access to my Sites directory with Web sharing turned on (and as a web developer, there are quite some sites there). Is there any way of working around this? Maybe I could fix this in my Apache config, if so, do you have any suggestions on how to do this?


As an aside: On Snow Leopard, in my experience the two were not connected... And in fact, on Lion, my Apache setup worked fine with Web sharing turned off, until I turned Web sharing on and off once. After that, apache started turning on/off on startup based on the Web sharing settings.

Aug 11, 2011 7:38 AM in response to JollyGewd

"Web Sharing" is just a Mac euphemism for "apache is running". Whether or not you are actually sharing your sites with the web is controlled by the settings in the httpd.conf files in /etc/apache2 and /etc/apache2/users. If you just want to create a site for local development or a local intranet then you have to add appropriate directives to deny the service to everyone except the internet addresses you want to have access. see http://httpd.apache.org/docs/2.2/mod/directives.html.


as a rule, it's not too much of a problem. even if you leave your testing site open to the web, it's unlikely that anyone will know it's there. If you have someone on your local web who's snooping around it's easy enough to find, of course, but I believe that OS X has 'show directories' turned off by default so they are not likely to find any of your web files except the index page and any files linked off that.

Aug 11, 2011 8:11 AM in response to twtwtw

Ah, that makes sense, thanks for the explanation! So basically, the checkbox "Web sharing" does nothing more than turning Apache on and off?


Concerning the default settings for indexes: in /private/etc/apache2/httpd.conf.default I see this:


<Directory "/Library/WebServer/Documents">

...

Options Indexes FollowSymLinks MultiViews

...

</Directory>


Apparently indexes are shown by default in the web directory, but I'm on a Macbook Pro which had Snow Leopard on it originally, so I'm not sure if these settings are the same for new Lion Macs.

Aug 11, 2011 8:29 AM in response to JollyGewd

Basically, yeah. the 'Web Sharing' checkbox is a way for people without too much technical knowledge to publish a webpage quickly and seamlessly. KInd of an internet 'easy-bake oven'... as far as the defaults go, well.. I set up my webserver so long agio that I can't remember what the files looked like originally. maybe indexes are on by default? I'll have to check my Lion partition (which I haven't yet modified) and see what it's conf files say.

Aug 11, 2011 2:31 PM in response to JollyGewd

JollyGewd wrote:


I'm not always on local networks in which I know all computers, and basically, I'm giving everyone free access to my Sites directory with Web sharing turned on (and as a web developer, there are quite some sites there). Is there any way of working around this? Maybe I could fix this in my Apache config, if so, do you have any suggestions on how to do this?

That is correct. You could adjust your Apache config, or just turn on your Firewall. This is one of the few situations where you would actually be using a Firewall for its intended purpose - to facilitate services for authorized users (yourself) while denying others. The MacOS X built-in Firewall is pretty anaemic, so you have to shut off all sharing services. The real ipfw firewall is still available via the command line or 3rd party GUI configuration tools.


As an aside: On Snow Leopard, in my experience the two were not connected... And in fact, on Lion, my Apache setup worked fine with Web sharing turned off, until I turned Web sharing on and off once. After that, apache started turning on/off on startup based on the Web sharing settings.


You may have been running a 3rd party build of Apache such as MacPorts or XAMPP. Another Apache installation would not be connected to the built-in Web Sharing preference setting. I prefer using the built-in Apache because there is less included with it. The MacPorts and XAMPP Apache builds are much like default Linux installations where they include every module under the sun. Then, when you go to deploy on a more restrictive sharing hosting provider, you could find that some of the modules your site depends on are not available. That is a bad time to make such a big discovery. All shared hosting sites will have some restricted subset of Apache modules available. It is better to know about such things and work around them in development.

Aug 12, 2011 1:14 AM in response to etresoft

etresoft wrote:

That is correct. You could adjust your Apache config, or just turn on your Firewall. This is one of the few situations where you would actually be using a Firewall for its intended purpose - to facilitate services for authorized users (yourself) while denying others. The MacOS X built-in Firewall is pretty anaemic, so you have to shut off all sharing services. The real ipfw firewall is still available via the command line or 3rd party GUI configuration tools.

Ok, that's good to know. For now I just changed "Listen 80" to "Listen 127.0.0.1:80" in httpd.conf, this seems to do the trick for me. At first it didn't seem to work though, but apparently I had a lot of httpd processes running, some of which kept running even after restarting apache from Terminal (probably because I was goofing around with launchctl load/unload and starting/stopping using apachectl). After a fresh restart, other computers on the local network couldn't get in anymore.

etresoft wrote:


You may have been running a 3rd party build of Apache such as MacPorts or XAMPP. Another Apache installation would not be connected to the built-in Web Sharing preference setting. I prefer using the built-in Apache because there is less included with it. The MacPorts and XAMPP Apache builds are much like default Linux installations where they include every module under the sun. Then, when you go to deploy on a more restrictive sharing hosting provider, you could find that some of the modules your site depends on are not available. That is a bad time to make such a big discovery. All shared hosting sites will have some restricted subset of Apache modules available. It is better to know about such things and work around them in development.

Good point about not having all kinds of modules enabled on a development machine. I do have MacPorts and use it quite frequently, but my apache has always been the OS X default (2010 Macbook Pro which originally had Snow Leopard on it). After upgrade to Lion, PHP was disabled in httpd.conf and local PHP scripts stopped working, which also indicates that I've been using the default apache all the time.

Aug 20, 2011 1:40 PM in response to voigtstr

There are a couple of tutorials for setting up local virtual hosts on the web: google 'OS X Leopard virtual host' (you don't want the Tiger versions because they rely on Net Info Manager). The basics are as follows, and this should work on Leopard through Lion. This will set up a host at the URL local.dev.vhost that points to a folder at /Users/yourname/Sites/virtualHostFolder:


Preparations:

  • download TextWrangler (you can use another text editor if you want, but everything I say will be geared towards TW)
  • make a current time machine backup - if you goof something up it will often be easier to start again from scratch than try to figure out what went wrong
  • open the following files in TextWrangler
    1. /etc/hosts
    2. /etc/apache2/httpd.conf
    3. /etc/apache2/extra/httpd-vhosts.conf
  • read the docs at http://httpd.apache.org/docs/2.2/vhosts/ (you'll be making a name-based virtual host, but make sure you get the overview as well as the details)


Setting up a virtual host:

  • in /etc/hosts, add a line like the following at the end of the file (this informs the system that there should be a location on the local machine with the name we want): 127.0.0.1 local.dev.vhost
  • in the httpd.conf file, search for 'Virtual hosts' and make sure the include line is uncommented by deleting the pound sign (#) at the start of the line. The line you want should be near the end of the document: Include /private/etc/apache2/extra/httpd-vhosts.conf.
  • edit the httpd-vhosts.conf file. first, comment out the example virtual hosts with pound signs (#). Or just delete them, there's a folder of the original files in the apache2 folder you can refer to at need. Add two new host specifiers as follows (this is minimal - read the apache documentation documentation for more options.

# This allows localhost to still find your main sites folder

<VirtualHost *:80>

DocumentRoot "/Users/yourname/Sites"

ServerName localhost

</VirtualHost>


# This is your virtual host

<VirtualHost *:80>

DocumentRoot "/Users/yourname/Sites/virtualHostFolder"

ServerName local.dev.vhost

</VirtualHost>

save everything, then restart apache.


You may also want to add a directory block for the virtual host folder to control access or set up other options. e.g. the following, which should deny access from everywhere except localhost and your site name:


<Directory "/Users/yourname/Sites/virtualHostFolder">

Order Deny,Allow

Allow from 127.0.0.1

Deny from all

</Directory>


You can put this in the httpd-vhosts.conf file or in the file at /etc/apache2/users/yourname.conf, doesn't matter.

Aug 25, 2011 12:17 PM in response to etresoft

To fix the problem with Web Sharing not starting via the Sharing Preferences panel, I found that the launch daemon is disabled out of the box (at least mine was).


Edit

/System/Library/LaunchDaemons/org.apache.httpd.plist
and remove the following lines:


    <key>Disabled</key>     <false/>


Note, it's important to actually remove these, and not just set it to "false". Surprisingly that wouldn't work.

Aug 26, 2011 11:59 AM in response to Andrew Vit

The launchd man page says:


-w Overrides the Disabled key and sets it to false. In

previous versions, this option would modify the

         configuration file. Now the state of the Disabled key
         is stored elsewhere on-disk.

The "elsewhere" is /var/db/launchd.db.


I suspect you were just as confused as I was about this bug in Lion. You couldn't get the web server to start so you tried to fix the config files. Once you got the web server started, you logically assumed that the last fix you made did the trick - my thoughts exactly. I think it was a bug in the mDNSResponder. If you keep clicking the web sharing button, even with the Diabled key in there, it will eventually start up. Then it will work normally 4-5 times in a row. Then it will fail 7 times, etc.


Supposedly, performing the same action over and over again, expecting a different result, is one of the traits of insanity. I guess you have to be insane to run a web server on Lion 🙂.

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 Account.