Mac下使用Brew搭建PHP(LNMP/LAMP)开发环境

Mac下搭建lamp开发环境很容易,有xampp和mamp现成的集成环境。但是集成环境对于经常需要自定义一些配置的开发者来说会非常麻烦,而且Mac本身自带apache和php,在brew的帮助下非常容易手动搭建,可控性很高。

MAC OS X 10.9.5系统里已经自带了 apache  和 php
,只是默认没有启用,我们将他们启用即可。
APACHE的默认版本是 Apache/2.2.26 (Unix),php版本是PHP 5.4.30。

Brew

由于调试程序需要,我需要同时安装低版本的php5.3,但是又不希望删除系统预装的php
5.4,或升级/降级系统的php5.4,那么怎么办呢?这个时候,就可以通过brew的方式安装新的php版本。

第一步,先安装 brew

brew对于mac,就像apt-get对于ubuntu,安装软件的好帮手,不能方便更多…

Brew 是 Mac 下面的包管理工具,通过 Github 托管适合 Mac 的编译配置以及
Patch,可以方便的安装开发工具。 Mac 自带ruby
所以安装起来很方便,同时它也会自动把git也给你装上。官方网站:

brew的安装方式如下:

在mac下终端里直接输入命令行:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"

ruby -e “$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/install)”

brew常用选项

安装完成之后,建议执行一下自检:brew doctor 如果看到Your system is ready
to brew. 那么你的brew已经可以开始使用了。

brew install xxx
brew uninstall xxx
brew list 
brew update xxx

常用命令: (所有软件以PHP5.5为例子)

Apache || Nginx

brew update                       
#更新brew可安装包,建议每次执行一下
brew search php55                  #搜索php5.5
brew tap josegonzalez/php         
#安装扩展<gihhub_user/repo>  
brew tap                           #查看安装的扩展列表
brew install php55                 #安装php5.5
brew remove  php55                 #卸载php5.5
brew upgrade php55                 #升级php5.5
brew options php55                 #查看php5.5安装选项
brew info    php55                 #查看php5.5相关信息
brew home    php55                 #访问php5.5官方网站
brew services list                 #查看系统通过 brew 安装的服务
brew services cleanup              #清除已卸载无用的启动配置文件
brew services restart php55        #重启php-fpm

第二步,安装PHP

   
先添加brew的PHP扩展库:

Apache

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

Apache的话使用mac自带的基本就够了,我的系统是10.9,可以使用以下命令控制Apache

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

sudo apachectl start
sudo apachectl restart
sudo apachectl stop

brew install php53 –with-apache –with-gmp –with-imap –with-tidy
–with-debug

唯一要改的是主目录,mac默认在home下有个sites(站点)目录,访问路径是

请注意:如果你希望以mac下的apache作为web server,编译时要加
–with-apache;如果你的web server 是 nginx这类,就需要加上
–with-fpm。

http://localhost/~user_name

Options
–disable-opcache
Build without Opcache extension
–disable-zend-multibyte
Disable auto-detection of Unicode encoded scripts
–homebrew-apxs
Build against apxs in Homebrew prefix
–with-apache
Enable building of shared Apache 2.0 Handler module, overriding any
options which disable apache
–with-cgi
Enable building of the CGI executable (implies –without-apache)
–with-debug
Compile with debugging symbols
–with-fpm
Enable building of the fpm SAPI executable (implies –without-apache)
–with-gmp
Build with gmp support
–with-homebrew-curl
澳门新葡亰娱乐在线,Include Curl support via Homebrew
–with-homebrew-libxslt
Include LibXSLT support via Homebrew
–with-homebrew-openssl
Include OpenSSL support via Homebrew
–with-imap
Include IMAP extension
–with-libmysql
Include (old-style) libmysql support instead of mysqlnd
–with-mssql
Include MSSQL-DB support
–with-pdo-oci
Include Oracle databases (requries ORACLE_HOME be set)
–with-phpdbg
Enable building of the phpdbg SAPI executable (PHP 5.4 and above)
–with-postgresql
Build with postgresql support
–with-thread-safety
Build with thread safety
–with-tidy
Include Tidy support
–without-bz2
Build without bz2 support
–without-mysql
Remove MySQL/MariaDB support
–without-pcntl
Build without Process Control support
–without-pear
Build without PEAR
–without-snmp
Build without SNmp support
–HEAD
Install HEAD version

这样很不适合做开发用,修改/etc/apache2/httpd.conf内容

HP编译过程中如果遇到 configure: error: Cannot find OpenSSL’s
<evp.h> 错误,执行
xcode-select –install
重新安装一下 Xcode Command Line Tools
即可解决该错误(来源:

DocumentRoot "/Users/username/Sites"
<Directory />
    Options Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

安装完php后,会有一段提示,请仔细阅读:

这样sites目录就是网站根目录了,代码都往这个下头丢

Caveats
To enable PHP in Apache add the following to httpd.conf and restart
Apache:
    LoadModule php5_module   
/usr/local/opt/php53/libexec/apache2/libphp5.so

Nginx

The php.ini file can be found in:
    /usr/local/etc/php/5.3/php.ini

要使用Nginx也比较方便,首先安装

✩✩✩✩ PEAR ✩✩✩✩

brew install nginx

If PEAR complains about permissions, ‘fix’ the default PEAR permissions
and config:
    chmod -R ug+w /usr/local/Cellar/php53/5.3.29/lib/php
    pear config-set php_ini /usr/local/etc/php/5.3/php.ini

启动关闭Nginx的命令如下(如果想要监听80端口,必须以管理员身份运行)

✩✩✩✩ Extensions ✩✩✩✩

#打开 nginx
sudo nginx
#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit
#测试配置是否有语法错误
nginx -t

If you are having issues with custom extension compiling, ensure that
you are using the brew version, by placing /usr/local/bin before
/usr/sbin in your PATH:

配置Nginx

      PATH=”/usr/local/bin:$PATH”

cd /usr/local/etc/nginx/
mkdir conf.d

PHP53 Extensions will always be compiled against this PHP. Please
install them
using –without-homebrew-php to enable compiling against system PHP.

修改Nginx配置文件

✩✩✩✩ PHP CLI ✩✩✩✩

vim nginx.conf

If you wish to swap the PHP you use on the command line, you should add
the following to ~/.bashrc,
~/.zshrc, ~/.profile or your shell’s equivalent configuration file:

主要修改位置是最后的include

export PATH=”$(brew –prefix homebrew/php/php53)/bin:$PATH”

worker_processes  1;  

error_log       /usr/local/var/log/nginx/error.log warn;

pid        /usr/local/var/run/nginx.pid;

events {
    worker_connections  256;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log      /usr/local/var/log/nginx/access.log main;
    port_in_redirect off;
    sendfile        on; 
    keepalive_timeout  65; 

    include /usr/local/etc/nginx/conf.d/*.conf;
}

To have launchd start php53 at login:
    ln -sfv /usr/local/opt/php53/*.plist ~/Library/LaunchAgents
Then to load php53 now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php53.plist
==> Summary
   /usr/local/Cellar/php53/5.3.29: 480 files, 31M, built in 12.9
minutes

修改自定义文件

   
等待PHP编译完成,开始安装PHP常用扩展,扩展安装过程中
brew会自动安装依赖包,使用 brew search php53-
命令,可以查看还有哪些扩展可以安装,然后执行 brew install php53-XXX
就可以了。

vim ./conf.d/default.conf

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

增加一个监听端口

echo ‘export PATH=”$(brew –prefix homebrew/php/php53)/bin:$PATH”‘
>> ~/.bash_profile  #for php
echo ‘export PATH=”$(brew –prefix homebrew/php/php53)/sbin:$PATH”‘
>> ~/.bash_profile  #for php-fpm
echo ‘export PATH=”/usr/local/bin:/usr/local/sbin:$PATH”‘ >>
~/.bash_profile #for other brew install soft
source ~/.bash_profile  #更新配置

server {
    listen       80;
    server_name  localhost;

    root /Users/username/Sites/; # 该项要修改为你准备存放相关网页的路径

    location / { 
        index index.php;
        autoindex on; 
    }   

    #proxy the php scripts to php-fpm  
    location ~ .php$ {
        include /usr/local/etc/nginx/fastcgi.conf;
        fastcgi_intercept_errors on; 
        fastcgi_pass   127.0.0.1:9000; 
    }   

}

如何卸载安装的 php5.3呢?

这个时候还不能访问php站点,因为还没有开启php-fpm。

# 卸载
brew uninstall php53
# 清除缓存以及老旧版本文件
brew cleanup -s

虽然mac
10.9自带了php-fpm,但是由于我们使用了最新的PHP,PHP中自带php-fpm,所以使用PHP中的php-fpm可以保证版本的一致。

测试一下效果:

这里的命令在安装完下一步的php后再执行

$ php -v
PHP 5.3.29 (cli) (built: Jan 24 2015 12:40:58) (DEBUG)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies

sudo nginx
sudo php-fpm -D

# mac系统自带的php
$ /usr/bin/php -v
PHP 5.4.30 (cli) (built: Jul 29 2014 23:43:29)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend Guard Loader v3.3, Copyright (c) 1998-2013, by Zend
Technologies

发表评论

电子邮件地址不会被公开。 必填项已用*标注

相关文章

网站地图xml地图