How do you get php working on Mac OS X?

PhpMacosOsx Lion

Php Problem Overview


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(); ?>

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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.

Solution 3 - Php

I have one word for you. MAMP.

Solution 4 - Php

Try it phpbrew

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

Solution 5 - Php

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.

Solution 6 - Php

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAJAView Question on Stackoverflow
Solution 1 - PhpMatt GibsonView Answer on Stackoverflow
Solution 2 - PhpRonaldtView Answer on Stackoverflow
Solution 3 - PhpcwallenpooleView Answer on Stackoverflow
Solution 4 - Phpydk2View Answer on Stackoverflow
Solution 5 - PhpHarman GillView Answer on Stackoverflow
Solution 6 - PhpTrickyJView Answer on Stackoverflow