Notice
Recent Posts
Recent Comments
Tags
- simpe ftp
- XSS PHP
- 하드 마이그레이션
- JavaScript
- jquery 바코드생성
- php 캐쉬제거
- django 엑셀불러오기
- SSD 복사
- asp ftp
- 404에러페이지
- ASP.Net Core 404
- ViewBag
- 강제이동
- asp.net core Select
- javascript redirection
- swagger 500 error
- asp.net core swagger
- 원격ftp
- 말줄임표시
- Mac Oracle
- TempData
- 맥 오라클설치
- 하드 윈도우 복사
- 타임피커
- asp.net dropdownlist
- 바코드 생성하기
- javascript 바코드 생성
- XSS방어
- ViewData
- asp.net Select
웹개발자의 기지개
[PHP] phpinfo() 로 리눅스 환경 정보 구하기 본문
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
<?php
/**
* 리눅스 배포판/버전을 최대한 자동으로 판별하는 함수
* - 1순위: /etc/os-release (가장 정확)
* - 2순위: php_uname() + apache_get_version() 기반 휴리스틱
*
* 리턴값 예시:
* [
* 'name' => 'Ubuntu',
* 'version' => '22.04.3 LTS',
* 'id' => 'ubuntu',
* 'id_like' => 'debian',
* 'method' => 'os-release',
* 'raw' => [...] // 원본 데이터 (디버깅용)
* ]
*/
function detectLinuxDistro()
{
$result = [
'name' => null,
'version' => null,
'id' => null,
'id_like' => null,
'method' => null,
'raw' => []
];
// ------------------------------
// 1) /etc/os-release 기반 (가장 정확)
// ------------------------------
$osReleaseFiles = [
'/etc/os-release',
'/usr/lib/os-release'
];
foreach ($osReleaseFiles as $file) {
if (is_readable($file)) {
$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$data = [];
foreach ($lines as $line) {
if (strpos($line, '=') === false) {
continue;
}
list($k, $v) = explode('=', $line, 2);
$k = trim($k);
$v = trim($v);
// 값 주변의 큰따옴표/작은따옴표 제거
if ((substr($v, 0, 1) === '"' && substr($v, -1) === '"') ||
(substr($v, 0, 1) === "'" && substr($v, -1) === "'")) {
$v = substr($v, 1, -1);
}
$data[$k] = $v;
}
$result['raw']['os-release'] = $data;
$result['name'] = isset($data['NAME']) ? $data['NAME'] : null;
$result['version'] = isset($data['VERSION']) ? $data['VERSION'] : null;
$result['id'] = isset($data['ID']) ? $data['ID'] : null;
$result['id_like'] = isset($data['ID_LIKE']) ? $data['ID_LIKE'] : null;
$result['method'] = 'os-release';
return $result;
}
}
// ------------------------------
// 2) php_uname() 기반 휴리스틱
// ------------------------------
$unameA = php_uname('a'); // 전체 문자열
$unameR = php_uname('r'); // 커널 버전
$uname = strtolower($unameA . ' ' . $unameR);
$result['raw']['php_uname'] = [
'a' => $unameA,
'r' => $unameR,
];
// Apache 버전이 있으면 같이 참고
if (function_exists('apache_get_version')) {
$apacheVersion = apache_get_version();
$result['raw']['apache_version'] = $apacheVersion;
$uname .= ' ' . strtolower($apacheVersion);
}
// 기본값
$result['name'] = 'Unknown Linux';
$result['version'] = null;
$result['id'] = null;
$result['id_like'] = null;
$result['method'] = 'heuristic';
// Ubuntu 계열
if (strpos($uname, 'ubuntu') !== false) {
$result['name'] = 'Ubuntu (heuristic)';
$result['id'] = 'ubuntu';
$result['id_like'] = 'debian';
}
// Debian
elseif (strpos($uname, 'debian') !== false) {
$result['name'] = 'Debian (heuristic)';
$result['id'] = 'debian';
$result['id_like'] = 'debian';
}
// CentOS / RHEL / Rocky / Alma 계열 (el7, el8, el9)
elseif (strpos($uname, 'centos') !== false || strpos($uname, '(centos)') !== false) {
$result['name'] = 'CentOS (heuristic)';
$result['id'] = 'centos';
$result['id_like'] = 'rhel';
} elseif (strpos($uname, 'rocky') !== false) {
$result['name'] = 'Rocky Linux (heuristic)';
$result['id'] = 'rocky';
$result['id_like'] = 'rhel';
} elseif (strpos($uname, 'alma') !== false) {
$result['name'] = 'AlmaLinux (heuristic)';
$result['id'] = 'almalinux';
$result['id_like'] = 'rhel';
} elseif (strpos($uname, 'red hat') !== false || strpos($uname, 'redhat') !== false) {
$result['name'] = 'Red Hat (heuristic)';
$result['id'] = 'rhel';
$result['id_like'] = 'rhel';
}
// 커널 버전에서 el7/el8/el9 같은 패턴이 있으면 살짝 붙여줌
if (preg_match('/\.el([0-9]+)/', $unameR, $m)) {
$el = $m[1];
$result['version'] = 'el' . $el . ' (kernel heuristic)';
}
return $result;
}
$os = detectLinuxDistro();
echo '<pre>';
print_r($os);
echo '</pre>';
?>
|
cs |
예상 출력 예시 (Ubuntu):
Array
(
[name] => Ubuntu
[version] => 22.04.3 LTS (Jammy Jellyfish)
[id] => ubuntu
[id_like] => debian
[method] => os-release
[raw] => Array
(
[os-release] => Array
(
[NAME] => Ubuntu
[VERSION] => 22.04.3 LTS (Jammy Jellyfish)
[ID] => ubuntu
[ID_LIKE] => debian
...
)
)
)
CentOS 7 계열이면 대략:
Array
(
[name] => CentOS Linux
[version] => 7 (Core)
[id] => centos
[id_like] => rhel
[method] => os-release
...
)
'PHP' 카테고리의 다른 글
| [PHP] 초간단 특정 파일의 접근 막기 (0) | 2026.01.30 |
|---|---|
| [PHP] 네이버페이, 카카오페이 연동하기 (0) | 2025.11.20 |
| [PHP] 관리자페이지 접근 시도 제한 시키기 - 간단 코드 (0) | 2025.11.02 |
| [PHP] 모든 태그 필터링(제거) 사용자 정의 함수 만들기 (0) | 2025.10.03 |
| [PHP] 초간단 금지어 단어 필터링 함수만들기 (0) | 2025.09.30 |
Comments
