java情况的装置配置,5下Java处境的装置配备

2019-09-26 01:46栏目:网络技术
TAG:

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

1、查看系统中的java环境

CentOS 7.5下Java环境的安装配置

php index.php

  [root@localhost ~]# java -version
  openjdk version "1.8.0_101"
  OpenJDK Runtime Environment (build 1.8.0_101-b13)
  OpenJDK 64-Bit Server VM (build 25.101-b13, mixed mode)

1、查看系统中的java环境

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

  进一步查看JDK信息:

  [root@localhost ~]# java -version
  openjdk version "1.8.0_101"
  OpenJDK Runtime Environment (build 1.8.0_101-b13)
  OpenJDK 64-Bit Server VM (build 25.101-b13, mixed mode)

PHP Fatal error:  Class 'PDO' not found in

  [root@localhost ~]# rpm -qa | grep java
  javapackages-tools-3.4.1-11.el7.noarch
  java-1.8.0-openjdk-headless-1.8.0.101-3.b13.el7_2.x86_64
  python-javapackages-3.4.1-11.el7.noarch
  tzdata-java-2016f-1.el7.noarch
  java-1.8.0-openjdk-1.8.0.101-3.b13.el7_2.x86_64

  进一步查看JDK信息:

PHP Fatal error:  Class 'Curl' not found in

  卸载 OpenJDK:rpm -e --nodeps

  [root@localhost ~]# rpm -qa | grep java
  javapackages-tools-3.4.1-11.el7.noarch
  java-1.8.0-openjdk-headless-1.8.0.101-3.b13.el7_2.x86_64
  python-javapackages-3.4.1-11.el7.noarch
  tzdata-java-2016f-1.el7.noarch
  java-1.8.0-openjdk-1.8.0.101-3.b13.el7_2.x86_64

PHP Fatal error:  Class 'Redis' not found in

  [root@localhost ~]# rpm -e --nodeps javapackages-tools-3.4.1-11.el7.noarch
  [root@localhost ~]# rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.101-3.b13.el7_2.x86_64
  [root@localhost ~]# rpm -e --nodeps python-javapackages-3.4.1-11.el7.noarch
  [root@localhost ~]# rpm -e --nodeps tzdata-java-2016f-1.el7.noarch
  [root@localhost ~]# rpm -e --nodeps java-1.8.0-openjdk-1.8.0.101-3.b13.el7_2.x86_64

  卸载 OpenJDK:rpm -e --nodeps

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

2、安装jdk

  [root@localhost ~]# rpm -e --nodeps javapackages-tools-3.4.1-11.el7.noarch
  [root@localhost ~]# rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.101-3.b13.el7_2.x86_64
  [root@localhost ~]# rpm -e --nodeps python-javapackages-3.4.1-11.el7.noarch
  [root@localhost ~]# rpm -e --nodeps tzdata-java-2016f-1.el7.noarch
  [root@localhost ~]# rpm -e --nodeps java-1.8.0-openjdk-1.8.0.101-3.b13.el7_2.x86_64

怎么加入这些扩展?

  下载jdk的rpm安装包上传到/usr/local/java

2、安装jdk

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

  下载地址:

  下载jdk的rpm安装包上传到/usr/local/java

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

图片 1

  下载地址:

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

cd /usr/local/java 进入jdk所在的文件夹执行安装命令:

图片 2

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

[root@localhost java]# rpm -ivh jdk-8u91-linux-x64.rpm
准备中...                          ################################# [100%]
正在升级/安装...
   1:jdk1.8.0_91-2000:1.8.0_91-fcs    ################################# [100%]
Unpacking JAR files...
    tools.jar...
    plugin.jar...
    javaws.jar...
    deploy.jar...
    rt.jar...
    jsse.jar...
    charsets.jar...
    localedata.jar...
    jfxrt.jar...

cd /usr/local/java 进入jdk所在的文件夹执行安装命令:

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

不报错就安装成功

[root@localhost java]# rpm -ivh jdk-8u91-linux-x64.rpm
准备中...                          ################################# [100%]
正在升级/安装...
  1:jdk1.8.0_91-2000:1.8.0_91-fcs    ################################# [100%]
Unpacking JAR files...
    tools.jar...
    plugin.jar...
    javaws.jar...
    deploy.jar...
    rt.jar...
    jsse.jar...
    charsets.jar...
    localedata.jar...
    jfxrt.jar...

linux环境下:

验证:

不报错就安装成功

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

[root@localhost java]# java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

验证:

web环境下:

3、环境变量设置

[root@localhost java]# java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

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

修改系统环境变量文件
vi + /etc/profile

3、环境变量设置

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

[root@localhost java]# vi + /etc/profile

修改系统环境变量文件
vi + /etc/profile

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

fi

[root@localhost java]# vi + /etc/profile

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

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

fi

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

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

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

JAVA_HOME=/usr/java/jdk1.8.0_91
JRE_HOME=/usr/java/jdk1.8.0_91/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

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

# 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

JAVA_HOME=/usr/java/jdk1.8.0_91
JRE_HOME=/usr/java/jdk1.8.0_91/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH

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

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

# 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

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

unset i
unset -f pathmunge
"/etc/profile" 82L, 1972C

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

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

红色字体的为添加的设置,通常java安装在/usr/java/目录下

unset i
unset -f pathmunge
"/etc/profile" 82L, 1972C

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

使修改生效
[root@localhost ~]# source /etc/profile   //使修改立即生效

红色字体的为添加的设置,通常java安装在/usr/java/目录下

export PATH

验证配置:

使修改生效
[root@localhost ~]# source /etc/profile  //使修改立即生效

# /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@localhost java]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/java/jdk1.8.0_91/bin:/usr/java/jdk1.8.0_91/jre/bin

验证配置:

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

这样就完成的对整个java环境的配置

[root@localhost java]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/java/jdk1.8.0_91/bin:/usr/java/jdk1.8.0_91/jre/bin

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

 

这样就完成的对整个java环境的配置

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

更多CentOS相关信息见CentOS 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=14

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

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-09/134967.htm

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

图片 3

版权声明:本文由澳门新葡亰平台游戏发布于网络技术,转载请注明出处:java情况的装置配置,5下Java处境的装置配备