如何在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://127.0.0.1/test.php
恭喜!现在,您已经知道如何在 Ubuntu Linux 14.04 LTS 上安装 PHP 7 了。我们将在下一篇文章中学习更多关于这些命令类型的知识。继续关注!
广告