如何在Ubuntu Linux 14.04 LTS上安裝PHP 7
PHP 是一種用於 Web 開發的伺服器端指令碼語言,但也可用作通用程式語言。它最初由 Rasmus Lerdorf 於 1994 年建立,PHP 的參考實現現在由 PHP 組製作。最新版本的 PHP 是 PHP7,它比 PHP 5.6 版本的速度提高了 2 倍,記憶體消耗減少了 50%。本文介紹如何在 Ubuntu Linux 上安裝 PHP7。
在安裝 PHP7 之前,您需要安裝一個名為 ondrej/php 的 PPA。這允許您同時安裝 PHP 5.6 和 7.0 版本。
配置可同時安裝 PHP 5.6 + 7.0 的 PPA
要配置 PPA,請使用以下命令:
$ sudo add-apt-repository ppa:ondrej/php
示例輸出應如下所示:
Co-installable PHP versions: PHP 5.6, PHP 7.0 and batteries included. You can get more information about the packages at https://deb.sury.org For PHP 5.6 use: ppa:ondrej/php5-5.6 For PHP 5.5 use: ppa:ondrej/php5 For PHP 5.4 use: ppa:ondrej/php5-oldstable BUGS & FEATURES: This PPA now has a issue tracker: https://deb.sury.org/pages/bugreporting.html PLEASE READ: If you like my work and want to give me a little motivation, please consider donating: https://deb.sury.org/pages/donate.html WARNING: add-apt-repository is broken with non-UTF-8 locales, see https://github.com/oerdnj/deb.sury.org/issues/56 for workaround: # LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php .............................
要更新軟體包索引,請使用以下命令:
$ sudo apt-get update
示例輸出應如下所示:
Ign http://dl.google.com stable InRelease Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB] Ign http://extras.ubuntu.com trusty InRelease Hit http://ppa.launchpad.net trusty InRelease Ign http://in.archive.ubuntu.com trusty InRelease Get:2 http://extras.ubuntu.com trusty Release.gpg [72 B] Hit http://dl.google.com stable Release.gpg Hit http://ppa.launchpad.net trusty InRelease Hit http://dl.google.com stable Release Hit http://extras.ubuntu.com trusty Release Get:3 http://in.archive.ubuntu.com trusty-updates InRelease [65.9 kB] Hit http://dl.google.com stable/main amd64 Packages Get:4 http://ppa.launchpad.net trusty InRelease [20.9 kB] Hit http://extras.ubuntu.com trusty/main Sources Get:5 http://security.ubuntu.com trusty-security/main Sources [106 kB] Hit http://extras.ubuntu.com trusty/main amd64 Packages Hit http://extras.ubuntu.com trusty/main i386 Packages ..................................
要列出所有 PHP 軟體包,請使用以下命令:
$sudo apt-cache search php7
示例輸出應如下所示:
php7.0-common - documentation, examples and common module for PHP libapache2-mod-php7.0 - server-side, HTML-embedded scripting language (Apache 2 module) php7.0-cgi - server-side, HTML-embedded scripting language (CGI binary) php7.0-cli - command-line interpreter for the PHP scripting language php7.0-phpdbg - server-side, HTML-embedded scripting language (PHPDBG binary) php7.0-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary) libphp7.0-embed - HTML-embedded scripting language (Embedded SAPI library) php7.0-dev - Files for PHP7.0 module development php7.0-curl - CURL module for PHP php7.0-enchant - Enchant module for PHP php7.0-gd - GD module for PHP php7.0-gmp - GMP module for PHP php7.0-imap - IMAP module for PHP php7.0-interbase - Interbase module for PHP php7.0-intl - Internationalisation module for PHP php7.0-ldap - LDAP module for PHP php7.0-mcrypt - libmcrypt module for PHP php7.0-readline - readline module for PHP php7.0-odbc - ODBC module for PHP php7.0-pgsql - PostgreSQL module for PHP php7.0-pspell - pspell module for PHP php7.0-recode - recode module for PHP php7.0-snmp - SNMP module for PHP php7.0-tidy - tidy module for PHP php7.0-xmlrpc - XMLRPC-EPI module for PHP php7.0-xsl - XSL module for PHP (dummy) php7.0 - server-side, HTML-embedded scripting language (metapackage) php7.0-json - JSON module for PHP php-all-dev - package depending on all supported PHP development packages php7.0-sybase - Sybase module for PHP php7.0-sqlite3 - SQLite3 module for PHP php7.0-mysql - MySQL module for PHP php7.0-opcache - Zend OpCache module for PHP php7.0-bz2 - bzip2 module for PHP php7.0-bcmath - Bcmath module for PHP php7.0-mbstring - MBSTRING module for PHP php7.0-soap - SOAP module for PHP php7.0-xml - DOM, SimpleXML, WDDX, XML, and XSL module for PHP php7.0-zip - Zip module for PHP
要安裝 PHP7,請使用以下命令:
$ sudo apt-get install php7.0
示例輸出應如下所示:
Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: libapache2-mod-php5 linux-headers-4.2.0-27 linux-headers-4.2.0-27-generic linux-image-4.2.0-27-generic linux-image-extra-4.2.0-27-generic linux-signed-image-4.2.0-27-generic Use 'apt-get autoremove' to remove them. The following extra packages will be installed: libssl1.0.2 php-common php7.0-cli php7.0-common php7.0-fpm php7.0-json php7.0-opcache php7.0-readline Suggested packages: php-pear The following packages will be REMOVED: php5 php5-cli php5-readline The following NEW packages will be installed: libssl1.0.2 php-common php7.0 php7.0-cli php7.0-common php7.0-fpm php7.0-json php7.0-opcache php7.0-readline 0 upgraded, 9 newly installed, 3 to remove and 3 not upgraded. Need to get 4,383 kB of archives. After this operation, 8,056 kB of additional disk space will be used. Do you want to continue? [Y/n] y ................................
要配置 Nginx 和 PHP 7,請使用以下命令:
$ sudo vi nginx.conf
追加如下程式碼:
# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}儲存並關閉檔案。要重新載入 nginx 伺服器,請使用以下命令:
$ sudo service nginx reload
示例輸出應如下所示:
* Reloading nginx configuration nginx [ OK ]
在重新載入 **nginx** 之前,您應該驗證您的 Linux 系統中是否已安裝 **nginx**。如果未安裝,請按如下方式安裝:
$ sudo apt-get install -y nginx
示例輸出應如下所示:
Reading state information... Done The following packages were automatically installed and are no longer required: libapache2-mod-php5 linux-headers-4.2.0-27 linux-headers-4.2.0-27-generic linux-image-4.2.0-27-generic linux-image-extra-4.2.0-27-generic linux-signed-image-4.2.0-27-generic Use 'apt-get autoremove' to remove them. The following extra packages will be installed: nginx-common nginx-core Suggested packages: fcgiwrap nginx-doc The following NEW packages will be installed: nginx nginx-common nginx-core 0 upgraded, 3 newly installed, 0 to remove and 3 not upgraded. Need to get 349 kB of archives. After this operation, 1,297 kB of additional disk space will be used. Get:1 http://in.archive.ubuntu.com/ubuntu/ trusty-updates/main nginx-common all .............................
如果 nginx 已安裝在 Linux 系統中,則使用上述命令重新載入 nginx。要測試新的 PHP 7 安裝,請在 /var/www/html/ 目錄中建立一個名為 test.php 的檔案。
test.php 應如下所示:
<?php phpinfo(); ?>
儲存並關閉檔案。按如下方式執行它:
https:///test.php
恭喜!現在,您已經知道如何在 Ubuntu Linux 14.04 LTS 上安裝 PHP 7 了。我們將在下一篇文章中學習更多關於這些命令型別的知識。繼續關注!
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP