Sling Academy
Home/PHP/How to set up PHP in MacOS

How to set up PHP in MacOS

Last updated: January 09, 2024

Introduction

If you’re working with web development or want to test PHP code locally on your Mac, setting up PHP is essential. This tutorial guides you through the process step-by-step, from installing PHP to configuring your environment.

Installing Homebrew

Firstly, you’ll need to install Homebrew, a popular package manager for MacOS. Open your terminal and run:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

This script will install Homebrew, making it much easier to manage software installations on your Mac.

Installing PHP

With Homebrew installed, you can easily install PHP by running:

brew update
brew install php

This will download and install the latest version of PHP.

Basic PHP Server

After installation, you can start a basic PHP server to test things out. Navigate to the directory where your PHP files are located, then run:

php -S localhost:8000

This starts a PHP server on port 8000, and you can view the output by visiting http://localhost:8000 in your browser.

Advanced Configuration

To customize your PHP setup, you can tweak the php.ini file. Find it using:

php --ini

Edit the file with a text editor of your choice (for instance, Nano or Vim).

Using Virtual Hosts

It’s often useful to set up virtual hosts for local web development. Modify your Apache configuration file (httpd.conf), which is typically located in /etc/apache2/, and add the required directives to define your virtual host.

Integrating with MySQL or MariaDB

Many PHP projects require a database. Install MySQL or MariaDB using Homebrew:

brew install mysql
brew services start mysql

Or for MariaDB:

brew install mariadb
brew services start mariadb

To connect PHP with the database, use PDO or mysqli in your PHP scripts.

Using Package Managers like Composer

Composer is a dependency manager for PHP akin to npm for Node.js or pip for Python. Install Composer:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Move the composer.phar file to a global path or use it locally in your project directory.

Conclusion

With PHP set up on your MacOS system, you’re ready to develop and test your web applications locally. Remember, the internet is your best resource for troubleshooting any setup issues. Happy coding!

Next Article: How to set up PHP in Ubuntu

Previous Article: How to set up PHP in Windows

Series: Basic PHP Tutorials

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array