Ubuntu上官方的源,比如 Ubuntu14.04 預設源中的是 PHP5.6.x、Ubuntu16.04 預設源中的是 PHP7.0.x,那麼如果想在 Ubuntu16.04 上安裝 PHP7.1,PHP7.2,應該怎麼辦呢?
答案是通過第三方的源來安裝,ppa:ondrej/php 是一個比較知名的PHP源(目前維護php5.6,php7.0,php7.1,php7.2)。下面就用這個源來安裝自己想要的PHP版本。
- 通過源安裝PHP
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install php7.1 # for PHP 7.1
sudo apt-get install php7.0 # for PHP 7.0
sudo apt-get install php5.6 # for PHP 5.6
- 安裝相關版本的PHP模組
sudo apt-cache search php7*
...
php-all-dev - package depending on all supported PHP development packages
php7.0 - server-side, HTML-embedded scripting language (metapackage)
php7.0-cgi - server-side, HTML-embedded scripting language (CGI binary)
php7.0-cli - command-line interpreter for the PHP scripting language
php7.0-common - documentation, examples and common module for PHP
php7.0-curl - CURL module for PHP
php7.0-dev - Files for PHP7.0 module development
php7.0-gd - GD module for PHP
php7.0-gmp - GMP module for PHP
...
...
...
php7.1-xml - DOM, SimpleXML, WDDX, XML, and XSL module for PHP
php7.1-xmlrpc - XMLRPC-EPI module for PHP
php7.1-zip - Zip module for PHP
php7.1-opcache - Zend OpCache module for PHP
php7.1 - server-side, HTML-embedded scripting language (metapackage)
php7.1-xsl - XSL module for PHP (dummy)
...
...
php7.2-bcmath - Bcmath module for PHP
php7.2-bz2 - bzip2 module for PHP
php7.2-cgi - server-side, HTML-embedded scripting language (CGI binary)
php7.2-cli - command-line interpreter for the PHP scripting language
php7.2-common - documentation, examples and common module for PHP
php7.2-curl - CURL module for PHP
php7.2-dba - DBA module for PHP
- 選擇需要的模組,安裝之
sudo apt-get install php7.2-curl php7.2-dev php7.2-gd php7.2-imap php7.2-intl php7.2-mbstring php7.2-mysql php7.2-xml php7.2-zip
- 切換PHP版本
sudo update-alternatives --config php
- 設定Apache與正確的PHP版本一起執行
sudo a2dismod php7.1 # unload the current version
sudo a2enmod php5.6 # load the version you need
sudo service apache2 restart # restart webserver to apply
到此結束,再也不用擔心安裝PHP,切換PHP版本這些事情啦。
參考: