PHP是一种用于Web开发的服务器端脚本语言,但也用作通用编程语言。PHP参考实现最初由Rasmus Lerdorf在1994年创建,现在由The PHP Group生产。最新版本的PHP是PHP7,它的性能比PHP 5.6快2倍,内存消耗降低50%。本文介绍“如何在Ubuntu Linux上安装PHP7”。
在安装PHP7之前,您需要安装一个名为ondrej / php的PPA 。这使您可以共同安装PHP版本5.6和7.0。
要配置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 .............................
如果Linux中已经安装了nginx,则使用上述命令重新加载nginx。要测试新的PHP 7安装,请在/ var / www / html /目录中创建一个名为test.php的文件。
test.php应该是这样的–
<?php phpinfo(); ?>
保存并关闭文件。按照以下方式运行–
http://localhost/test.php
恭喜你!现在,您知道“如何在Ubuntu Linux 14.04 LTS上安装PHP 7”。在我们的下一篇Linux文章中,我们将详细了解这些类型的命令。继续阅读!