55

I have recently updated to Lion and enabled Web Sharing in the System Preferences but I am unable to get php working.

I added an info file to the web root directory and it outputs the file as text.

info.php
with the content
<?php phpinfo(); ?>
3
  • 3
    I generally install a VM and slap Debian on it. It is better organised for doing server programming (IMNSHO).
    – Quentin
    Jul 22, 2011 at 13:19
  • 1
    @Quentin This is true, but I generally get by with the builtin stuff plus MySQL for doing simple web dev, especially when away from home -- on a MacBook Air you don't want to be taking up any memory or disk space you don't have to! :) Jul 22, 2011 at 13:29
  • 2
    Please let us to know here to post this question, if not on stackoverflow.com? Is superuser.com equipped to answer a question like this?
    – tmadsen
    Sep 10, 2011 at 14:51

6 Answers 6

106

(Edit: This method appears to work fine for 10.9 (Mavericks), 10.10 (Yosemite) and 10.11 (El Capitan), so I figured I'd mention that for any new influx of slightly frustrated OS X updaters :D )

Edit your /etc/apache2/httpd.conf and make sure the line:

LoadModule php5_module libexec/apache2/libphp5.so

...exists. I think it's commented out by default in the standard OS X config, but from what I remember, you just need to uncomment it, then re-start Apache:

sudo apachectl restart

And you should be good to go.

4
  • Great, this worked for me on my Lion installation... and its better to use the already existing & available softwares rather than downloading extra softwares, installing them... Aug 25, 2012 at 8:57
  • Yes, this works with 10.9.5 out of the box, also because there is this additional configfile: /etc/apache2/other/php5.conf. So don't struggle with <ifmodule>... etc. May 12, 2015 at 13:19
  • This fixed the issue after I upgraded to El Capitan from Yosemite
    – frazras
    Oct 6, 2015 at 17:26
  • Still works with MacOS Mojave 10.14.4, but notice the line to uncomment is now #LoadModule php7_module libexec/apache2/libphp7.so Thank you! Apr 3, 2019 at 23:34
28

UPDATE: Please note that this was written for OS X pre-(High) Sierra. If you run OSX 10.12 or newer, please follow this more than excellent guide by Andy Miller: macOS 10.15 Catalina Apache Setup: Multiple PHP Versions


I too like to use things that are basically already there. I don't see why anyone would use MAMP or AMPPS (or any other packed 3rd party out-of-box webserver app) when Mac OS X comes with apache and PHP by default.

Took me a couple of tries to get it working, so here is basically what did it for me and hopefully it'll help you guys save a little time.

Like Matt Gibson said, start terminal and type: (sudo requires your root password)

sudo nano /etc/apache2/httpd.conf

Then uncomment this line by removing the '#' in front of it (ctrl+v can be used as page-down)

LoadModule php5_module libexec/apache2/libphp5.so

To make sure you can include files etc in PHP, scroll to "User _www" (in my case) and change that to: (where "yourusername" is the user you login with)

User yourusername

You can leave the group as-is, "Group _www" by default on a fresh OS X Mountain Lion install.

On default apache only looks for index.html, so search for "DirectoryIndex index.html" and change that to: (adding index.html at the end is optional of course)

DirectoryIndex index.php index.html index.htm

Exit and save by pressing ctrl+x (and confirm with "y")

Then restart apache:

sudo apachectl restart

My phpinfo(); returned with a PHP Version 5.3.15

==================

Since I find it useful to have my local sites in my user dir, I created a directory /Users/yourusername/Sites (which isn't there on default anymore in Mountain Lion).

Again, edit httpd.conf via "sudo nano /etc/apache2/httpd.conf" and ...

Scroll down to "DocumentRoot" and change it to: (where "yourusername" is the username you login with)

DocumentRoot "/Users/yourusername/Sites/"

Scroll to where it says "# This should be changed to whatever you set DocumentRoot to." and change the next line to: (where "yourusername" is the username you login with)

<Directory "/Users/yourusername/Sites/">

Then exit and save by pressing ctrl+x (and confirm with "y")

Restart apache.

2
  • Also tested it on OS X Yosemite, worked perfectly as expected.
    – Ronaldt
    Jun 21, 2014 at 19:21
  • These steps are PERFECT! Thank you! It actually worked. (Yosemite 10.10.2) Jun 20, 2015 at 1:34
4

I have one word for you. MAMP.

4
  • I love xampp, but it doesn't work on my MacBook Pro with Lion, thats why I'm here :) Aug 30, 2011 at 20:04
  • 1
    @Pete That isn't Xampp, that is MAMP. Big difference. Aug 30, 2011 at 20:17
  • 1
    It is surely better that we foster an understanding of non packaged up solutions as in using what is already there. This is not solving the problem it is finding a different way to get something done - not as good IMO.
    – landed
    Feb 19, 2014 at 10:55
  • @landed Personally, I've taken to using port install for everything. Not entirely sure that a package manager is substantially different though. Feb 19, 2014 at 21:19
2

Try it phpbrew

It's ease build, install tool for PHP, any version.

2

For the latest version of mac os i.e Mojave 10.14, follow the steps below to activate PHP server:

  1. Open terminal. Press command+spacebar, type terminal and press enter.

  2. Enter command $ sudo nano /etc/apache2/httpd.conf to edit httpd.conf file in nano, which is an inbuilt file editor in macOS.

  3. Enter the password.

  4. Now search loadmodule by pressing control+w.

  5. Find file named #LoadModule php7_module libexec/apache2/libphp7.so

  6. Uncomment the file by removing the # symbol present at the very beginning of the module.

  7. Now exit the editor by pressing control+x, press y to save the changes and press enter to confirm changes to the httpd.conf file.

  8. Now restart the apache server by entering the command

    $ sudo apachectl restart

  9. PHP server should be running now running.

Now you can check the working of your server by opening the PHP file from your default server directory or you can also change the directory as well.

2

After upgrading the MAC OSX to 10.14.5 My Apache config file was new and all my previous settings were replaced with default settings.

I was not able to open any PHP files in my browser. I followed the above steps and it resolved the issue.

Note - In OSX 10.14.5 there is Php7.1.23

Steps I followed -

  1. Edit the httpd.conf file located under /etc/apache2/httpd.conf
  2. This below given live was commented. I uncommented it by removing the # sign and saved the httpd.conf file and restarted the apache service.

LoadModule php7_module libexec/apache2/libphp7.so

  1. sudo apachectl restart

Thank you for the solution, appreciated your help friends.

Regards,

Vicky Jadhav (India - pune)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.