Home >php教程 >PHP源码 >php *.php和/usr/local/php/bin/php *.php的区别

php *.php和/usr/local/php/bin/php *.php的区别

WBOY
WBOYOriginal
2016-06-08 17:19:411363browse

下文我们一起来看一篇关于在php中php *.php和/usr/local/php/bin/php *.php的区别是什么吧,这个小编目前也搞不明白了,我们一起来看看。

<script>ec(2);</script>

说一个基本概念,很多人可能会遇到过这种情况,有些PHP程序因为部署原因会在linux命令行下执行,这样就会用到php命令执行,例如:

php index.php

很方便,但也会出现一些问题,例如:

PHP Fatal error:  Class 'PDO' not found in

PHP Fatal error:  Class 'Curl' not found in

PHP Fatal error:  Class 'Redis' not found in

...等等,我们统称为Class not found in问题,其实原因看来就是没有加入扩展,但是疑问也就从这里出来了,

怎么加入这些扩展?

为什么有时候在浏览器访问web程序的时候同样的环境怎么没有出现这些问题,而且还能正常调用这些扩展类呢?

对于问题1,网上很多教程大家可以输入google.com然后输入Linux php加入某某扩展来查看解决,我们重点说下问题2。

确定问题现象,我们以lnmp环境为例,一步步分析,

首先这些扩展类和php是有关系的,加载的是否成功通过php.ini来查看,我们初期判断两边是不是用了不同的php.ini?

下来我们就在不同的地方输出phpinfo()查看php的基本信息,果然,

linux环境下:

Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini

web环境下:


Configuration File (php.ini) Path => /usr/local/php/etc
Loaded Configuration File => /usr/local/php/etc/php.ini

通过上面的信息可以看出,两边用的php完全是不一样的,为了确定,我们验证了下通过/usr/local/php/bin/php来执行先前的报错的php程序,发现一切正常PDO,Curl,Redis都正常调用。

为什么会出先这样的情况呢?

那是因为,你在安装完PHP后,“不经意”的操作中又进行了一遍php的安装编译操作,导致存放了两分不同的php.ini文件,lnmp环境下的php.ini是放置在/usr/local/php/etc/php.ini,特殊操作会默认放置在/etc/php.ini,而且如果不配置php *.php的环境变量,他会默认读取/etc/php.ini下的配置信息。从而导致在linux下使用php命令与web下不一致的结果。

问题找到了,改怎么解决的?

删除“不经意”的操作中安装编译的php

配置php环境变量,让php命令指向/usr/local/php/bin/php,我们以centOS为例配置环境变量进行说明:

[root@CentOS ~]# vi /etc/profile

编辑profile文件,注,以下操作为永久有效。

在文件末尾加上如下两行代码

PATH=/usr/local/php/bin:$PATH

export PATH

# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}
if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`id -u`
        UID=`id -ru`
    fi
    USER="`id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi
# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /sbin
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
    pathmunge /sbin after
fi
HOSTNAME=`/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi
for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null 2>&1
        fi
    fi
done
unset i
unset pathmunge
PATH=/usr/local/php/bin:$PATH
export PATH

要是刚才的修改马上生效,需要执行以下代码

[root@CentOS ~]# source /etc/profile

这时再查看系统环境变量,就能看见刚才加的东西已经生效了

[root@CentOS ~]# echo $PATH
/usr/local/php/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

现在使用php命令和/usr/local/php/bin/php是一个效果了,指向了同样的php.ini文件。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn