Home  >  Article  >  Backend Development  >  How to install PHP on Mac?

How to install PHP on Mac?

藏色散人
藏色散人Original
2019-03-30 13:37:338222browse

Before enabling PHP on your Mac, you must first enable Apache. Both PHP and Apache are free and open source software programs and both are installed on all macs. PHP is server-side software, and Apache is the most widely used web server software. Enabling Apache and PHP on Mac is not difficult.

How to install PHP on Mac?

01. Enable Apache on MacOS

To enable Apache, please open Applications > Utilities on Mac Applications in the folder. You need to switch to the root user in the terminal so that you can run commands without any permission issues. To switch to the root user and start Apache, enter the following code in the terminal.

sudo su -
apachectl start

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

02. Enable PHP for Apache

Back up the current Apache configuration before starting. Because the configuration may change with future upgrades. Enter the following information in the terminal:

cd /etc/apache2/
cp httpd.conf httpd.conf.sierra

Next, edit the Apache configuration:

vi httpd.conf

Uncomment (delete #):

LoadModule php5_module libexec/apache2/libphp5.so

Then, restart Apache:

apachectl restart

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

03. Verify whether PHP is enabled

To verify whether PHP is enabled, please create a phpinfo() in DocumentRoot page. 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 &#39;<?php phpinfo();&#39; >
/Library/WebServer/Documents/phpinfo.php

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

04. Other Apache commands

You have learned how to use apachectl start to start Apache in terminal mode.

Here are more related command lines you may need. They should be executed as the root user in the terminal. If not, please add it in front.

StopApache

apachectl stop

gracefulStop

apachectl graceful-stop

RestartApache

apachectl restart

restart of graceful

apachectl graceful

FindApacheversion

httpd -v

Note: Starting, restarting or stopping "graceful" can prevent the process from suddenly Stop and allow ongoing processes to complete.

The above is the detailed content of How to install PHP on Mac?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn