In order to work on any PHP web based application, you will need to install it first. In Windows it is easy to install and configure. But if we talk about the Linux or Mac platform then the installation steps are totally different. We already covered up the LAMP stack setup for Ubuntu platform. Today, in this post, we will install PHP in Mac along with the Apache. In order to run PHP, we needed Apache or Nginx server. Both PHP and Apache are widely used open source software. The MacOS comes up with the Apache server inbuilt. But by default, it is disabled. Also, we will install Homebrew in Mac. So, let’s see how to install PHP in Mac.
Prerequisites
Before starting, please make sure you met the below requirements.
- MacOS
- Terminal access with Sudo privileges
Once, you are done, let’s begin the installation steps of PHP in MacOS.
Enable Apache in MacOS
As I already discussed, Apache is inbuilt with the MacOS. So, at the very first step, we need to enable this service. Hence, to enable Apache service, simply open the terminal, which is located in the Mac’s Applications > Utilities folder. You need to switch to the root user in Terminal. So, that you can run any commands without any permission issues. In order to switch to the root user and start Apache, enter the below command into Terminal.
sudo su -
After switching to the root user, next you have to hit the below command.
apachectl start
That’s it. Now the Apache service is enabled. For testing purpose, we can visit the localhost in the browser to see the Test page of Apache.
So, here, Apache is running and we can see the default page. Now, in the next step, we have to install PHP in MacOS. But the simplest way to install PHP in MacOS is using Homebrew.
Hence, we will install Homebrew in MacOS first.
Install Homebrew in MacOS
Homebrew is an open source package manager for that is used to manage any package in MacOS. It is available for Linux as well.
For installing Homebrew, you can visit the official page of Homebrew or you can use the below command as well.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Once, you enter the above command, it will start installing Homebrew.
Once the installation is completed, you will need to add Homebrew to root path so that it can be accessible from anywhere in your system. So, just follow the instruction as per the brew in the terminal itself. It asked to execute the below two commands.
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/umesh/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)”
you can check the version of Homebrew using below command.
homebrew --version
This will return the installed version of Homebrew.
After installation of Homebrew, we can install PHP in MacOS.
Install PHP in MacOS Using Homebrew
Now, we will install PHP through Homebrew package manager. So, in order to install PHP, simply open the terminal and hit the below command.
brew install php
This will take a couple of minutes to install PHP along with all the necessary extensions.
Once the installations completed, it will instruct you to start PHP services.
In order to start or restart the PHP services, you can use the below command.
brew services start php
brew services restart php
This brew command will start/restart the PHP service in MacOS.
You can check the installed PHP version as well in order to check for the successful installation.
php --version
We have the latest version of PHP installed now. In the next step, we need to export the PHP path so that we can run PHP scripts.
You can run the below script for doing this.
export PATH="/opt/homebrew/etc/php/8.2/bin:$PATH"
Here, I have the PHP version 8.2, hence, I have included that path.
Once the PATH is exported, we can create a test file to check whether we are able to run PHP scripts.
Create and Run a PHP Script in MacOS
I have create a new folder named PHP inside the Documents directory. Then inside this folder, I have created a PHP file named hello.php with a test script.
Now, in the next step, we need to start PHP server at this folder. So, in order to do that, we will navigate to the terminal and simply drag this script folder inside the Terminal. After doing that, it will add the PATH of the dragged folder as shown below.
Now, we have to switch to that folder so, just add cd before the folder PATH.
In the next step, you will need to start PHP development server using the below command. Simply hit the below command inside the Terminal having the project folder path.
php -S localhost:8080
It will start the development server on the Port 8080.
Now, we will be able to run the PHP script in the browser.
That’s all for this post. We did PHP Installation in Mac using Homebrew. Homebrew provides package management in Mac and Linux platform. So, using this, we can install any package in Mac OS platform.
I hope this post will be helpful for the beginners while setup the PHP environment for the development.
Sammy says
I’ve found a tool called ServBay.dev can provide much easier way for PHP developers, especially the beginners.
It includes all versions of PHP, MariaDB, and PostgreSQL, as well as Redis and Memcached. Users can run multiple versions of PHP instances all at once. Trust me, it’s made my PHP dev life a whole lot smoother. Might be worth a shot for you to check it out!
Umesh Rana says
Yes, that makes more easy to setup PHP environment.