How to Install PHP on a Mac

01
of 05

PHP and Apache

Many website owners use PHP with their websites to expand the capabilities of the sites. Before you can enable PHP on a Mac, you first have to enable Apache. Both PHP and Apache are free open source software programs and both come installed on all Macs. PHP is server-side software, and Apache is the most widely used web server software. Enabling Apache and PHP on a Mac isn't difficult to do.

02
of 05

Enable Apache on MacOS

To enable Apache, open the app, which is located in the Mac's Applications > Utilities folder. You need to switch to the root user in Terminal so you can run commands without any permission issues. To switch to the root user and start Apache, enter the following code into Terminal.

sudo su -

apachectl start 

That's it. If you want to test if it worked, enter http://localhost/ in a browser, and you should see the standard Apache test page.

03
of 05

Enabling PHP for Apache

Make a backup of the current Apache configuration before you begin. This is a good practice as the configuration may change with future upgrades. Do this by entering the following in Terminal:

cd /etc/apache2/

cp httpd.conf httpd.conf.sierra

Next, edit the Apache configuration with:

vi httpd.conf

Uncomment the next line (remove #):

LoadModule php5_module libexec/apache2/libphp5.so

Then, restart Apache:

apachectl restart

Note: When Apache is running, its identity is sometimes "httpd," which is short for "HTTP daemon." This example code assumes a PHP 5 version and MacOS Sierra. As the versions are upgraded, the code must change to accommodate new information.

04
of 05

Verify That PHP Is Enabled

To verify that PHP is enabled, create a phpinfo() page in your DocumentRoot. In MacOS Sierra, the default DocumentRoot is located in /Library/WebServer/Documents. Verify this from the Apache configuration:

grep DocumentRoot httpd.conf

Create the phpinfo() page in your DocumentRoot:

echo '<?php phpinfo();' > /Library/WebServer/Documents/phpinfo.php

Now open a browser and enter http://localhost/phpinfo.php to verify that PHP is enabled for Apache.

05
of 05

Additional Apache Commands

You've already learned how to start Apache in Terminal mode with apachectl start. Here are a few more command lines you might need. They should be executed as the root user in Terminal. If not, prefix them with .

Stop Apache

apachectl stop

Graceful Stop

apachectl graceful-stop

Restart Apache

apachectl restart

Graceful Restart

apachectl graceful

To find the Apache version

httpd -v

Note: A "graceful" start, restart or stop prevents an abrupt halt to proceedings and allows ongoing processes to complete.

Format
mla apa chicago
Your Citation
Bradley, Angela. "How to Install PHP on a Mac." ThoughtCo, Nov. 28, 2016, thoughtco.com/how-to-install-php-on-a-mac-2694012. Bradley, Angela. (2016, November 28). How to Install PHP on a Mac. Retrieved from https://www.thoughtco.com/how-to-install-php-on-a-mac-2694012 Bradley, Angela. "How to Install PHP on a Mac." ThoughtCo. https://www.thoughtco.com/how-to-install-php-on-a-mac-2694012 (accessed December 17, 2017).