Mac下安装 php 环境

Mac 下如何搭建 PHP 开发环境?
更新于: 2022-02-18 09:34:17

XAMPP

用集成环境他不香吗(然而并不香,该配置的一样不少,而且还得多学习一样,xampp的配置与报错处理)。

XAMPP 也可以在 linux 下运行的

X: Mac/Win/Linux 系统都支持

记得用 Applciation 里的这个工具完成卸载

安装与管理

我电脑上因为各种原因失败了。

# 安装(记得挂代理)
brew install php
# 启动
brew services start php

Error: No such file or directory @ rb_sysopen - /Users/aric.zheng/Library/Caches/Homebrew/downloads/47a599c9e22bbc998bba20cbc21d234c459bc6724de27b5fd99888e9dce2ea5c--freetds-1.3.6.catalina.bottle.tar.gz

# 在错误中找到对应的‘freetds’包,单独安装<这个操作可能要多次执行>
brew install freetds
# 然后再继续
brew install php

Mac上以 brew 方式卸载

brew uninstall php

有部分文件需要手动删除,用 rm 删除即可

$ brew uninstall php
Uninstalling /usr/local/Cellar/php/8.1.2... (513 files, 80.0MB)

Warning: The following php configuration files have not been removed!
If desired, remove them manually with `rm -rf`:
  /usr/local/etc/php
  /usr/local/etc/php/8.1
  /usr/local/etc/php/8.1/conf.d
  /usr/local/etc/php/8.1/conf.d/ext-opcache.ini
  /usr/local/etc/php/8.1/pear.conf
  /usr/local/etc/php/8.1/php-fpm.conf
  /usr/local/etc/php/8.1/php-fpm.d
  /usr/local/etc/php/8.1/php-fpm.d/www.conf
  /usr/local/etc/php/8.1/php.ini
  /usr/local/etc/php/8.1/php.ini-production
rm -rf /usr/local/etc/php
rm -rf /usr/local/etc/php/8.1
rm -rf /usr/local/etc/php/8.1/conf.d
rm -rf /usr/local/etc/php/8.1/conf.d/ext-opcache.ini
rm -rf /usr/local/etc/php/8.1/pear.conf
rm -rf /usr/local/etc/php/8.1/php-fpm.conf
rm -rf /usr/local/etc/php/8.1/php-fpm.d
rm -rf /usr/local/etc/php/8.1/php-fpm.d/www.conf
rm -rf /usr/local/etc/php/8.1/php.ini
rm -rf /usr/local/etc/php/8.1/php.ini-production

安装指定版本

  1. 搜索 brew search php
  2. 比如我要安装 php@7.4
  3. 安装命令 brew install php@7.4
  4. 先看一下现在的系统中的 php 版本,以及命令
  5. 将这个版本设置为默认版本 link 一下 brew link php@7.4
  6. 有需要的话可以设置环境变量
# 搜索 
brew search php
# 安装
brew install php@7.4
# link
brew link php@7.4
# 添加到环境变量中<非必须的>
If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.profile
  echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.profile

验证安装情况

$ which php
/usr/local/bin/php
$ ls -alh /usr/local/bin/php
lrwxr-xr-x  1 alo7.aric  admin  32 Feb 18 09:17 /usr/local/bin/php -> ../Cellar/php@7.4/7.4.27/bin/php
$ php --version
PHP 7.4.27 (cli) (built: Dec 16 2021 18:14:46) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.27, Copyright (c), by Zend Technologies

安装过程细节

安装过程截图
$ brew install php@7.4

To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /usr/local/etc/php/7.4/

php@7.4 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have php@7.4 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.profile
  echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.profile

For compilers to find php@7.4 you may need to set:
  export LDFLAGS="-L/usr/local/opt/php@7.4/lib"
  export CPPFLAGS="-I/usr/local/opt/php@7.4/include"


To restart php@7.4 after an upgrade:
  brew services restart php@7.4
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/php@7.4/sbin/php-fpm --nodaemonize
==> Summary
🍺  /usr/local/Cellar/php@7.4/7.4.27: 498 files, 72.3MB
==> Running `brew cleanup php@7.4`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

编译安装

这种方式的好处<推荐>

  1. 原理很清楚,所有的系统都通用
  2. 明确安装了哪些东西,位置,组件等
  3. 具体版本完全可以自己去指定

基本步骤

  1. 下载资源包,地址 https://www.php.net/releases/
# 下载资源包
cd /tmp
wget https://www.php.net/distributions/php-8.1.2.tar.gz

docker 环境搭建

WIP


PHP环境LAMP/LNMP安装与配置

本课程适于PHP初学者,属于PHP开发环境的安装。LAMP是 Linux + Apache/Nginx + Mysql + PHP的简称。本课程和您一起,手把手安装配置LAMP环境。目前,国内外,无论一线互联网企业还是创业公司,LAMP都是Web开发的主流,以其高性能、特稳定、开源免费等特性闻名于世。

  • 1-1 LAMP是什么?
  • 2-1 LAMP之安装Linux
  • 3-1 LAMP之安装PHP
  • 6-1 LAMP之安装nginx

php-fpm: 

  • FastCGI管理器,只适用于php(说了等于没说)
  • cgi: 为了保证web传过来的程序是标准程序?

测试一下

php 的安装是否完成,用 phpinfo(); 来查看。

# vim test.php
<?php
	phpinfo();
# 用系统中的php命令运行php程序
php test.php

参考