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

How to install PHP on Mac?

藏色散人
Release: 2023-04-05 18:26:02
Original
8249 people have browsed it

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
Copy after login

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
Copy after login

Next, edit the Apache configuration:

vi httpd.conf
Copy after login

Uncomment (delete #):

LoadModule php5_module libexec/apache2/libphp5.so
Copy after login

Then, restart Apache:

apachectl restart
Copy after login
Copy after login

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
Copy after login

Create the phpinfo() page in your DocumentRoot:

echo &#39;<?php phpinfo();&#39; >
/Library/WebServer/Documents/phpinfo.php
Copy after login

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
Copy after login

gracefulStop

apachectl graceful-stop
Copy after login

RestartApache

apachectl restart
Copy after login
Copy after login

restart of graceful

apachectl graceful
Copy after login

FindApacheversion

httpd -v
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template