macOS PHP 开发环境搭建

1,505 阅读2分钟

1 安装 iTerm2

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

2 安装 PhpStorm

推荐 JetBrains PhpStorm 作为集成开发工具。

3 安装 Homebrew

  1. Homebrew 作为 macOS 不可或缺的套件管理器,用来安装、升级以及卸载常用的软件。在命令行中执行以下命令即可安装:
# 使用系统自带的 ruby 安装 Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  1. 安装后可以修改 Homebrew 源,国外源一直不是很给力,这里我们将 Homebrew 的 git 远程仓库改为中国科学技术大学开源软件镜像
cd "$(brew --repo)"
git remote set-url origin git://mirrors.ustc.edu.cn/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

4 安装一些必要的工具包

brew install wget
brew install autoconf
brew install openssl

5 安装 nginx

  1. 这里我们选择 nginx 代替系统自带的 Apache,作为我们的 Web 服务器:
brew install nginx
  1. 安装完成后,nginx 的一些常用命令:
sudo nginx # 启动 nginx 服务
nginx -h # nginx 帮助信息
sudo nginx -s stop|quit|reopen|reload # 停止|退出|重启|重载 nginx 服务

6 安装 MySQL

  1. 推荐 MySQL 作为数据库服务器,当然,你也可以选择安装 PostgreSQL 或者 MariaDB。
brew install mysql
  1. 安装完成后,启动 MySQL:
mysql.server start
  1. 进入 MySQL 服务器:
mysql -u root -p

7 安装 Redis

  1. 推荐 Redis 作为 noSQL 数据库服务器:
brew install redis
  1. 安装完成后,启动 Redis:
redis-server &

8 安装 PHP 7.2

  1. 安装 PHP7.2 来代替系统自带的 PHP7.1:
brew tap homebrew/homebrew-core
brew install php72
echo export PATH="$(brew --prefix homebrew/php/php72)/bin:$PATH" >> ~/.bash_profile # 代替系统自带的 php
source ~/.bash_profile
  1. 替换系统 php-fpm
ln -s /usr/local/Cellar/php/php72/sbin/php-fpm  /usr/local/bin/php-fpm
  1. 验证 PHP 以及 PHP-FPM 版本:

    关闭命令行窗口,然后重新打开 item2,查看版本

php -v
php-fpm -v
  1. 启动 php-fpm:
sudo killall php-fpm && sudo php-fpm

Ps: 根据个人的实际开发场景,降版本安装 php5.* 也是用类似方法,即把 php72 替换为 php56

  1. 配置 nginx.conf 文件

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

nginx -h

输出:

nginx version: nginx/1.12.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.12.1/)
  -c filename   : set configuration file (default: /usr/local/etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

打开配置文件:

vim /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 文件:

vi /home/www/php_project/index.php

写入内容:

<?php
phpinfo();

重启 Nginx:

sudo nginx -s stop && sudo nginx

打开浏览器,访问localhost:8099。可以看到关于 PHP 配置的信息。

9 安装 Composer

Composer 是 PHP 用来管理依赖(dependency)关系的工具。你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖的库文件。

安装并启动国内镜像服务:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
composer config -g repo.packagist composer https://packagist.phpcomposer.com # 改为国内源

10 安装 PHP 扩展

  1. 安装 Redis 扩展:
wget https://pecl.php.net/get/redis-3.1.3.tgz # 下载源码包
tar -zxvf redis-3.1.3.tgz # 解压
cd redis-3.1.3
phpize # 生成编译配置                 
./configure # 编译配置检测
make # 编译
make install # 安装

扩展安装完成后,我们还需最后一步,修改php.ini文件,并重启 PHP-FPM:

# 追加 extension=redis.so
vi /usr/local/etc/php/7.2/php.ini

# 重启 php-fpm
sudo killall php-fpm && sudo php-fpm -D

# 查看是否安装成功
php -m |grep redis 
  1. 安装 Swoole 扩展
wget https://codeload.github.com/swoole/swoole-src/tar.gz/v4.2.9 #下载源码包
tar -zxvf swoole-src-4.2.9.tar.gz # 解压
cd swoole-src-4.2.9
phpize # 生成编译配置                 
./configure # 编译配置检测
make # 编译
make install # 安装

扩展安装完成后,我们还需最后一步,修改php.ini文件,并重启 PHP-FPM:

# 追加 extension=swoole.so
vi /usr/local/etc/php/7.2/php.ini

# 重启 php-fpm
sudo killall php-fpm && sudo php-fpm -D

# 查看是否安装成功
php -m |grep swoole