1. 程式人生 > >Mac下php版本切換

Mac下php版本切換

本文介紹在Mac開發環境下怎樣進行php版本的切換

前言

預設php的安裝方式是homebrew,如果不是,那就別看了。。。

安裝php多版本

Mac下預設安裝了php但是版本不是很高,用php -v檢視php版本

1
2
3
4
5
6
localhost:~ Ken$ php -v
PHP 5.6.17 (cli) (built: Jan  8 2016 10:27:48) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    with Xdebug v2.3
.3, Copyright (c) 2002-2015, by Derick Rethans localhost:~ Ken$

是php 5.6,我們希望安裝php7又不把php5.6解除安裝,咋辦呢?
首先,我們先用 brew install php70 安裝php7

1
2
3
4
5
6
7
8
9
10
11
12
localhost:~ Ken$ brew install php70
==> Installing php70 from homebrew/php
Error: Cannot install homebrew/php/php70 because conflicting formulae are
installed. php56: because different php versions install the same binaries. Please `brew unlink php56` 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取消homebrew與php56的關聯,再安裝php7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
localhost:~ Ken$ brew install php70
==> Installing php70 from homebrew/php
==> Downloading https://homebrew.bintray.com/bottles-php/php70-7.
###################################################################### 100.0%
==> Pouring php70-7.0.2.el_capitan.bottle.10.tar.gz
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module    /usr/local/opt/php70/libexec/apache2/libphp7.so
    
    <FilesMatch .php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

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

✩✩✩✩ Extensions ✩✩✩✩

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:

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

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

✩✩✩✩ PHP CLI ✩✩✩✩

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:

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

✩✩✩✩ FPM ✩✩✩✩

To launch php-fpm on startup:
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/opt/php70/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist

The control script is located at /usr/local/opt/php70/sbin/php70-fpm

OS X 10.8 and newer come with php-fpm pre-installed, to ensure you are using the brew version you need to make sure /usr/local/sbin is before /usr/sbin in your PATH:

  PATH="/usr/local/sbin:$PATH"

You may also need to edit the plist to use the correct "UserName".

Please note that the plist was called 'homebrew-php.josegonzalez.php70.plist' in old versions
of this formula.

To have launchd start homebrew/php/php70 at login:
  ln -sfv /usr/local/opt/php70/*.plist ~/Library/LaunchAgents
Then to load homebrew/php/php70 now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php70.plist
==> Summary
�  /usr/local/Cellar/php70/7.0.2: 331 files, 49.3M

顯示安裝成功!

安裝php-version

此時我們再安裝php-version ==> php版本切換工具
安裝命令:

1
2
brew install php-version
source $(brew --prefix php-version)/php-version.sh && php-version 5

執行php-version檢視已存在的php版本,前面帶*的是當前環境正在使用的php版本,使用php-versiom+版本號的方式切換php版本~

1
2
3
4
5
6
7
8
9
10
localhost:~ Ken$ php-version
  5.5.30
* 5.6.17
  7.0.2
localhost:~ Ken$ php-version 7.0.2
localhost:~ Ken$ php -v
PHP 7.0.2 (cli) (built: Jan  7 2016 10:40:26) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
localhost:~ Ken$