PHP Development Guide

PHP 开发指引

安装

CentOS

安装 PHP 7.0+ 需要安装扩展 Yum 源

# PHP 7.0.x from IUS
sudo yum install mod_php70u php70u-mysqlnd php70u-pecl-redis php70u-xml php70u-gd php70u-devel php70u-mbstring php70u-opcache php70u-pdo php70u-pear php70u-soap

macOS

Brew 可以安装多个版本的 PHP 环境。

命令 作用
brew install php@7.3 安装 PHP 7.3
brew-php-switcher 7.3 切换 /usr/local/bin 下连接的 php 环境

Ubuntu

凭借第三方安装源 Ubuntu 可实现多版本 PHP 共存。

export LC_ALL=C.UTF-8
sudo add-apt-repository ppa:ondrej/php
sudo add-apt-repository ppa:ondrej/nginx           # Stable
sudo add-apt-repository ppa:ondrej/nginx-mainline  # New Features
sudo add-apt-repository ppa:ondrej/apache2
# PHP 7.0
sudo apt install php7.0 php7.0-mbstring php7.0-mysql php7.0-opcache php7.0-json php7.0-xmlrpc php7.0-readline php7.0-zip php7.0-gd php7.0-bcmath php7.0-gd php7.0-fpm php7.0-curl php7.0-cli php7.0-xml php7.0-mcrypt
# PHP 7.1
sudo apt install php7.1 php7.1-mbstring php7.1-mysql php7.1-opcache php7.1-json php7.1-xmlrpc php7.1-zip php7.1-bcmath php7.1-gd php7.1-fpm php7.1-curl php7.1-cli php7.1-xml php7.1-mcrypt php-redis
# PHP 7.2
sudo apt install php7.2 php7.2-mbstring php7.2-mysql php7.2-opcache php7.2-json php7.2-xmlrpc php7.2-zip php7.2-bcmath php7.2-gd php7.2-fpm php7.2-curl php7.2-cli php7.2-xml php-tokenizer php-redis
# PHP 7.3
sudo apt install php7.3 php7.3-mbstring php7.3-mysql php7.3-opcache php7.3-json php7.3-xmlrpc php7.3-zip php7.3-bcmath php7.3-gd php7.3-fpm php7.3-curl php7.3-cli php7.3-xml php-tokenizer php-redis
# PHP 7.4
sudo apt install php7.4 php7.4-mbstring php7.4-mysql php7.4-opcache php7.4-json php7.4-xmlrpc php7.4-zip php7.4-bcmath php7.4-gd php7.4-fpm php7.4-curl php7.4-cli php7.4-xml php-tokenizer php-redis
# 切换默认 PHP 版本
update-alternatives --config php
# Apache2 + PHP 默认版本 @ Ubuntu 16.04
sudo apt install libapache2-mod-php php7.0 php7.0-mbstring php7.0-mysql php7.0-xmlrpc php7.0-readline php7.0-zip php7.0-gd php7.0-bcmath php7.0-gd php7.0-curl php7.0-cli php7.0-xml php7.0-mcrypt php7.0-fpm php-redis
# Nginx 完整版
sudo apt install nginx-extras

MAMP

安装 pecl 扩展

MAMP 支持多个版本的 PHP 引擎,安装扩展时需要先用 export 调整 PATH 来调整默认的 PHP 版本:

export PATH=/Applications/MAMP/bin/php/php7.0.26/bin:$PATH
pecl install redis

 装完扩展后在 MAMP 中编辑对应的 PHP 配置模版激活扩展:

extension=redis.so

常用命令

启动内置 http 服务

php -S 0.0.0.0:8088

常用设置

OPcache

; 是否侦测文件改动
opcache.validate_timestamps=off
; 多少次请求后会检查文件是否跟新,默认为 2
opcache.revalidate_freq=0
; PHP 7.0+ 最高支持 100000 10 万
opcache.max_accelerated_files=100000

常见错误

未找到 autocconf

$ pecl install redis
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
downloading redis-4.2.0.tgz ...
Starting to download redis-4.2.0.tgz (235,569 bytes)
.................................................done: 235,569 bytes
25 source files, building
running: phpize
Configuring for:
PHP Api Version:         20151012
Zend Module Api No:      20151012
Zend Extension Api No:   320151012
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

解决方案

brew install autoconf
Author: njun
njun's picture
Updated: 2020/01/19