How to Run PHP on Mac

Subodh Poudel Feb 02, 2024
  1. Use the php -S Command to Run PHP on Mac
  2. Use the Built-In Apache Web Server to Run PHP on Mac
  3. Use MAMP to Run PHP on Mac
How to Run PHP on Mac

In this tutorial, we will introduce methods to run PHP on Mac.

Use the php -S Command to Run PHP on Mac

PHP is a server-side language. It runs on the server. Therefore, it needs a webserver to run. There are different web servers like Apache HTTP Server, NGINX, Oracle HTTP Server, Lighttpd, Node.js Server, etc. The server-side languages need these kinds of servers to run the script. PHP also overs a built-in web server for application development. It is intended for testing purposes, and it is recommended that it should not be used in public networks. The web server is capable of running only a single-threaded process. The command PHP -S builds a local server in PHP. Then, we can specify our PHP file in the URL to run the PHP file.

Suppose we have a directory project where our PHP file index.php is located.

Firstly, open the terminal and go to the project directory using the cd command.

Write the command PHP -S with the option 127.0.0.1:8000 and press enter. Then, a local web server starts.

Go to a web browser and type the address 127.0.0.1:8000/index.php. The PHP file will run.

The address 127.0.0.1 is a loop-back address. It redirects to the same machine. We do not need a physical connection to a network. It is used for testing purposes in a local machine. The number 8000 after the IP address in the port used by the IP. We can refer to the IP address 127.0.0.1 as localhost.

Example Code:

php -S 127.0.0.1:8000

Output:

[Sun Oct 3 10:26:13 2021] PHP 7.4.11 Development Server (http://127.0.0.1:8000) started

Use the Built-In Apache Web Server to Run PHP on Mac

PHP comes with a built-in Apache Web Server in Mac. We can start the Apache service to create a local server and run our PHP file. We can use the command sudo apachectl start in the terminal to start the webserver. Then, typing the URL http://localhost/index.php where our PHP file is index.html will run the PHP file. The PHP file should be in the root directory to run.

We can stop the web server using the apachectl stop command. We can check the status of the Apache server using the command apachectl status. The command will display various server information like active status, memory, task, CPU, etc. Thus, we can run a PHP file starting the Apache server on Mac.

Example Code:

sudo apachectl start

Use MAMP to Run PHP on Mac

We can use the MAMP local server environment to run PHP files on Mac. MAMP stands for Macintosh, Apache, MySQL, and PHP. It is used to run a local server on Mac, and it uses an Apache webserver. We can specify the port number of our server from the MAMP application in the Preference option.

For example, after opening the MAMP application, click on the Start Servers option. Then, the local server will start. Go to the browser and type the address http://127.0.0.1:8000/index.php. The index.php file will run on the browser. The port 8000 is the default port, and it can be set from the MAMP application. Thus, we can run a PHP file on Mac using the MAMP.

Subodh Poudel avatar Subodh Poudel avatar

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

LinkedIn