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

Mar 22, 2012 9:08 PM in response to etresoft

@ etresoft


Thanks for this article. Trying to set up vhosts myself.


As to your comment on why the web sharing will not start up in Lion, someone here found out why it got broken and how to fix it. I've simplified the instructions:


After upgrading to Lion, some users lost the ability to turn on the web sharing service in OS X. This was partially caused by the Apache httpd.conf file referencing to a non-existing file. PHP was upgraded to 5.3 in Lion, but the old httpd.conf keeps referencing 5 lib file. You could verify this by following these instructions below:

  1. 1.Go to System Preferences and try to start Web Sharing
  2. 2.It will turn yellow for a split of a second and then back to grey
  3. 3.Start Console and look at the system log
  4. 4.You could see something like this:
    dyld: Library not loaded: /usr/lib/libpq.5.dylib
    Referenced from: /usr/sbin/httpd
    Reason: image not found
    Job appears to have crashed: Trace/BPT trap: 5
    Throttling respawn: Will start in 10 seconds
  5. 5.Apache is referencing a file libpq.5.dylib, but that file doesn’t exist!
  6. 6.However a file libpq.5.3.dylib exists at that location instead
  7. 7.All you have to do to make the missing file link to the new version 5.3 file
  8. 8.Type this in Terminal: sudo ln -s /usr/lib/libpq.5.3.dylib /usr/lib/libpq.5.dylib
  9. 9.Restart Web Sharing in System Preferences
  10. 10.You should be good to go!

I'm taking no credit for this fix. I just dug it out buried deep in this discussion forum.

Mar 26, 2012 9:57 AM in response to etresoft

can anyone tell me what i'm actually supposed to do to carry out this part of the instrucitons please?:


Edit the launchd config file for Apache:

sudo vi /System/Library/LaunchDaemons/org.apache.httpd.plist


i ignored it and carried on and typed

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

but that gave

launchctl: Error unloading: org.apache.httpd

so i guess it'd down to me not editing the launchd file for Apache, whatever that means


thanks.

Mar 26, 2012 10:33 AM in response to john cummin1

I think that was part of my trying to get around a bug in Lion websharing. Sometimes it doesn't want to start when you click on the Web Sharing box in System Preferences. Just keep clicking until it starts.


You can ignore that line and

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist


I never did figure out what was wrong. I just checked a vanilla 10.7.3 machine and the bug is still there. Plus, when I wrote this, the User Tips weren't enabled yet. They are now, so I need to re-do this as a real user tip. No time like the present, eh?

Mar 27, 2012 4:51 PM in response to etresoft

Dear All,


I'm quite a beginner in apache / PHP settings and developpment, but still, here is my problem.

I want to settup a apache / PHP / postgresql framework.


First I've installed postgresql (9.1) in /Library/PostgreSQL/9.1/bin/ and check I can create and use DB : OK


I've set up my /etc/apache2/http.conf to activate PHP (uncommenting corresponding line)

I've set up my /etc/php.ini (/usr/libexec/libphp5.so)


Unfortunately, with this configuation,I wasn't able to use postgresql.


The best way to sove my problem was to recompile PHP using the -with-pgsql option, in order to make PHP deals with postgres.

So I've compiled PHP 5.4.0 using the following configuration :

./configure --with-pgsql=/Library/PostgreSQL/9.1/bin/ --prefix=/usr --with-apxs2=/usr/bin/apxs

I've then check I can access to my postgres DB with a simple PHP code : OK


Then launching apache via web sharing (from syst. pref.) and opening a brief PHP file (one line : <?PHP phpinfo() ?>).

The web page indicate that PHP 5.3.8 was launched, not PHP5.4.0. (also in /var/log/apache2/error_log)


I though I could solve my probleme by updating the apache2 to 2.4.1

So I updated apache 2.2.1 (build in release of Lion) to 2.4.1, so as it could integrate the newly installed PHP 5.4.0 (and not the PHP 5.3.8).

I used the following configuration :

./configure --prefix=/usr --enable-modules=all --enable-mods-shared=all


But this was useless, since the apache launched by syst.pref web sharing click boxe is still apache 2.2.1 (set in /usr/sbin) but not apache 2.4.1 :


solath:bin meurice$ /usr/sbin/httpd -v


Server version: Apache/2.2.21 (Unix)

Server built: Nov 15 2011 15:12:57


solath:bin meurice$ /usr/bin/httpd -v


Server version: Apache/2.4.1 (Unix)

Server built: Mar 27 2012 20:54:26



I probably missing a configuration step, but I can't figure it out.

I've tried to modify the file /System/Library/LaunchDaemons/org.apache.httpd.plist, replacing "/usr/sbin" by /usr/bin", but it make the activation of apache via syst.pref. web sharing click box failed.


I've tried to copy the libphp5.so comming with PHP 5.4.0 to /usr/libexec/apache2/

but then I also failed to launch apache2


I would be very grateful if you could give me some clues to succed, because yet, I didn't find anything in google.


I would rather not use any MAMP or mac port installation, because I would prefer to clearly understand what I'm doing. The benefit will be greater for my knowledges, and moreover I'm pretty sure it will be very helpful since I want to step into PHP developpement, and understanding the way apache / PHP are connected could be helpful in debugging.



Bests.

Mar 27, 2012 6:39 PM in response to BillBaroud

BillBaroud wrote:


I want to settup a apache / PHP / postgresql framework.


Why PostgreSQL? MySQL is so easy to install and will do everything short of decent spatial extensions. If you really need PostgreSQL, OK, but there are far fewer people to help. I've never installed it, so all I can do is work around that fact.


Unfortunately, with this configuation,I wasn't able to use postgresql.


Why not? What errors did you get?


The best way to sove my problem was to recompile PHP using the -with-pgsql option, in order to make PHP deals with postgres.


Are you sure about that? The stock PHP seems to include both PDO and standard PostgreSQL drivers. You've just tripled (at least) the complexity of your problem.


So I've compiled PHP 5.4.0 using the following configuration :

./configure --with-pgsql=/Library/PostgreSQL/9.1/bin/ --prefix=/usr --with-apxs2=/usr/bin/apxs

I've then check I can access to my postgres DB with a simple PHP code : OK


Then launching apache via web sharing (from syst. pref.) and opening a brief PHP file (one line : <?PHP phpinfo() ?>).

The web page indicate that PHP 5.3.8 was launched, not PHP5.4.0. (also in /var/log/apache2/error_log)


Well, if you really, really want to run PostreSQL, and you really, really, really want to run a newer version of PHP, there are ways to accomplish that. Of course very few people do such things.


I though I could solve my probleme by updating the apache2 to 2.4.1

So I updated apache 2.2.1 (build in release of Lion) to 2.4.1, so as it could integrate the newly installed PHP 5.4.0 (and not the PHP 5.3.8).

I used the following configuration :

./configure --prefix=/usr --enable-modules=all --enable-mods-shared=all


OK. Whoa. Time out. Full stop.


But this was useless, since the apache launched by syst.pref web sharing click boxe is still apache 2.2.1 (set in /usr/sbin) but not apache 2.4.1 :


solath:bin meurice$ /usr/sbin/httpd -v


Server version: Apache/2.2.21 (Unix)

Server built: Nov 15 2011 15:12:57


solath:bin meurice$ /usr/bin/httpd -v


Server version: Apache/2.4.1 (Unix)

Server built: Mar 27 2012 20:54:26



I probably missing a configuration step, but I can't figure it out.

I've tried to modify the file /System/Library/LaunchDaemons/org.apache.httpd.plist, replacing "/usr/sbin" by /usr/bin", but it make the activation of apache via syst.pref. web sharing click box failed.


No más ! No más !


I've tried to copy the libphp5.so comming with PHP 5.4.0 to /usr/libexec/apache2/

but then I also failed to launch apache2


And now, we reinstall the operating system 😟


I would be very grateful if you could give me some clues to succed, because yet, I didn't find anything in google.


I would rather not use any MAMP or mac port installation, because I would prefer to clearly understand what I'm doing.


After reinstalling the OS, get back to step where you couldn't connect to your PostgreSQL database with PHP. When something doesn't work, please ask first. Don't go corrupting your OS.

Mar 27, 2012 11:47 PM in response to etresoft

First many thanks for considering my question, and for your answer.


BillBaroud wrote:


I want to settup a apache / PHP / postgresql framework.




Why PostgreSQL? MySQL is so easy to install and will do everything short of decent spatial extensions. If you really need PostgreSQL, OK, but there are far fewer people to help. I've never installed it, so all I can do is work around that fact.



OK, I really need postgreSQL.



Unfortunately, with this configuation,I wasn't able to use postgresql.


Why not? What errors did you get?



I Got this error :


PHP Fatal error: Call to undefined function pg_connect()


the pg_connect() is the PHP function to make a connection to the SGDB, like mysql_connect but for PostgreSQL.





Well, if you really, really want to run PostreSQL, and you really, really, really want to run a newer version of PHP, there are ways to accomplish that. Of course very few people do such things.


Did the ways I follow were bad ? Could you please give me the good way to upgrade apache for macosx then ?



After reinstalling the OS, get back to step where you couldn't connect to your PostgreSQL database with PHP. When something doesn't work, please ask first. Don't go corrupting your OS.


OK no worry about that. My system work just fine, I didn't make anything undoable. All I want is to upgrade apache so it integrate the newer release of PHP (5.4.0), not the oldest one (5.3.8). I thought it was easy to upgrade tools with mac osx but for this particular apache upgrade it's not obvious.


again thank for your answers

Mar 28, 2012 4:24 AM in response to BillBaroud

BillBaroud wrote:


I Got this error :


PHP Fatal error: Call to undefined function pg_connect()


It sure looked like PHP had PostgreSQL support incuded. Do you have/need PostgreSQL client libraries? They might be a separate install. That is the problem with PostgreSQL, more people are starting from scratch there.


I will investigate.


Did the ways I follow were bad ? Could you please give me the good way to upgrade apache for macosx then ?


Replacing PHP and/or Apache is definitely outside the scope of this thread. It is about setting up the software that comes included with MacOS X. There are ways to upgrade included software, but very few, if any, people really need to do that. Plus, it is a hassle so the people who know how to do those things typically don't want to bother.


However, with Lion, you can now easily run inside a VM. That is what I did when I updated my new User Tips yesterday. I could try using the VM to upgrade Apache and PHP if that were really the best solution. If you really do need to do that, it would be best to start a new question dedicated to that task. Of course, you will get a round of questions asking why you would want to do such a thing - as you should.


OK no worry about that. My system work just fine, I didn't make anything undoable. All I want is to upgrade apache so it integrate the newer release of PHP (5.4.0), not the oldest one (5.3.8).


Is that what you want to do? Or do you want to use PostgreSQL?


I thought it was easy to upgrade tools with mac osx but for this particular apache upgrade it's not obvious.


It is not trivial. MacOS X isn't designed for that. It can be done by people who know how, but such people typically don't see the need.

Mar 28, 2012 4:50 AM in response to etresoft

OK no worry about that. My system work just fine, I didn't make anything undoable. All I want is to upgrade apache so it integrate the newer release of PHP (5.4.0), not the oldest one (5.3.8).


Is that what you want to do? Or do you want to use PostgreSQL?




Since I was thinking that the 5.3.8 PHP build of mac osx seems not to handle Postgresql, I choose to compile it so as it deals with Postgresql.

For now, my installation seems to work (PHP and postgresql). Ican write PHP that remotelly call postgresql (http://fr.php.net/manual/en/book.pgsql.php).


The problem I had now is to tell apache to use the correct PHP (mean the 5.4.0, not the 5.3.8). I didn't find a way to do this in conf files of apache.

One solution I saw was to re-install apache : I've re-install it using the -enable-layout = Darwin option.

Yet, apache is working, but still not able to deal with PHP.... pfiouuu


Anyway, thanks again for the attention you give to this topic.

Mar 28, 2012 4:58 AM in response to BillBaroud


BillBaroud wrote:


Since I was thinking that the 5.3.8 PHP build of mac osx seems not to handle Postgresql, I choose to compile it so as it deals with Postgresql.

I just installed PostreSQL and it worked right out of the box.


For now, my installation seems to work (PHP and postgresql). Ican write PHP that remotelly call postgresql (http://fr.php.net/manual/en/book.pgsql.php).

OK. I'm confused again. Does it work or doesn't it? Or do you mean your installation and not the default?


Maybe go back to step one and see about getting the default software working. It works for me.

Mar 28, 2012 5:16 AM in response to etresoft

Ok to summarize what is know working :

- Postgresql is working by itself

- I can know access to my postgresql DB with PHP (since I've updated PHP to 5.4.0)

- I have installed apache 2.4.1:

- I can launch it via syst.prefs > share > web sharing (cool)

- Seeing /var/log/apache2/error_log, I can see that It uses PHP 5.4.0 (also cool) :

[Wed Mar 28 14:04:20.650630 2012] [mpm_event:notice] [pid 937:tid 140735167261024] AH00489: Apache/2.4.1 (Unix) PHP/5.4.0 configured -- resuming normal operations

[Wed Mar 28 14:04:20.650701 2012] [core:notice] [pid 937:tid 140735167261024] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND -D WEBSHARING_ON'


- But when opening a PHP script with a web browser, the code is displayed not interpreted .... which make me think something is wrong in http.conf ....

Mar 28, 2012 5:32 AM in response to BillBaroud

BillBaroud wrote:


Ok to summarize what is know working :

- Postgresql is working by itself

- I can know access to my postgresql DB with PHP (since I've updated PHP to 5.4.0)


Yes, but I can access my PostgreSQL database with the built-in PHP. I haven't installed any other PHP or Apache. Everything works fine.


You cannot say the same thing. I suggest you go back to square one. Undo any system changes. Verify that a simple info.php works with the built-in Apache and PHP. Then try PostgreSQL. This was why I was so worried about all the changes you have made. Now you cannot perform simple tasks. You have made extensive changes to the system, none of which were required.


It may still be an interesting exercise to upgrade Apache and PHP on MacOS X and do it correctly. It is not something that you need to undertake at this time. First get the built-in software running correctly. Then, if you have a compelling need to upgrade something, start a new question for that. You will have to be able to definitively prove that you need to upgrade Apache and PHP and are able to get that working, otherwise, nobody will care. You haven't proved that so far.


Mar 28, 2012 5:46 AM in response to etresoft

Merf OK.


So I'm now re-installing OSX Lion 😉.


Then I'll get PostgreSQL (9.1)


Where did you installed it ?

How did you check you can access PostgresSQL using PHP (So I can perform the same test as yours) ?

Does the simple info.php correspond to <?PHP phpinfo(); ?>


And I agree with you, I'll post a new message before doing any installation process.


bests,

Mar 28, 2012 6:23 AM in response to BillBaroud

BillBaroud wrote:


Where did you installed it ?

How did you check you can access PostgresSQL using PHP (So I can perform the same test as yours) ?

Does the simple info.php correspond to <?PHP phpinfo(); ?>


And I agree with you, I'll post a new message before doing any installation process.

I just did the default PostgreSQL install. It wasn't any more difficult than MySQL. There is an annoying bit about wanting to have more shared memory.


I created a little demo table and a tiny PHP script to read from it. No great programming there, but it did work.


<html>

<head>

<title>Postres test</title>

</head>

<body>

<p>

<?php

$connection = pg_connect("host=localhost port=5432 dbname=test user=postgres password=postgres")

or die ("Cannot connect --> " . pg_last_error($conn));


$result=pg_exec("SELECT * FROM test"); // Sample of SQL QUERY


while($fetch = pg_fetch_row($result))


printf("%s<br>", json_encode($fetch));


pg_close($connection); // Close this connection

?>

</p>

</body>

</html>


Obviously I didn't put much effort into this. Hopefully you have a more secure postgres user account.

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.