1. 程式人生 > >在Mac中如何安裝PHP7

在Mac中如何安裝PHP7

經歷了一個漫長的等待,在我們的PHP5.6釋出後,我們最新的PHP7終於釋出了(等等,我們的6到哪去了?),根據官方的說法,現在的PHP7要比PHP5.6快一倍,那麼我們如果要嚐鮮,怎麼去體驗呢?
其實我們有好幾種方法來安裝。
第一種,我們可以去官網上下原始碼去編譯,這個就不多說了。
第二種,我們可以使用我們的第三方包來安裝,比如說homebrew。本文就會講述如何使用這種方法安裝。

安裝方法

  1. 通過brew來安裝:

     $ brew tap homebrew/dupes  
     $ brew tap homebrew/versions  
     $ brew tap homebrew/homebrew-php  
    
     // 建立目錄 
    sudo mkdir /usr/local/var sudo chmod 777 /usr/local/var //修改成你自己的使用者名稱和組 sudo chown -R <username>:<group> /usr/local/sbin sudo mkdir /usr/local/sbin/ //安裝PHP7 $ brew unlink php56

    $ brew install php70 --with-httpd24

    //刪掉以前安裝的PHP5.6,必須加--with-httpd24 不然不會有libphp7.so檔案 //你可以根據自己的情況來修改 $ brew link php70 //大功告成
    $ php -v PHP 7.0.0 (cli) (built: Dec 2 2015 13:06:23) ( NTS ) Copyright (c) 1997-2015 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
  2. $ brew info php70
    //出現資訊放在附錄裡,仔細閱讀

    3.配置Apache 

    $ sudo vim /etc/apache2/httpd.conf
    找到這行註釋掉(大概在168行)
    #Comment out the PHP5 module   
    #LoadModule
    php5_module libexec/apache2/libphp5.so 找到這行並註釋掉(大概在最後一行)#Include /private/etc/apache2/other/*.conf 
    LoadModule php7_module /usr/local/opt/php70/libexec/apache2/libphp7.so    <FilesMatch .php$>      SetHandler application/x-httpd-php    </FilesMatch>    <IfModule php7_module>      AddType application/x-httpd-php .php      AddType application/x-httpd-php-source .phps      <IfModule dir_module>        DirectoryIndex index.html index.php      </IfModule>    </IfModule>
  3. 重啟Apache
     sudo apachectl restart
  4. 到你的Apache的預設目錄/Library/WebServer/Documents下面去增加一個info.php的檔案。
     <?php    
     phpinfo();    
     ?>

附錄:

########brew info php70 命令後的資訊########### ==> 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" GMP has moved to its own formula, please install it by running: brew install php70-gmp ✩✩✩✩ 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. With the release of macOS Sierra the Apache module is now not built by default. If you want to build it on your system you have to install php with the --with-apache option. See brew options php70 for more details. To have launchd start josegonzalez/php/php70 now and restart at login: brew services start josegonzalez/php/php70  ########brew info php70 命令後的資訊###########
########brew info php70 命令後的資訊########### ==> 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" GMP has moved to its own formula, please install it by running: brew install php70-gmp ✩✩✩✩ 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. With the release of macOS Sierra the Apache module is now not built by default. If you want to build it on your system you have to install php with the --with-apache option. See brew options php70 for more details. To have launchd start josegonzalez/php/php70 now and restart at login: brew services start josegonzalez/php/php70  ########brew info php70 命令後的資訊###########

########brew info php70 命令後的資訊########### ==> 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" GMP has moved to its own formula, please install it by running: brew install php70-gmp ✩✩✩✩ 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. With the release of macOS Sierra the Apache module is now not built by default. If you want to build it on your system you have to install php with the --with-apache option. See brew options php70 for more details. To have launchd start josegonzalez/php/php70 now and restart at login: brew services start josegonzalez/php/php70  ########brew info php70 命令後的資訊###########