How to Work with PHP programs on macOS / Mac OS X
The macOS / Mac OS X operating system comes pre-installed with the libraries needed to run PHP programs. You can interact with PHP programs via the Terminal application [Applications -> Utilities -> Terminal]. You can also use the built-in web server that comes with Mac, the Apache web server, to execute PHP programs. For more information on running PHP via the local web server, see the following:
Running PHP on Apache for Mac
PHP programs can be created using any text editor such as EditRocket. PHP programs and scripts typically end with the .php extension. EditRocket will automatically recognize files with the .php extension as PHP programs, and will color the syntax accordingly.
To create a PHP program, simply create a new file, such as hello.php. In the file, place the following:
The above program can be executed using the EditRocket Tools -> PHP -> Execute Program option, or you can execute it from the Terminal window. To execute the script in the Terminal, use the cd command to cd to the directory where the hello.php file was saved, such as
cd /Users/user/Desktop
Then type the following:
php hello.php
Hello World! should then be printed to the screen.
Submit a Pull Request Report a Bug
Table of Contents
- Using Packages
- Using the bundled PHP prior to macOS Monterey
- Compiling PHP on macOS
This section contains notes and hints specific to installing PHP on macOS. PHP is bundled with macOS since macOS X [10.0.0] prior to macOS Monterey [12.0.0]. Compiling is similar to the Unix installation guide.
+add a note
User Contributed Notes
There are no user contributed notes for this page.
How to Install PHP on a Mac
flickr Editorial/Getty Images / Getty Images
Updated on February 21, 2019
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.
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 //localhost/ in a browser, and you should see the standard Apache test page.
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.
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 '