【转】mac os 安装php

时间:2021-06-19 19:17:08

安装PHP

添加brew的PHP扩展库:

brew update
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

可以使用brew options php55命令来查看安装php5.5的选项,这里我用下面的选项安装:

brew install php55 --with-fpm --with-gmp --with-imap --with-tidy --with-debug --with-mysql --with-libmysql

PHP编译过程中如果遇到configure: error: Cannot find OpenSSL's <evp.h>错误,执行xcode-select --install 重新安装一下Xcode Command Line Tools 在GitHub HomeBrew上有关于这个讨论:
For future reference of anybody looking for Command Line Tools with Xcode 5, open up a Terminal window and type xcode-select --install. A window will appear informing you command line tools are required. Click Install and you should be good to go

等待PHP编译完成,开始安装PHP常用扩展,扩展安装过程中brew会自动安装依赖包,例如php55-pdo-pgsql 会自动装上postgresql,这里我安装以下PHP扩展:

brew install php55-apcu\
php55-gearman\
php55-geoip\
php55-gmagick\
php55-imagick\
php55-intl\
php55-mcrypt\
php55-memcache\
php55-memcached\
php55-mongo\
php55-opcache\
php55-pdo-pgsql\
php55-phalcon\
php55-redis\
php55-sphinx\
php55-swoole\
php55-uuid\
php55-xdebug;

扩展里面提一下php55-phalcon 和 php55-swoole. 一个是C语言写的PHP框架,安装来个人摸索熟悉一下,还没有真正的使用过,大致看了一下文档,感觉非常吊炸天。目前公司的项目是基于Yii2的,也看看这个框架。 另外一个swoole是国产的PHP高性能网络通信框架,貌似不错,可能在项目中会考虑用到它。

由于Mac自带了php和php-fpm,因此需要添加系统环境变量PATH来替代自带PHP版本。

echo 'export PATH="$(brew --prefix php55)/bin:$PATH"' >> ~/.bash_profile  #for php
echo 'export PATH="$(brew --prefix php55)/sbin:$PATH"' >> ~/.bash_profile #for php-fpm
echo 'export PATH="/usr/local/bin:/usr/local/sbib:$PATH"' >> ~/.bash_profile #for other brew install soft
source ~/.bash_profile

测试一下效果:

#brew安装的php 他在/usr/local/opt/php55/bin/php
php -v
PHP 5.5. (cli) (built: Jul ::) (DEBUG)
Copyright (c) - The PHP Group
Zend Engine v2.5.0, Copyright (c) - Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) -, by Zend Technologies
with Xdebug v2.2.5, Copyright (c) -, by Derick Rethans
#Mac自带的PHP
/usr/bin/php -v
PHP 5.4. (cli) (built: Jan ::)
Copyright (c) - The PHP Group
Zend Engine v2.4.0, Copyright (c) - Zend Technologies #brew安装的php-fpm 他在/usr/local/opt/php55/sbin/php-fpm
php-fpm -v
PHP 5.5. (fpm-fcgi) (built: Jul ::) (DEBUG)
Copyright (c) - The PHP Group
Zend Engine v2.5.0, Copyright (c) - Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) -, by Zend Technologies
with Xdebug v2.2.5, Copyright (c) -, by Derick Rethans #Mac自带的php-fpm
/usr/sbin/php-fpm -v
PHP 5.4. (fpm-fcgi) (built: Jan ::)
Copyright (c) - The PHP Group
Zend Engine v2.4.0, Copyright (c) - Zend Technologies

修改php-fpm配置文件,vim /usr/local/etc/php/5.5/php-fpm.conf,找到pid相关大概在25行,去掉注释 pid = run/php-fpm.pid, 那么php-fpm的pid文件就会自动产生在/usr/local/var/run/php-fpm.pid,下面要安装的Nginx pid文件也放在这里。

#测试php-fpm配置
php-fpm -t
php-fpm -c /usr/local/etc/php/5.5/php.ini -y /usr/local/etc/php/5.5/php-fpm.conf -t #启动php-fpm
php-fpm -D
php-fpm -c /usr/local/etc/php/5.5/php.ini -y /usr/local/etc/php/5.5/php-fpm.conf -D #关闭php-fpm
kill -INT `cat /usr/local/var/run/php-fpm.pid` #重启php-fpm
kill -USR2 `cat /usr/local/var/run/php-fpm.pid` #也可以用上文提到的brew命令来重启php-fpm,不过他官方不推荐用这个命令了
brew services restart php55 #还可以用这个命令来启动php-fpm
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist

启动php-fpm之后,确保它正常运行监听9000端口:

lsof -Pni4 | grep LISTEN | grep php
php-fpm calvin 9u IPv4 0xf11f9e8e8033a2a7 0t0 TCP 127.0.0.1: (LISTEN)
php-fpm calvin 0u IPv4 0xf11f9e8e8033a2a7 0t0 TCP 127.0.0.1: (LISTEN)
php-fpm calvin 0u IPv4 0xf11f9e8e8033a2a7 0t0 TCP 127.0.0.1: (LISTEN)
php-fpm calvin 0u IPv4 0xf11f9e8e8033a2a7 0t0 TCP 127.0.0.1: (LISTEN)

#正常情况,会看到上面这些进程

PHP-FPM开机启动:

ln -sfv /usr/local/opt/php55/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php55.plist

安装php composer

brew install composer
#检查一下情况
composer --version
Composer version 1.0.-alpha8 -- ::

redis memcached这些软件brew 已经自动依赖安装上,如果想开机自动启动,或者查看使用说明 brew info redis即可。另外,composer的中文文档:猛戳这里