目录
安装 PHP
配置 PHP 环境
与 Web 服务器配合使用
使用 Apache:
使用 Nginx:
首页 后端开发 php教程 在MacOS上设置PHP

在MacOS上设置PHP

Jul 17, 2025 am 04:15 AM
PHP Installation

推荐使用 Homebrew 安装 PHP,运行 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 安装 Homebrew,再执行 brew install php 或指定版本如 brew install php@8.1;安装后编辑对应路径的 php.ini 文件调整 memory_limit、upload_max_filesize、post_max_size 和 display_errors 等配置并重启服务;若使用 Apache,确认模块已启用并启动服务即可;若使用 Nginx,则需安装 Nginx、配置站点文件并启用 PHP-FPM,通过 fastcgi_pass 指向 127.0.0.1:9000 并设置 SCRIPT_FILENAME 后重启服务。

Setting Up PHP on macOS

PHP 是 macOS 上常用的开发语言之一,系统自带了 PHP 环境,但版本通常比较旧。如果你需要搭建一个现代、稳定的 PHP 开发环境,可以自己手动安装和配置。

Setting Up PHP on macOS

安装 PHP

macOS 提供了几种方式来安装 PHP,最推荐的方式是通过 Homebrew,它能帮你轻松管理软件包。

  1. 如果还没安装 Homebrew,可以在终端运行 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 来安装。
  2. 安装完成后,运行 brew install php 就可以安装最新稳定版的 PHP。

你也可以选择安装特定版本,比如 brew install php@8.1,具体版本号取决于当前可用版本。

Setting Up PHP on macOS

安装完成后,可以用 php -v 查看当前 PHP 版本确认是否生效。

配置 PHP 环境

安装完 PHP 后,默认的配置文件路径是 /opt/homebrew/etc/php/{版本号}/php.ini(如果是 Apple Silicon 芯片)或者 /usr/local/etc/php/{版本号}/php.ini

Setting Up PHP on macOS

你可以编辑这个文件,调整一些常用设置:

  • memory_limit:调整内存限制,比如设为 256M
  • upload_max_filesizepost_max_size:根据需要调大上传限制
  • display_errors:在开发环境中建议打开,方便调试

修改完后记得重启 PHP 或相关服务,比如用 brew services restart php 使配置生效。

另外,如果你使用 Apache 或 Nginx,还需要确保它们加载了正确的 PHP 模块或 FastCGI 配置。

与 Web 服务器配合使用

大多数情况下你会把 PHP 和 Web 服务器一起使用。macOS 自带 Apache,也可以用 Homebrew 安装 Nginx。

使用 Apache:

Homebrew 安装的 PHP 会自动配置 Apache 的模块加载路径。你可以检查 /etc/apache2/httpd.conf 文件中是否启用了 PHP 模块。

运行 sudo apachectl start 启动 Apache,然后访问 http://localhost 就能看到默认页面。

使用 Nginx:

可以通过 brew install nginx 安装 Nginx,然后配置其站点文件指向你的 PHP 项目目录,并启用 PHP-FPM:

  1. 启动 PHP-FPM:brew services start php
  2. 修改 Nginx 配置文件,加入类似如下内容:
location ~ \.php$ {
    fastcgi_pass  127.0.0.1:9000;
    include       fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

保存后重启 Nginx:sudo brew services restart nginx

这样就可以用 Nginx PHP 搭建本地开发环境了。


基本上就这些,整个过程不复杂但容易忽略细节,比如路径错误、服务没启动、配置文件没改对等。只要一步步来,应该没问题。

以上是在MacOS上设置PHP的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Laravel 教程
1604
29
PHP教程
1510
276
利用WSL 2的力量来实现Linux-intagity PHP开发工作流程 利用WSL 2的力量来实现Linux-intagity PHP开发工作流程 Jul 26, 2025 am 09:40 AM

wsl2isthenewstanceforseriousphpdevelopmentonwindows.1.installwsl2withubuntuingusingwsl-install,thenupdatewithsudoaptupdat E && sudoaptupgrade-y,keepprojectsinthelinuxfilesystemforoptimalperformance.2.installphp8.3andComposerviaondEjsurýsppa

掌握PHP-FPM和NGINX:高性能设置指南 掌握PHP-FPM和NGINX:高性能设置指南 Jul 25, 2025 am 05:48 AM

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

在MacOS上设置PHP 在MacOS上设置PHP Jul 17, 2025 am 04:15 AM

推荐使用Homebrew安装PHP,运行/bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"安装Homebrew,再执行brewinstallphp或指定版本如brewinstallphp@8.1;安装后编辑对应路径的php.ini文件调整memory_limit、upload_max_filesize、post_max_size和display_

解锁峰值PHP性能:配置OPCACHE和JIT编译 解锁峰值PHP性能:配置OPCACHE和JIT编译 Jul 24, 2025 pm 09:58 PM

OPcache和JIT是PHP8.0 性能优化的核心工具,正确配置可显着提升执行效率;1.启用OPcache并设置opcache.enable=1、opcache.memory_consumption=192、opcache.max_accelerated_files=20000、opcache.validate_timestamps=0以实现opcode缓存并减少解析开销;2.配置JIT通过opcache.jit_buffer_size=256M和opcache.jit=1254启用追踪JIT

自动化PHP环境设置:将PHP集成到CI/CD管道中 自动化PHP环境设置:将PHP集成到CI/CD管道中 Jul 26, 2025 am 09:53 AM

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

从头开始在AWS EC2上部署可扩展的PHP环境 从头开始在AWS EC2上部署可扩展的PHP环境 Jul 26, 2025 am 09:52 AM

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

揭开PHP汇编的神秘面纱:从源构建自定义PHP以获得最佳性能 揭开PHP汇编的神秘面纱:从源构建自定义PHP以获得最佳性能 Jul 25, 2025 am 06:59 AM

汇编phomerceisnotn coresemencomeformostprojectsbutprovidesfuidsfuidsfudsfiidesfulstrolcontrolforperperance,minimalbloat,andspecificoptimization.2.itinvolvesConvertingPhpphpphp'scsourcececececececeodeintoIntoExecutables,允许customizationLikizationLikeStripingunusedunsuptipingunseftimpipingunseftimpippingunsippingsextensenions enablingCpuspucpu

故障排除常见的PHP安装陷阱:诊断清单 故障排除常见的PHP安装陷阱:诊断清单 Jul 26, 2025 am 09:50 AM

verifySystemRequirements and dipendenciesbyConfirmingoScompatiby andInstallingSenlingEssentialLibrariesandBuildTools,使用PackageManagerSlikeSlikeAptoryUmTosImplifyDependentyDependentymanagement.2.Checkphpphpphpphpphpphpphpconfigurationand and conconfigurationAndCompConfigurationAndCompilationErrateRrationRuntirNumentByRunningMinimal./confictecomma

See all articles