php dev for mac - yaokun123/php-wiki GitHub Wiki

macOS Sierra 已经帮我们预装了 Ruby、PHP(5.6)、Perl、Python 等常用的脚本语言,以及 Apache HTTP 服务器。由于 nginx 既能作为 HTTP 服务器也能作为反向代理服务器,且配置简单,这里我们用 nginx 代替 Apache 作为我们默认的 HTTP 服务器。

下面是我在 macOS Sierra 配置的 PHP 开发环境:

一、安装命令行终端

这里我们选择 iTerm2,iTerm2 功能强大,可以替代系统默认的命令行终端。下载解压后,将iTerm2 直接拖入"应用程序"目录。

防止ssh断连:在客户端的 ~/.ssh/ 文件夹中添加 config 文件,并添加配置: ServerAliveInterval 60

详细:http://www.cnblogs.com/dudu/archive/2013/02/07/ssh-write-failed-broken-pipe.html

二、安装 IDE

这里我们选择 JetBrains PhpStorm 作为集成开发环境。

三、安装 Xcode

Xcode 是苹果出品的包含一系列工具及库的开发套件。

通过 App Store 安装最新版本的 Xcode。我们一般不会用 Xcode 来开发 PHP 项目。但这一步也是必需的,因为 Xcode 会帮你附带安装一些如 Git 等必要的软件。当然你也可以通过源码包安装 Git。

四、安装 Xcode Command Line Tools

这一步会帮你安装许多常见的基于 Unix 的工具。Xcode 命令行工具作为 Xcode 的一部分,包含了 GCC 编译器。在命令行中执行以下命令即可安装:

xcode-select --install

五、安装包管理器

Homebrew 作为 macOS 不可或缺的套件管理器,用来安装、升级以及卸载常用的软件。在命令行中执行以下命令即可安装:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安装后可以修改 Homebrew 源,国外源一直不是很给力,这里我们将 Homebrew 的 git 远程仓库改为中国科学技术大学开源软件镜像:

cd "$(brew --repo)"
git remote set-url origin git://mirrors.ustc.edu.cn/brew.git

六、安装 HTTP 服务器

这里我们选择 nginx 代替系统自带的 Apache,作为我们的 HTTP 服务器:

brew install nginx

安装完成后,nginx 的一些常用命令:

sudo nginx # 启动 nginx 服务
nginx -h # nginx 帮助信息
sudo nginx -s stop|quit|reopen|reload # 停止|退出|重启|重载 nginx 服务

七、安装数据库

这里我们选择 MySQL 作为我们的数据库服务器:

brew install mysql

安装完成后,启动 MySQL:

mysqld

进入 MySQL 服务器:

mysql -u root -p

八、开启 PHP-FPM

nginx 本身不能处理 PHP,它只是个 HTTP 服务器,当接收一个 PHP 请求后,nginx 会将其交由 PHP 解释器处理,并把结果返回给客户端。nginx 一般是把请求发 FastCGI 管理进程处理,FastCGI 管理进程选择 CGI 子进程处理结果并返回被 nginx。

PHP-FPM是一个 PHP FastCGI 管理器,一开始只是 PHP 源代码的一个补丁,旨在将 FastCGI 进程管理整合进 PHP 包中。必须将它 patch 到 PHP 源代码中,在编译安装 PHP 后才可以使用。PHP 从版本 5.3 开始官方集成 PHP-FPM。

添加 PHP-FPM 的配置文件:

cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
php-fpm --fpm-config /private/etc/php-fpm.conf

修改 PHP-FPM 的 error_log 路径:

vi /var/log/php-fpm.log # 新建文件
vi /private/etc/php-fpm.conf # 将 error_log=log/php-fpm.log 修改为:error_log = /var/log/php-fpm.log,保存

启动 PHP-FPM:

sudo php-fpm

关闭 PHP-FPM:

ps aux|grep php-fpm
sudo kill php-fpm min pid # 杀死 php-fpm 最小的进程id

九、配置 nginx.conf 文件

通过以下命令可以查看 nginx.conf 文件的位置:

nginx -h

输出:

nginx version: nginx/1.10.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/Cellar/nginx/1.10.1/)
  -c filename   : set configuration file (default: /usr/local/etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

打开配置文件:

vi /usr/local/etc/nginx/nginx.conf

在文件末尾可以看到:

include servers/*;

它将同目录下的servers目录里的文件都包含了进来,由此,我们可以在servers文件里创建开发项目的配置信息:

cd servers/
vi test.conf

将以下配置信息,写入 test.conf文件中:

server {
    listen       8099;
    server_name  localhost;
    root /home/www/php_project;
    rewrite . /index.php;
    location / {
        index index.php index.html index.htm;
        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;
    }
}

在上述的/home/www/php_project的目录下,我们创建一个 index.php文件:

cd /home/www/php_project
vi test.php

写入内容:

<? php
phpinfo();

重启 nginx:

nginx -s reload

打开浏览器,访问localhost:8099。可以看到关于 PHP 配置的信息。 至此,MNMP(MacOS-nginx-MySQL-PHP)环境已经搭建完成。