如何在 Ubuntu 上安装 PHP 7(下一代 PHP)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30987883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:10:56  来源:igfitidea点击:

How do I install PHP 7 (PHP next generation) on Ubuntu

phpubuntu

提问by RyanNerd

How do I install PHP 7 (PHP next generation) on Ubuntu?

如何在 Ubuntu 上安装 PHP 7(下一代 PHP)?

I wanted to install PHP 7 on a clean install of Ubuntu to see what issues I would face.

我想在全新安装的 Ubuntu 上安装 PHP 7,看看我会遇到什么问题。

Here's the official (yet meager guide):

这是官方的(但很简陋的指南):

https://wiki.php.net/phpng

https://wiki.php.net/phpng

回答by RyanNerd

I did a run through on my own, and thought it would be useful to document what problems I ran into as I went and how I overcame them thus saving others perhaps some pain and suffering.

我自己进行了一次运行,并认为记录我在旅途中遇到的问题以及我如何克服它们从而为其他人节省一些痛苦和苦难会很有用。

I did however (after the fact) find this http://jcutrer.com/howto/linux/how-to-compile-php7-on-ubuntu-14-04which is a much cleaner guide than what I provide below.

然而,我确实(事后)找到了这个http://jcutrer.com/howto/linux/how-to-compile-php7-on-ubuntu-14-04,这是一个比我在下面提供的更清晰的指南。

From the guide (https://wiki.php.net/phpng):

从指南(https://wiki.php.net/phpng):

./buildconf 
mkdir  ~/tmp 
cd ~/tmp
git clone https://git.php.net/repository/php-src.git
cd php-src

So far so good. At this point I started having problems:

到现在为止还挺好。在这一点上,我开始遇到问题:

./buildconf

./buildconf

Reported that I did not have make installed. The solution was found here: https://askubuntu.com/questions/192645/make-command-not-found

报告说我没有安装make。解决方案在这里找到:https: //askubuntu.com/questions/192645/make-command-not-found

I tried this, but still had problems so I did this:

我试过这个,但仍然有问题,所以我这样做了:

sudo apt-get purge make  
sudo apt-get install make 

Next problem when I tried ./buildconf again it complained that I did not have autoconfig. Here's the solution to that:

下一个问题,当我再次尝试 ./buildconf 时,它抱怨我没有自动配置。这是解决方案:

sudo apt-get install autoconf  

Now finally got to run ./configure command and got this:

现在终于可以运行 ./configure 命令并得到这个:

configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers. configure: error: bison is required to build PHP/Zend when building a GIT checkout!

配置:警告:如果您想重新生成 PHP 解析器,您将需要 re2c 0.13.4 或更高版本。配置:错误:在构建 GIT 检出时构建 PHP/Zend 需要野牛!

Installing Bison also appears to install re2c as well so this fixes both problems:

安装 Bison 似乎也安装了 re2c,因此这可以解决两个问题:

sudo apt-get install bison    

Config churned along for quite some time and then puked with:

配置搅动了很长一段时间,然后吐了:

configure: error: xml2-config not found. Please check your libxml2 installation.

配置:错误:找不到 xml2-config。请检查您的 libxml2 安装。

Should be an easy fix right? Just run:

应该很容易解决吧?赶紧跑:

sudo apt-get install libxml2

Nope. Not that easy. We already have the latest and greatest for this:

不。没那么容易。我们已经拥有最新和最好的:

libxml2 is already the newest version.

libxml2 已经是最新版本了。

Okay. Maybe we can solve this like we did make and purge and reinstall:

好的。也许我们可以像制作、清除和重新安装一样解决这个问题:

sudo apt-get purge libxml2 sudo apt-get install libxml2

须藤 apt-get 清除 libxml2 须藤 apt-get 安装 libxml2

Nope. Running ./configure barfs on the same error. SO to the rescue:

不。在相同的错误上运行 ./configure barfs。所以救援:

How to fix error with xml2-config not found when installing PHP from sources?

从源代码安装 PHP 时,如何修复找不到 xml2-config 的错误?

Aparently we need the development version of this library:

显然我们需要这个库的开发版本:

sudo apt-get install libxml2-dev

./configure again and we have progress...a new error:

./configure 再次,我们有进展...一个新的错误:

configure: error: Cannot find OpenSSL's

配置:错误:找不到 OpenSSL 的

Learning from our last foray let's try:

从我们上次尝试中学习,让我们尝试:

sudo apt-get install openssl-dev

or

或者

sudo apt-get install open-ssl-dev

Nope, Okay let's do a search for this:

不,好吧,让我们搜索一下:

apt-cache search openssl 

Wow, that's quite a few packages. Let's narrow it down a bit:

哇,那是相当多的包。让我们把它缩小一点:

apt-cache search openssl | grep php

Well that gives us a smaller list, but all have php5 in the front and we are doing a php7 (phpng Next Generation build from scratch); will any of these packages work? Off to the interwebs for our solution:

好吧,这给了我们一个较小的列表,但前面都有 php5,我们正在做一个 php7(从头开始构建 phpng Next Generation);这些软件包中的任何一个都有效吗?到互联网上获取我们的解决方案:

https://serverfault.com/questions/415458/php-error-cannot-find-openssls-evp-h

https://serverfault.com/questions/415458/php-error-cannot-find-openssls-evp-h

sudo apt-get install libcurl4-openssl-dev pkg-config

Let's see if that made ./configure happy (nope, but progress...a new error):

让我们看看这是否让 ./configure 开心(不,但进展......一个新错误):

configure: error: Please reinstall the BZip2 distribution

配置:错误:请重新安装 BZip2 发行版

Searching the web again leads us to:

再次搜索网络导致我们:

http://zgadzaj.com/how-to-install-php-53-and-52-together-on-ubuntu-1204

http://zgadzaj.com/how-to-install-php-53-and-52-together-on-ubuntu-1204

which tells us to do this:

这告诉我们这样做:

sudo apt-get install libbz2-dev

Once again we send a prayer to the ./configure god and...

我们再一次向 ./configure 神祈祷,然后......

If configure fails try --with-vpx-dir= configure: error: jpeglib.h not found.

如果配置失败,请尝试 --with-vpx-dir= configure: error: jpeglib.h not found。

TO THE INTERWEBS!!!

到互联网!

Found this: http://www.omniweb.com/wordpress/?p=1040

发现这个:http: //www.omniweb.com/wordpress/?p= 1040

sudo apt-get install libjpeg-dev 

Try ./configureagain and SURVEY SAYS:

再试./configure一次,调查说:

configure: error: png.h not found.

配置:错误:找不到 png.h。

From the same website as above it says to do this:

从与上面相同的网站上,它说要这样做:

sudo apt-get install libpng-dev

Try ./configureAGAIN and SURVEY SAYS:

再试一次./configure,调查说:

configure error xpm.h not found

找不到配置错误 xpm.h

I took a stab in the dark and hit the target with:

我在黑暗中刺伤并用以下方法击中目标:

sudo apt-get install libxpm-dev

Once again praying to the ./configuregod and the reply is:

再次向./configure上帝祈祷,得到的答复是:

configure: error: freetype-config not found.

配置:错误:未找到 freetype-config。

The ALL KNOWING Google directs us to the SO god here:

ALL KNOWING Google 将我们引向这里的 SO 神:

Configure php 5.3 fail with error : freetype.h not found (but libfreetype6-dev is installed)

配置 php 5.3 失败并显示错误:未找到 freetype.h(但已安装 libfreetype6-dev)

sudo apt-get install libfreetype6-dev 

By now you have probably recognized the pattern of:

到现在为止,您可能已经认识到以下模式:

  1. run ./configure

  2. Search the error message on your favorite search engine or directly on SO.

  3. Install the missing component via apt-get.

  4. GOTO 1.

  1. 运行 ./configure

  2. 在您最喜欢的搜索引擎上或直接在 SO 上搜索错误消息。

  3. 通过 apt-get 安装缺少的组件。

  4. 转到 1。

So from here on I am just going to show the error and the apt-get command or other commands that resolve the issue:

所以从这里开始,我将只显示错误和 apt-get 命令或其他解决问题的命令:

configure: error: Unable to locate gmp.h

配置:错误:无法找到 gmp.h

build php5.3.8 on ubuntu , get error: configure: error: Unable to locate gmp.h

在 ubuntu 上构建 php5.3.8,得到错误:配置:错误:无法定位 gmp.h

sudo apt-get install libgmp-dev  
sudo apt-get install libgmp3-dev
ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

配置:错误:未找到 mcrypt.h。请重新安装 libmcrypt。

sudo apt-get install libmcrypt-dev

configure: error: Please reinstall the mysql distribution

配置:错误:请重新安装mysql发行版

http://www.spiration.co.uk/post/1332/install%20PDO_MYSQL%20-%20mysql_config%20and%20header%20files%20problem

http://www.spiration.co.uk/post/1332/install%20PDO_MYSQL%20-%20mysql_config%20and%20header%20files%20problem

sudo apt-get install libmysqlclient15-dev  

configure: error: Cannot find pspell

配置:错误:找不到pspell

sudo apt-get install libpspell-dev

configure: error: Can not find recode.h anywhere under /usr / usr/local /usr /opt.

配置:错误:在 /usr/usr/local/usr/opt 下的任何地方都找不到 recode.h。

sudo apt-get install librecode-dev

configure: WARNING: unrecognized options: --with-t1lib

配置:警告:无法识别的选项:--with-t1lib

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=658248

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=658248

No solution. Appears to be a regression bug. Workaround is to remove the --with-t1lib from the configuration options. I think this may apply to Windows builds only, but I'm not certain.

没有解决方案。似乎是一个回归错误。解决方法是从配置选项中删除 --with-t1lib。我认为这可能仅适用于 Windows 版本,但我不确定。

configure: WARNING: unrecognized options: --with-my-sql

配置:警告:无法识别的选项:--with-my-sql

No solution. Work around is to remove the --with-mysql switch from the configuration options. Since we are including the pdo-mysql this is probably not an issue anyway.

没有解决方案。解决方法是从配置选项中删除 --with-mysql 开关。由于我们包括 pdo-mysql,这可能不是问题。

FINALLY We get to actually build PHP7 with this command (I suggest you make yourself a cup of coffee now as this takes some time) :

最后,我们可以用这个命令实际构建 PHP7(我建议你现在给自己泡杯咖啡,因为这需要一些时间):

make

Use the command sudo make install to actually install our php build:

使用命令 sudo make install 实际安装我们的 php 构建:

sudo make install 

To check if all is well and we have the right version execute this in the terminal:

要检查一切是否正常并且我们有正确的版本,请在终端中执行此操作:

cd $HOME/tmp/usr/bin
./php -v

PHP 7.0.0-dev (cli) (built: Jun 22 2015 18:11:13) Copyright (c) 1997-2015 The PHP Group Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies

PHP 7.0.0-dev (cli) (built: Jun 22 2015 18:11:13) 版权所有 (c) 1997-2015 The PHP Group Zend Engine v3.0.0-dev, 版权所有 (c) 1998-2015 Zend Technologies

回答by Mark Amery

Ond?ej Sury has a PPA at https://launchpad.net/~ondrej/+archive/ubuntu/php

Ond?ej Sury 在https://launchpad.net/~ondrej/+archive/ubuntu/php 上有一个 PPA

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.0

Using the PPA is certainly easier than building from source!

使用 PPA 肯定比从源代码构建更容易!

A note for people with PHP 5.x already installed; apt-getcomplained a bit when I tried to install install php7.0 on a box with PHP 5 on it, but after I did apt-get remove php5to get rid of my existing PHP 5.6 install and then explicitly did sudo apt-get install php7.0-commonI was able to get the sudo apt-get install php7.0to go through. Don't know why this was necessary; if somebody who understands Apt better than I do would like to edit in some better instructions (or provide a better answer altogether and ask me to delete this one) then feel free.

已安装 PHP 5.x 的人的注意事项;apt-get当我试图在一个装有 PHP 5 的盒子上安装 php7.0 时有点抱怨,但是在我apt-get remove php5摆脱了我现有的 PHP 5.6 安装之后,sudo apt-get install php7.0-common我明确地做到了sudo apt-get install php7.0。不知道为什么这是必要的;如果比我更了解 Apt 的人想要编辑一些更好的说明(或完全提供更好的答案并要求我删除此答案),那么请随意。

回答by FerraBraiZ

Installing PHP7(Nightly Build - latest build) on Ubuntu using the terminal

使用终端在 Ubuntu 上安装 PHP7(Nightly Build - 最新版本)

i will assume that you have apache2 already installed, if not then install it with the following command:

我假设您已经安装了 apache2,如果没有,请使用以下命令安装它:

apt-get install apache2

check if apache is running:

检查 apache 是否正在运行:

service apache2 status

You would be able to see something like:

您将能够看到如下内容:

apache2.service - LSB: Apache2 web server
Loaded: loaded (/etc/init.d/apache2)
Active: ACTIVE (RUNNING)

if apache is running then procced with the PHP 7 installation process:

如果 apache 正在运行,则执行 PHP 7 安装过程:

First add the PHP7 repo to your sources list, /etc/apt/sources.list, using your preferred text editor:

首先使用您喜欢的文本编辑器将 PHP7 存储库添加到您的源列表 /etc/apt/sources.list 中:

deb http://repos.zend.com/zend-server/early-access/php7/repos ubuntu/ 

After adding the PHP 7 repo, update your system with the following command:

添加 PHP 7 存储库后,使用以下命令更新您的系统:

apt-get update

now if everything went ok you should be able to install PHP7 Nightly Build with the following command:

现在,如果一切顺利,您应该可以使用以下命令安装 PHP7 Nightly Build:

apt-get install php7-nightly

During the install a warning may appear, “Do you want to continue?, if it does press enter. After pressing enter, you will also get a warning similar to below

在安装过程中可能会出现一个警告,“你想继续吗?,如果它确实按回车键。按回车后,您还会收到类似于下面的警告

WARNING: The following packages cannot be authenticated!
  php7-nightly
Install these packages without verification? [y/N]

Type Y and then press Enter to continue.

键入 Y,然后按 Enter 继续。

The installation is finished! Now you need to get PHP7 to work with Apache, in order to do that, copy/move the libraries and modules of PHP7 to the Apache directories with the following commands:

安装完成!现在您需要让 PHP7 与 Apache 一起工作,为此,使用以下命令将 PHP7 的库和模块复制/移动到 Apache 目录:

cp /usr/local/php7/libphp7.so /usr/lib/apache2/modules/
cp /usr/local/php7/php7.load /etc/apache2/mods-available/

After moving the files, you need to edit /etc/apache2/apache2.conf and add the following lines to the bottom of the file. which will tell apache to serve php files

移动文件后,您需要编辑 /etc/apache2/apache2.conf 并将以下几行添加到文件底部。这将告诉 apache 提供 php 文件

<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

Now that you fixed the apache2.conf run the following command to enable the PHP mpm module and switch to mpm_prefork.

现在您修复了 apache2.conf,运行以下命令以启用 PHP mpm 模块并切换到 mpm_prefork。

a2dismod mpm_event && a2enmod mpm_prefork && a2enmod php7

Restart Apache so that the changes you made take effect:

重新启动 Apache 以使您所做的更改生效:

service apache2 restart

Now its time to test PHP 7

现在是测试 PHP 7 的时候了

To test out PHP7, you are going to create a PHP file a called info.php in /var/www/html/ by using your favorite editor and inserting the following lines of code.

要测试 PHP7,您将使用您喜欢的编辑器并插入以下代码行,在 /var/www/html/ 中创建一个名为 info.php 的 PHP 文件。

<?php
phpinfo();
?>

Now you can go to your favorite browser and try the following URL

现在您可以转到您喜欢的浏览器并尝试以下 URL

http://localhost/info.php

At the top of the page, check that the PHP version is 7.0 or higher.

在页面顶部,检查 PHP 版本是否为 7.0 或更高。

PHP7 version example

PHP7 版本示例

Now that You verified that it's working, you should remove your info.php as it's a security risk. Remove it with the following command:

既然您已验证它正在运行,您应该删除您的 info.php,因为它存在安全风险。使用以下命令删除它:

rm /var/www/html/info.php

rm /var/www/html/info.php

Source:https://www.atlantic.net/community/howto/try-php7-debian-8-2-lamp-stack/

来源:https://www.atlantic.net/community/howto/try-php7-debian-8-2-lamp-stack/

回答by Light93

I compiled myself php 7 by following this => http://blog.idojo.co/how-to-setup-a-lamp-stack-with-all-the-latest-softwares/, the instructions were pretty easy, adjust the configure options to your needs, the default configure options worked fine for me on ubuntu server 14.04....

我按照这个编译了自己的 php 7 => http://blog.idojo.co/how-to-setup-a-lamp-stack-with-all-the-latest-softwares/,说明很简单,调整根据您的需要配置选项,默认配置选项在 ubuntu 服务器 14.04 上对我来说效果很好....

The lamp stack includes, Apache 2.4.18, MySQL 5.7.10and PHP 7.0.4

灯组包括,Apache 2.4.18MySQL 5.7.10PHP 7.0.4

回答by Ritin

On Ubuntu 16

在 Ubuntu 16 上

apt-get update

apt-get install php libapache2-mod-php php-mcrypt

apt-get 更新

apt-get 安装 php libapache2-mod-php php-mcrypt

this installed php7

这安装了php7

回答by RyanNerd

I am the OP. My suggestion is to use Ond?ej Sury 's PPA at https://launchpad.net/~ondrej/+archive/ubuntu/php-7.0as suggested by @Mark Amery

我是 OP。我的建议是按照@Mark Amery 的建议在https://launchpad.net/~ondrej/+archive/ubuntu/php-7.0上使用 Ond?ej Sury 的 PPA

If you you want the fun and games of installing PHP 7 from the source then then here is the script I use for the config and build (Note the "make -j4" is if you have a 4 core machine and want it to parallel thread on every core during the build process):

如果您想要从源代码安装 PHP 7 的乐趣和游戏,那么这里是我用于配置和构建的脚本(注意“make -j4”是如果您有一台 4 核机器并希望它并行线程在构建过程中的每个核心上):

#!/bin/sh
./buildconf --force

./configure \
--prefix=/usr/php7 \
--with-config-file-path=/etc/php7 \
--enable-phpdbg \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pgsql \
--with-pdo-pgsql \
--with-pdo-sqlite \
--enable-intl \
--with-pear \
--with-gd \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--enable-exif \
--enable-zip \
--with-zlib \
--with-zlib-dir=/usr \
--with-mcrypt=/usr \
--enable-soap \
--enable-xmlreader \
--with-xsl \
--with-curl=/usr \
--with-tidy \
--with-xmlrpc \
--enable-sysvsem \
--enable-sysvshm \
--enable-shmop \
--enable-pcntl \
--with-readline \
--enable-mbstring \
--with-curl \
--with-gettext \
--enable-sockets \
--with-bz2 \
--with-openssl \
--with-gmp \
--enable-bcmath \
--enable-calendar \
--enable-ftp \
--with-pspell=/usr \
--with-enchant=/usr \
--enable-wddx \
--with-imap \
--with-imap-ssl \
--with-freetype-dir=/usr/local \
--with-xpm-dir=/usr \
--with-kerberos \
--enable-sysvmsg 

make -j4 
sudo make install

回答by vearutop

You can use php-buildto build and install PHP 7 (and other PHP versions). It can be much easier if you are not familiar with PHP building procedure.

您可以使用php-build来构建和安装 PHP 7(和其他 PHP 版本)。如果您不熟悉 PHP 构建过程,这会容易得多。

回答by Justin Samuel

You could install a control panel that supports Ubuntu and PHP 7. For example, ServerPilot supports Ubuntuand installs PHP 7.

您可以安装支持 Ubuntu 和 PHP 7 的控制面板。例如,ServerPilot支持 Ubuntu安装 PHP 7

回答by NetVicious

I see other users are posting binaries but if you want to compile it yourself try auto-apt.

我看到其他用户正在发布二进制文件,但如果您想自己编译它,请尝试使用 auto-apt。

For compiling any application from source you can use auto-apt. It's not a well known tool but it helps a lot for compiling applications with lots of external libraries.

要从源代码编译任何应用程序,您可以使用 auto-apt。它不是一个众所周知的工具,但它对编译具有大量外部库的应用程序有很大帮助。

Auto-apt creates a driven checking of the configure script and detects which packages the configure.sh script it's looking for, and it allows you to install they if you want the feature the script it's looking at that moment.

Auto-apt 创建对配置脚本的驱动检查,并检测它正在寻找的 configure.sh 脚本的包,如果您想要它正在查看的脚本的功能,它允许您安装它们。

sudo apt-get install auto-apt
sudo auto-apt update
sudo auto-apt updatedb && sudo auto-apt update-local
sudo auto-apt run ./configure

More info: https://help.ubuntu.com/community/AutoApt

更多信息:https: //help.ubuntu.com/community/AutoApt