首页 > 编程技术 > php

MAC利用brew命令安装PHP7环境的方法

发布时间:2016-11-25 15:27

brew 又叫Homebrew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件, 只需要一个命令, 非常方便brew类似ubuntu系统下的apt-get的功能,下面我们来看MAC利用brew安装PHP7环境的方法

最近几年一直在LNMP环境下进行开发,其中的PHP还是5.5版本,有点老旧,去年12月PHP7已经正式发布,新增了一些语法特性,最大的亮点是性能的提升,所以一直想升级到PHP7,由于时间关系一直拖到现在。到写本文为止PHP最新版本为:7.0.9的stable,所以稳定性上基本不用担心。

之前写的 MAC下安装LNMP环境 是用brew安装的,这次升级PHP7继续使用brew, 安装/升级软件很方便。

升级brew

brew update
配置

brew tap homebrew/dupes
brew tap homebrew/versions 
brew tap homebrew/homebrew-php
安装PHP7

brew install php70
遇到的问题

安装提示:

➜  ~ brew install php70
==> Installing php70 from homebrew/php
Error: Cannot install homebrew/php/php70 because conflicting formulae are installed.

  php55: because different php versions install the same binaries.

Please `brew unlink php55` before continuing.

Unlinking removes a formula's symlinks from /usr/local. You can
link the formula again after the install finishes. You can --force this
install, but the build may fail or cause obscure side-effects in the
resulting software.
根据提示执行: brew unlink php55
继续安装: brew install php70

配置文件

安装好后生成的配置文件都在 /usr/local/etc/php/7.0 目录里,分别如下:

php.ini /usr/local/etc/php/7.0/php.ini
php-fpm.conf /usr/local/etc/php/7.0/php-fpm.conf
php, phpize, php-config ls /usr/local/opt/php70/bin
php-fpm /usr/local/opt/php70/sbin/php-fpm

加入开机启动

mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/php70/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist
命令行配置

vim ~/.zshrc

export PATH="$(brew --prefix php55)/bin:$PATH"
替换为
export PATH="$(brew --prefix homebrew/php/php70)/bin:$PATH"

alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php5.plist"
alias php-fpm.restart='php-fpm.stop && php-fpm.start'
修改为

alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist"
alias php-fpm.restart='php-fpm.stop && php-fpm.start'
重新加载配置文件

source ~/.zshrc
验证版本

PHP版本

➜  ~ /usr/local/opt/php70/bin/php -v
PHP 7.0.9 (cli) (built: Jul 21 2016 14:50:47) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
php-fpm版本

➜  ~ /usr/local/opt/php70/sbin/php-fpm -v
PHP 7.0.9 (fpm-fcgi) (built: Jul 21 2016 14:50:51)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
启动PHP-FPM

执行: php-fpm.start

检查是否启动成功:

➜  ~ ps aux|grep php-fpm
qloog           60380   0.2  0.0  2432792    604 s001  R+    6:00PM   0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn php-fpm
qloog           60378   0.0  0.0  2473348    620   ??  S     6:00PM   0:00.00 /usr/local/opt/php70/sbin/php-fpm --fpm-config /usr/local/etc/php/7.0/php-fpm.conf
qloog           60377   0.0  0.0  2473348    648   ??  S     6:00PM   0:00.00 /usr/local/opt/php70/sbin/php-fpm --fpm-config /usr/local/etc/php/7.0/php-fpm.conf
qloog           60372   0.0  0.0  2475396   7844   ??  S     6:00PM   0:00.03 /usr/local/opt/php70/sbin/php-fpm --fpm-config /usr/local/etc/php/7.0/php-fpm.conf
安装扩展

yaf

brew install php70-yaf

配置文件
/usr/local/etc/php/7.0/conf.d/ext-yaf.ini

memcached

brew install php70-memcached

配置文件
/usr/local/etc/php/7.0/conf.d/ext-memcached.ini

redis

brew install php70-redis

配置文件
/usr/local/etc/php/7.0/conf.d/ext-redis.ini

xdebug

brew install php70-xdebug

配置文件
/usr/local/etc/php/7.0/conf.d/ext-xdebug.ini

swoole

brew install php70-swoole

配置文件
/usr/local/etc/php/7.0/conf.d/ext-swoole.ini

更多扩展执行 brew install 扩展名 进行安装

在php5.3.9添加了max_input_vars函数它限制了提交数据的长度了,如果我们长度限制100而提交的数据为200那么就会只保存前面100个字符哦,下面来看看。

公司运营的论坛被网络攻击,无奈没有很好的解决方法,就只有一个个的封IP地址,最终封了1600多个ip段,2个月后网络攻击逐步减少,担心会屏蔽正常用户,所以开始解封一批国内的字段的ip地址,论坛程序是discuz,由于discuz在禁止ip字段页面没有做分页功能,其导致1600多个字段同时读取在页面上。

问题来了,删除ip字段时程序不响应,在记录数1000条以上,不管是提交一条删除记录,还是全部删除记录,都是没效果的。

1000条记录下,删除功能失效

1000条记录下,删除功能失效

最终在discuz官方论坛管理员帮助下解决!

discuz官方 

discuz官方

解决方法:在php.ini里面加入

max_input_vars,指的是表达提交的数量,默认值为 1000。官方添加这个变量的目的是为了更好的保护服务器不受攻击。避免堵塞。

max_input_vars = 2000

实例代码:

max_execution_time = 30     ; Maximum execution time of each script, in seconds
max_input_time = 60     ; Maximum amount of time each script may spend parsing request data
max_input_vars = 2000

重启php-fpm后,功能正常了。

下面我们来看一篇关于PHP 构建使用 igbinary 作为序列化组件的 memcached 模块的教程希望这篇教程能够给各位同学带来帮助。

无论是从 PECL 安装 php5-memcached 还是从 apt-get 命令安装,安装上的 memcached 模块都是不带有 igbinary 序列化组件支持的,由于 igbinary 有很大的性能优势,所以尽量使用 igbinary 作为 memcached 的序列化组件。

环境说明

操作系统:Ubuntu Server 14.04 64-bit
PHP:PHP 5.5.9

构建过程

首先,安装 PHP 开发版

yuanyu@usvr:~$ sudo apt-get install php5-dev
 
然后安装依赖库

yuanyu@usvr:~$ sudo apt-get install libevent-dev
yuanyu@usvr:~$ sudo apt-get install pkg-config
 
接下来,安装 igbinary 模块

yuanyu@usvr:~$ sudo pecl install igbinary
 
从源码构建 libmemcached

获取 libmemcached 的源码,这里使用的是 1.0.18 版本

yuanyu@usvr:~$ cd tmp
yuanyu@usvr:~/tmp$ wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
 
解压缩,配置,并且进行构建

yuanyu@usvr:~/tmp$ tar xzvf libmemcached-1.0.18.tar.gz
yuanyu@usvr:~/tmp$ cd libmemcached-1.0.18
yuanyu@usvr:~/tmp/libmemcached-1.0.18$ ./configure
yuanyu@usvr:~/tmp/libmemcached-1.0.18$ make
yuanyu@usvr:~/tmp/libmemcached-1.0.18$ sudo make install

从源码构建 php5-memcached

使用 pecl 获取 php5-memcached 的源码,然后配置的时候启用 --enable-memcached-igbinary。

yuanyu@usvr:~/tmp$ pecl download memcached-2.2.0
yuanyu@usvr:~/tmp$ tar xzvf memcached-2.2.0.tgz
yuanyu@usvr:~/tmp$ cd memcached-2.2.0
yuanyu@usvr:~/tmp/memcached-2.2.0$ phpize
yuanyu@usvr:~/tmp/memcached-2.2.0$ ./configure --enable-memcached-igbinary --disable-memcached-sasl
yuanyu@usvr:~/tmp/memcached-2.2.0$ make
yuanyu@usvr:~/tmp/memcached-2.2.0$ sudo make install
 
配置 PHP,加入新构建的两个模块

上面的构建步骤完成之后,就会在 PHP 扩展模块产生 2 个 so 文件:igbinary.so 和 memcached.so,将这个两个文件配置到你的运行环境中即可。

下面我们来看一篇关于 安装php的parsekit扩展查看opcode的操作教程希望这篇文章能够对各位带来帮助,具体的如下。

我们知道PHP是一门解释型语言,用它们编写的动态内容都需要依赖相应的解释器程序来运行,解释器程序需要对输入的脚本代码进行分析,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode)。

想要查看php程序的opcode,需要安装php的parsekit扩展。
安装过程如下:
[root@localhost ~]# wget http://pecl.php.net/get/parsekit-1.3.0.tgz
[root@localhost ~]# tar zxvf parsekit-1.3.0.tgz
[root@localhost ~]# cd parsekit-1.3.0
[root@localhost parsekit-1.3.0]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519
[root@localhost parsekit-1.3.0]# ./configure -with-php-config=/usr/local/php/bin/php-config
[root@localhost parsekit-1.3.0]# make
[root@localhost parsekit-1.3.0]# make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
然后编辑php.ini文件,在里面加上
extension="parsekit.so"
[root@localhost parsekit-1.3.0]# vim /usr/local/php/etc/php.ini
这样我们就成功的安装了parsekit扩展了(注:楼主是在php5.2下安装的parsekit扩展,在php5.4,php5.5下会提示编译失败)。

当然还有一种更简单的安装parsekit的方法,那就是直接使用pecl。
在命令行下输入命令:
[root@localhost parsekit-1.3.0]# /usr/local/php/bin/pecl install parsekit
这样就可以自动帮你安装parsekit了。

然后,我们在命令行下输入:
[root@localhost parsekit-1.3.0]# /usr/local/php/bin/php -r "var_dump(parsekit_compile_string('print 1+1;'));"
这样就可以查看php代码的opcode了。
结果如下:
array(20) {
  ["type"]=>
  int(4)
  ["type_name"]=>
  string(14) "ZEND_EVAL_CODE"
  ["fn_flags"]=>
  int(0)
  ["num_args"]=>
  int(0)
  ["required_num_args"]=>
  int(0)
  ["pass_rest_by_reference"]=>
  bool(false)
  ["uses_this"]=>
  bool(false)
  ["line_start"]=>
  int(0)
  ["line_end"]=>
  int(0)
  ["return_reference"]=>
  bool(false)
  ["refcount"]=>
  int(1)
  ["last"]=>
  int(5)
  ["size"]=>
  int(5)
  ["T"]=>
  int(2)
  ["last_brk_cont"]=>
  int(0)
  ["current_brk_cont"]=>
  int(4294967295)
  ["backpatch_count"]=>
  int(0)
  ["done_pass_two"]=>
  bool(true)
  ["filename"]=>
  string(17) "Parsekit Compiler"
  ["opcodes"]=>
  array(5) {
    [0]=>
    array(8) {
      ["address"]=>
      int(29981148)
      ["opcode"]=>
      int(1)
      ["opcode_name"]=>
      string(8) "ZEND_ADD"
      ["flags"]=>
      int(197378)
      ["result"]=>
      array(3) {
        ["type"]=>
        int(2)
        ["type_name"]=>
        string(10) "IS_TMP_VAR"
        ["var"]=>
        int(0)
      }
      ["op1"]=>
      array(3) {
        ["type"]=>
        int(1)
        ["type_name"]=>
        string(8) "IS_CONST"
        ["constant"]=>
        &int(1)
      }
      ["op2"]=>
      array(3) {
        ["type"]=>
        int(1)
        ["type_name"]=>
        string(8) "IS_CONST"
        ["constant"]=>
        &int(1)
      }
      ["lineno"]=>
      int(1)
    }
    [1]=>
    array(7) {
      ["address"]=>
      int(29981268)
      ["opcode"]=>
      int(41)
      ["opcode_name"]=>
      string(10) "ZEND_PRINT"
      ["flags"]=>
      int(770)
      ["result"]=>
      array(3) {
        ["type"]=>
        int(2)
        ["type_name"]=>
        string(10) "IS_TMP_VAR"
        ["var"]=>
        int(1)
      }
      ["op1"]=>
      array(3) {
        ["type"]=>
        int(2)
        ["type_name"]=>
        string(10) "IS_TMP_VAR"
        ["var"]=>
        int(0)
      }
      ["lineno"]=>
      int(1)
    }
    [2]=>
    array(7) {
      ["address"]=>
      int(29981388)
      ["opcode"]=>
      int(70)
      ["opcode_name"]=>
      string(9) "ZEND_FREE"
      ["flags"]=>
      int(271104)
      ["op1"]=>
      array(4) {
        ["type"]=>
        int(2)
        ["type_name"]=>
        string(10) "IS_TMP_VAR"
        ["var"]=>
        int(1)
        ["EA.type"]=>
        int(0)
      }
      ["op2"]=>
      array(3) {
        ["type"]=>
        int(8)
        ["type_name"]=>
        string(9) "IS_UNUSED"
        ["opline_num"]=>
        string(1) "0"
      }
      ["lineno"]=>
      int(1)
    }
    [3]=>
    array(7) {
      ["address"]=>
      int(29981508)
      ["opcode"]=>
      int(62)
      ["opcode_name"]=>
      string(11) "ZEND_RETURN"
      ["flags"]=>
      int(16777984)
      ["op1"]=>
      array(3) {
        ["type"]=>
        int(1)
        ["type_name"]=>
        string(8) "IS_CONST"
        ["constant"]=>
        &NULL
      }
      ["extended_value"]=>
      int(0)
      ["lineno"]=>
      int(1)
    }
    [4]=>
    array(5) {
      ["address"]=>
      int(29981628)
      ["opcode"]=>
      int(149)
      ["opcode_name"]=>
      string(21) "ZEND_HANDLE_EXCEPTION"
      ["flags"]=>
      int(0)
      ["lineno"]=>
      int(1)
    }
  }
}

标签:[!--infotagslink--]

您可能感兴趣的文章: