Running PHP on Mac
In this article, we'll show you how to run PHP on your Mac.
php -S
Run PHP on Mac
using command
PHP is a server-side language. It runs on a server. Therefore, it requires a web server to run. There are different web servers like Apache HTTP Server, NGINX, Oracle HTTP Server, Lighttpd, Node.js Server, etc. Server-side languages require these types of servers to run the scripts. PHP also provides a built-in web server for application development. It is used for testing purposes and is not recommended to be used in a public network. The web server can only run a single-threaded process. Command PHP -S
Build a local server using PHP. Then, we can specify our PHP file in the URL to run the PHP file.
Assume that we have a directory where project
our PHP files index.php
are located.
First, open a terminal and cd
change into the directory using the command project
.
127.0.0.1:8000
Write the command
with options PHP -S
and press Enter. The local web server then starts.
Go to your web browser and enter the address 127.0.0.1:8000/index.php
. The PHP file will run.
The address 127.0.0.1
is a loopback address. It redirects to the same machine. We do not need a physical connection to the network. It is used for testing in the local machine. The number after the IP address in the port used by IP 8000
. We can refer to the IP address 127.0.0.1
as localhost.
Sample 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
Run PHP on Mac using the built-in Apache web server
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 files. We can use sudo apachectl start
the command in the terminal to start the web server. Then, enter 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 in order to run.
We can apachectl stop
stop the web server using the command. We can apachectl status
check the status of the Apache server using the command. This command will display various server information such as activity status, memory, tasks, CPU, etc. Therefore, we can run a PHP file on Mac to start the Apache server.
Sample code:
sudo apachectl start
Running PHP on Mac with MAMP
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 uses the Apache web server. We can specify the port number of our server from the Preference options of the MAMP application.
For example, after opening the MAMP application, click 启动服务器
Options. Then, the local server will start. Go to the browser and enter the address http://127.0.0.1:8000/index.php
. index.php
The file will run on the browser. The port 8000
is the default port and can be set from the MAMP application. Therefore, we can use MAMP to run PHP files on Mac.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a Post exists in PHP
Publish Date:2025/04/13 Views:170 Category:PHP
-
PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Returns the article tag with ID from the action page
Publish Date:2025/04/13 Views:80 Category:PHP
-
Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at
Switching PHP versions on Ubuntu
Publish Date:2025/04/13 Views:78 Category:PHP
-
Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t
Resizing images in PHP
Publish Date:2025/04/13 Views:155 Category:PHP
-
In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using
PHP upload image
Publish Date:2025/04/13 Views:61 Category:PHP
-
We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Updating PHP 7.x to 7.4 on CentOS
Publish Date:2025/04/13 Views:131 Category:PHP
-
This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of