如何在Ubuntu Linux 14.04 LTS上安装PHP 7

时间:2020-01-09 10:45:34  来源:igfitidea点击:

与PHP 5.6版相比,PHP 7的性能提高了2倍,内存消耗降低了50%。
如何在Ubuntu Linux 14.04 LTS服务器上安装PHP 7?
如何与Nginx Web服务器一起安装带有PHP-FPM的PHP 7?
您需要安装一个名为ondrej/php的PPA。
这使您可以共同安装PHP版本5.6和7.0。

PHP 7的最新版本是7.0.5,您将学习如何在Ubuntu 14.04 LTS服务器上安装该版本。

如何在Ubuntu Linux 14.04 LTS上安装PHP 7

在Ubuntu 14.04 LTS上安装和配置PHP 7的过程如下:

  • 在Ubuntu Linux 14.04 LTS上启用ondrej/php
  • 使用sudo apt-get update更新apt缓存
  • 使用apt-cache search php7命令列出Ubuntu上的所有PHP 7软件包
  • 通过运行sudo apt-get install php7.0在Ubuntu上安装PHP 7软件包。
  • 通过编辑nginx.conf文件,将Nginx配置为使用PHP 7

让我们详细查看所有命令。

为可共同安装的PHP 5.6 + 7.0配置PPA

执行以下命令:

$ sudo apt-get install -y language-pack-en-base
$ sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php

或者

$ sudo add-apt-repository ppa:ondrej/php

更新包索引

要从其源重新同步包索引文件,请执行:

$ sudo apt-get update

输出示例:

Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://archive.ubuntu.com trusty-updates InRelease [64.4 kB]
Get:2 http://ppa.launchpad.net trusty InRelease [15.5 kB]                      
Hit http://security.ubuntu.com trusty-security InRelease                       
Hit http://archive.ubuntu.com trusty Release.gpg             
Hit http://archive.ubuntu.com trusty Release         
Get:3 http://archive.ubuntu.com trusty-updates/main amd64 Packages [696 kB]
Hit http://security.ubuntu.com trusty-security/main amd64 Packages             
Get:4 http://ppa.launchpad.net trusty/main amd64 Packages [23.2 kB]           
Hit http://security.ubuntu.com trusty-security/restricted amd64 Packages       
Hit http://security.ubuntu.com trusty-security/universe amd64 Packages         
Get:5 http://ppa.launchpad.net trusty/main Translation-en [12.2 kB]            
Hit http://security.ubuntu.com trusty-security/multiverse amd64 Packages       
Get:6 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [15.9 kB]
Hit http://security.ubuntu.com trusty-security/main Translation-en             
Get:7 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [336 kB]
Hit http://security.ubuntu.com trusty-security/multiverse Translation-en       
Get:8 http://archive.ubuntu.com trusty-updates/multiverse amd64 Packages [13.0 kB]
Hit http://archive.ubuntu.com trusty-updates/main Translation-en               
Hit http://security.ubuntu.com trusty-security/restricted Translation-en       
Hit http://archive.ubuntu.com trusty-updates/multiverse Translation-en         
Hit http://archive.ubuntu.com trusty-updates/restricted Translation-en         
Hit http://archive.ubuntu.com trusty-updates/universe Translation-en           
Hit http://security.ubuntu.com trusty-security/universe Translation-en         
Hit http://archive.ubuntu.com trusty/main amd64 Packages                       
Hit http://archive.ubuntu.com trusty/restricted amd64 Packages       
Hit http://archive.ubuntu.com trusty/universe amd64 Packages
Hit http://archive.ubuntu.com trusty/multiverse amd64 Packages
Hit http://archive.ubuntu.com trusty/main Translation-en
Hit http://archive.ubuntu.com trusty/multiverse Translation-en
Hit http://archive.ubuntu.com trusty/restricted Translation-en
Hit http://archive.ubuntu.com trusty/universe Translation-en
Ign http://archive.ubuntu.com trusty/main Translation-en_US
Ign http://archive.ubuntu.com trusty/multiverse Translation-en_US
Ign http://archive.ubuntu.com trusty/restricted Translation-en_US
Ign http://archive.ubuntu.com trusty/universe Translation-en_US
Fetched 1,176 kB in 3s (326 kB/s)                  
Reading package lists... Done

列出所有PHP 7软件包

执行以下apt-cache命令:

$ apt-cache search php7

输出示例:

php7.0-common - Common files for packages built from the PHP source
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-dbg - Debug symbols for PHP7.0
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
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-modules-source - PHP 7.0 modules source package
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

安装PHP 7

执行以下命令以安装PHP 7以及有用的php模块,例如MySQL,GD,curl等:

$ sudo apt-get install php7.0

或者

$ sudo apt-get install php7.0 php7.0-cli php7.0-fpm php7.0-gd php7.0-json php7.0-mysql php7.0-readline

输出示例:
安装PHP 7.0

配置Nginx和PHP 7

编辑您的nginx.conf文件:

$ sudo vi nginx.conf

或者

$ sudo vi /etc/nginx/sites-enabled/default

修改或者追加如下:

# 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

测试新的PHP 7安装

在/var/www/html /目录中创建一个名为test.php的文件:

<?php
        phpinfo();
?>

保存并关闭文件。
如下运行:

http://your-domain/test.php

如何升级或者修补我的php 7.x版本?

执行以下两个命令:

$ sudo apt-get update
$ sudo apt-get upgrade