It is recommended to use Homebrew to install PHP, run /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" Install Homebrew, and then execute brew install php or the specified version such as brew install php@8.1; after installation, edit the php.ini file in the corresponding path to adjust the configuration of memory_limit, upload_max_filesize, post_max_size, display_errors and other configurations and restart the service; if using Apache, confirm that the module has been enabled and the service is started; if using Nginx, you need to install Nginx, configure the site files and enable PHP-FPM, and use fastcgi_pass Point to 127.0.0.1:9000 and set SCRIPT_FILENAME to restart the service.
PHP is one of the commonly used development languages on macOS. The system comes with a PHP environment, but the version is usually older. If you need to build a modern and stable PHP development environment, you can manually install and configure it yourself.

Install PHP
macOS provides several ways to install PHP. The most recommended way is through Homebrew, which can help you manage packages easily.
- If Homebrew has not been installed yet, you can run
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
in the terminal to install it. - After the installation is completed, run
brew install php
to install the latest stable version of PHP.
You can also choose to install a specific version, such as brew install php@8.1
, the specific version number depends on the currently available version.

After the installation is completed, you can use php -v
to check whether the current PHP version is effective.
Configure a PHP environment
After installing PHP, the default configuration file path is /opt/homebrew/etc/php/{版本號(hào)}/php.ini
(if it is an Apple Silicon chip) or /usr/local/etc/php/{版本號(hào)}/php.ini
.

You can edit this file and adjust some common settings:
-
memory_limit
: adjust the memory limit, for example, set to256M
-
upload_max_filesize
andpost_max_size
: Increase the upload limit as needed -
display_errors
: It is recommended to open it in the development environment for easy debugging
After modification, remember to restart PHP or related services, such as using brew services restart php
to make the configuration take effect.
Also, if you are using Apache or Nginx, you also need to make sure they load the correct PHP module or FastCGI configuration.
Work with a web server
In most cases you will use PHP with a web server. macOS comes with Apache, and you can also install Nginx using Homebrew.
Using Apache:
PHP installed by Homebrew will automatically configure the module loading path of Apache. You can check if the PHP module is enabled in the /etc/apache2/httpd.conf
file.
Run sudo apachectl start
to start Apache, and then visit http://localhost
to see the default page.
Using Nginx:
You can install Nginx through brew install nginx
, then configure its site files to point to your PHP project directory, and enable PHP-FPM:
- Start PHP-FPM:
brew services start php
- Modify the Nginx configuration file and add contents similar to the following:
location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }
Restart Nginx after saving: sudo brew services restart nginx
In this way, you can use Nginx PHP to build a local development environment.
Basically all this is it. The whole process is not complicated but details are easy to ignore, such as path errors, service not started, and configuration files not changed to peer. As long as you do it step by step, there should be no problem.
The above is the detailed content of Setting Up PHP on macOS. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

NginxhandlesstaticfilesandroutesdynamicrequeststoPHP-FPM,whichprocessesPHPscriptsviaFastCGI;2.OptimizePHP-FPMbyusingUnixsockets,settingpm=dynamicwithappropriatemax_children,spareservers,andmax_requeststobalanceperformanceandmemory;3.ConfigureNginxwit

WSL2isthenewstandardforseriousPHPdevelopmentonWindows.1.InstallWSL2withUbuntuusingwsl--install,thenupdatewithsudoaptupdate&&sudoaptupgrade-y,keepingprojectsintheLinuxfilesystemforoptimalperformance.2.InstallPHP8.3andComposerviaOnd?ejSury’sPPA

It is recommended to use Homebrew to install PHP, run /bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" to install Homebrew, and then execute brewinstallphp or a specified version such as brewinstallphp@8.1; after installation, edit the php.ini file in the corresponding path to adjust memory_limit, upload_max_filesize, post_max_size and display_

CompilingPHPfromsourceisnotnecessaryformostprojectsbutprovidesfullcontrolforpeakperformance,minimalbloat,andspecificoptimizations.2.ItinvolvesconvertingPHP’sCsourcecodeintoexecutables,allowingcustomizationlikestrippingunusedextensions,enablingCPU-spe

LaunchanEC2instancewithAmazonLinux,appropriateinstancetype,securesecuritygroup,andkeypair.2.InstallLAMPstackbyupdatingpackages,installingApache,MariaDB,PHP,startingservices,securingMySQL,andtestingPHP.3.DecouplecomponentsbymovingdatabasetoRDS,storing

OPcache and JIT are the core tools for PHP8.0 performance optimization. Correct configuration can significantly improve execution efficiency; 1. Enable OPcache and set opcache.enable=1, opcache.memory_consumption=192, opcache.max_accelerated_files=20000, opcache.validate_timestamps=0 to implement opcode caching and reduce parsing overhead; 2. Configure JIT to enable tracking JIT through opcache.jit_buffer_size=256M and opcache.jit=1254

ChooseaCI/CDplatformlikeGitHubActionsorGitLabCIfortightversioncontrolintegrationandminimalinfrastructure;2.DefineaconsistentPHPenvironmentusingcontainerizationwithimageslikephp:8.2-cliorcomposer:latestandinstalldependenciesviacomposerinstall--no-inte

VerifysystemrequirementsanddependenciesbyconfirmingOScompatibilityandinstallingessentiallibrariesandbuildtools,usingpackagemanagerslikeaptoryumtosimplifydependencymanagement.2.CheckPHPconfigurationandcompilationerrorsbyrunningaminimal./configurecomma
