如何通过php找到已安装的Linux发行版?

时间:2014-01-15 04:16:13

标签: php ubuntu

有没有办法找出哪个版本的Ubuntu安装了php?

控制台上的

我会用

cat /etc/lsb-release 

并且会得到例如:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=13.10
DISTRIB_CODENAME=saucy
DISTRIB_DESCRIPTION="Ubuntu 13.10"

3 个答案:

答案 0 :(得分:3)

您可以解析/ etc / lsb-release以获取信息。

<?php
$release_info = parse_ini_file("/etc/lsb-release");
echo $release_info["DISTRIB_ID"];
echo $release_info["DISTRIB_RELEASE"];
echo $release_info["DISTRIB_DESCRIPTION"];
?>

答案 1 :(得分:2)

这会收集很多有用的数据:

function get_release_info(){
    if(!$r = @parse_ini_file("/etc/lsb-release")){
        if(!$r = @parse_ini_file("/etc/os-release")){
            $r = @parse_ini_file("/etc/redhat-release");
        }
    }
    /*for ex:
        $r["DISTRIB_ID"]=Ubuntu
        $r["DISTRIB_RELEASE"]=13.10
        $r["DISTRIB_CODENAME"]=saucy
        $r["DISTRIB_DESCRIPTION"]="Ubuntu 13.10"
    */
    $r["debian_version"]=trim(@file_get_contents("/etc/debian_version")); // for ex. "wheezy/sid"
    $r["PHP_OS"]=PHP_OS;
    $r["SERVER_SOFTWARE"]=@$_SERVER["SERVER_SOFTWARE"];
    $r["osx_system_arch"]=@$_SERVER["_system_arch"];// Mac-specific for ex.=> x86_64
    $r["osx_system_version"]=@$_SERVER["_system_version"];// Mac-specific for ex.=> 10.9
    /**
        'a': This is the default. Contains all modes in the sequence "s n r v m".
        's': Operating system name. eg. FreeBSD.
        'n': Host name. eg. localhost.example.com.
        'r': Release name. eg. 5.1.2-RELEASE.
        'v': Version information. Varies a lot between operating systems.
        'm': Machine type. eg. i386.
    */
    $r["kernel_release_name"]=php_uname('r');
    $r["os_name"]=php_uname('s');
    $r["uname_version_info"]=php_uname('v');
    $r["machine_type"]=php_uname('m');
    $r["php_uname"]=php_uname();

    if(stristr($r["uname_version_info"],"Ubuntu")){
        // source: http://wiki.ubuntuusers.de/Kernel/Linux-Versionsnummern#Versionsnummern-herausfinden
        $distribution["4.10"]=array("Warty Warthog", "2.6.8");
        $distribution["5.04"]=array("Hoary Hedgehog", "2.6.10");
        $distribution["5.10"]=array("Breezy Badger", "2.6.12");
        $distribution["6.06"]=array("Dapper Drake", "2.6.15");
        $distribution["6.10"]=array("Edgy Eft", "2.6.17");
        $distribution["7.04"]=array("Feisty Fawn", "2.6.20");
        $distribution["7.10"]=array("Gutsy Gibbon", "2.6.22");
        $distribution["8.04"]=array("Hardy Heron", "2.6.24");
        $distribution["8.10"]=array("Intrepid Ibex", "2.6.27");
        $distribution["9.04"]=array("Jaunty Jackalope", "2.6.28");
        $distribution["9.10"]=array("Karmic Koala", "2.6.31");
        $distribution["10.04"]=array("Lucid Lynx", "2.6.32");
        $distribution["10.10"]=array("Maverick Meerkat", "2.6.35");
        $distribution["11.04"]=array("Natty Narwhal", "2.6.38");
        $distribution["11.10"]=array("Oneiric Ocelot", "3.0");
        $distribution["12.04"]=array("Precise Pangolin", "3.2"); #Backports: 3.5, 3.8, 3.11, 3.13
        $distribution["12.10"]=array("Quantal Quetzal", "3.5");
        $distribution["13.04"]=array("Raring Ringtail", "3.8");
        $distribution["13.10"]=array("Saucy Salamander", "3.11");
        $distribution["14.04"]=array("Trusty Tahr", "3.13"); # Backports 3.16, 3.19
        $distribution["14.10"]=array("Utopic Unicorn", "3.16");
        $distribution["15.04"]=array("Vivid Vervet", "3.19");
        $distribution["15.10"]=array("Wily Werewolf", "4.2");
        $distribution["16.04"]=array("Xenial Xerus", "4.4");

        foreach($distribution as $distribution=>$name_kernel){
            list($name,$kernel)=$name_kernel;
            if(version_compare($r["kernel_release_name"],$kernel,'>=')) {
                $r["ubuntu_distribution_by_kernel"]=$distribution;
                $r["ubuntu_distribution_name_by_kernel"]=$name;
                $r["ubuntu_distribution_shortname_by_kernel"]=strtolower(preg_replace("/ .*$/","", $name));
            }
        }
        if(empty($r["DISTRIB_RELEASE"]) and !empty($r["ubuntu_distribution_by_kernel"])) {
            $r["DISTRIB_RELEASE"]=$r["ubuntu_distribution_by_kernel"];
            $r["DISTRIB_DESCRIPTION"]="Ubuntu ".$r["ubuntu_distribution_by_kernel"];
            $r["DISTRIB_CODENAME"]=$r["ubuntu_distribution_shortname_by_kernel"];
        }
    }
    return $r;
}

答案 2 :(得分:0)

$ _SERVER []可以提供帮助。

在我的Mac上我有

$_SERVER["_system_arch"] => x86_64
$_SERVER["_system_version"] => 10.9

我建议您阅读/ etc / lsb-release中的文件。

您可以找到更多信息here