为什么我的脚本使用PHP5而不是PHP7

时间:2016-09-27 12:58:47

标签: php apache

我已经阅读了十几个线程,但无法找出问题所在。我需要一台机器上的php5和php7,并希望php7解释一个文件夹中的脚本。我在配置中找不到错误,所以请看看

Apache:Apache / 2.4.23(Debian)

操作系统:Debian测试

安装了两个PHP版本:

nano /var/www/test/.htaccess

AddHandler application/x-httpd-php7 .php

为此文件夹设置了右AddHandler

nano /etc/apache2/apache2.conf

...
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow 
# access here, or in any related virtual host.
<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>
...

设置了AllowOverwrite

update-alternatives php

  Auswahl      Pfad             Priorität Status
------------------------------------------------------------
* 0            /usr/bin/php7.0   70        automatischer Modus
  1            /usr/bin/php5     50        manueller Modus
  2            /usr/bin/php7.0   70        manueller Modus

和php7应该是默认的anway

php -v

PHP 7.0.11-1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.11-1, Copyright (c) 1999-2016, by Zend Technologies

在命令行中可以使用

w3m http://localhost/test/info.php

PHP Version 5.6.24-0+deb8u1

System           Linux ber-eagle02vm 3.2.0-4-amd64 #1 SMP Debian 3.2.68-1+deb7u1 x86_64
Build Date       Jul 26 2016 08:17:13
Server API       Apache 2.0 Handler
Virtual
Directory        disabled
Support
Configuration
File (php.ini)   /etc/php5/apache2
Path
Loaded
Configuration    /etc/php5/apache2/php.ini
File
Scan this dir
for additional   /etc/php5/apache2/conf.d
...

但不是在服务器环境中!

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;


public class TestLine {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestLine().start();
            }
        });
    }

    private static void start() {
        JFrame frame = new JFrame();
        frame.setContentPane(new CarthPanel());
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private static class CarthPanel extends JComponent {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D gg = (Graphics2D) g;

//            System.out.println("(w,h)=(" + w + "," + h + ")");

            gg.translate(getWidth(), 0); // <<< when uncommenting both lines, mirroring applies
            gg.scale(-1.0, 1.0);    //

            BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);

            try {
                paintTest(img.createGraphics());
            } catch (Exception e) {
                e.printStackTrace();
            }

            g.drawImage(img, 0, 0, null);
        }



        private void paintTest(Graphics2D g) {
            // black background
            g.setColor(Color.black);
            g.fillRect(0, 0, getWidth(), getHeight());

            // colored corners
            g.setColor(Color.RED);
            g.drawLine(0, 0, 10, 0);
            g.drawLine(0, 0, 0, 10);
            g.setColor(Color.RED);
            g.drawLine(0, 199, 10, 199);
            g.drawLine(0, 199, 0, 189);
            g.setColor(Color.CYAN);
            g.drawLine(189, 0, 199, 0);
            g.drawLine(199, 0, 199, 10);
            g.setColor(Color.CYAN);
            g.drawLine(189, 199, 199, 199);
            g.drawLine(199, 199, 199, 189);

            // yellow squares
            g.setColor(Color.yellow);
            g.drawRect(3, 3, 10, 10);
            g.fillRect(186, 3, 11, 11);
            g.fillRect(3, 186, 11, 11);
            g.drawRect(186, 186, 10, 10);

            String chars = "ABC";
            g.setFont(Font.decode("Arial 72"));
            Rectangle2D rect = g.getFontMetrics().getStringBounds(chars, g);
            g.drawString(chars, (int) (200 - rect.getWidth()) / 2, (int) (200 - rect.getHeight()));
        }
    }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

看起来您正在使用mod_phpServer API => Apache 2.0 Handler),这意味着php作为模块嵌入在apache中。要回答这个问题,据我所知,你不能同时加载多个mod_php,这就是为什么你通过网络服务器提供的php总是第5版。

当你在命令行上使用php时,它与网络服务器无关,它根据你的更新替代品使用php7。 php命令转到/usr/bin/php7

一种方法是使用cgi(fastcgi,php-fpm)用于不同的php版本。我还读到了通过使用不同的虚拟主机来设置它,但从未尝试过,我想这也是另一个问题。 希望它有所帮助!