คำสั่ง SSH
[root@localhost ~]# su - mill
Last login: Thu Aug 14 17:17:25 +07 2025 on pts/0 [mill@localhost ~]$ export DISPLAY=:0
[mill@localhost ~]$ xhost +SI:localuser:root sudo DISPLAY=:0 /home/mill/restart_webviewer.sh
ทำให้คำสั่งที่คุณรันด้วย shell สามารถรันจาก PHP ได้ โดยวิธีที่ปลอดภัยและทำงานได้เหมือนกัน
ตัวอย่าง PHP
<?php
// รันโดย user mill (ไม่ใช้ root) บน GUI session ของ mill
// 1. ตั้ง DISPLAY
putenv('DISPLAY=:0');
// 2. อนุญาต root เข้าถึง X server (ถ้าจำเป็น)
exec('xhost +SI:localuser:root 2>&1', $xhost_output, $xhost_status);
// 3. รันสคริปต์ restart_webviewer.sh ด้วย sudo (ต้องตั้ง NOPASSWD ใน sudoers)
exec('sudo DISPLAY=:0 /home/mill/restart_webviewer.sh 2>&1', $output, $status);
// 4. แสดงผลลัพธ์
echo "<h3>Output of xhost:</h3>";
echo "<pre>" . implode("\n", $xhost_output) . "</pre>";
echo "<h3>Output of restart_webviewer.sh:</h3>";
echo "<pre>" . implode("\n", $output) . "</pre>";
?>
ขั้นตอนสำคัญ
1.ให้ PHP ใช้ user mill
- ถ้าเว็บรันด้วย Apache/NGINX ให้เปลี่ยน user ของเว็บเป็น
mill
- หรือใช้
sudo NOPASSWD
สำหรับสคริปต์:
sudo visudo
เพิ่มบรรทัด:
mill ALL=(ALL) NOPASSWD: /home/mill/restart_webviewer.sh
2.X server ต้องรันอยู่
- ต้องมี session GUI ของ user
mill
เปิดอยู่ (DISPLAY=:0
) - หรือใช้ Xvfb สำหรับ headless
หมายเหตุ
xhost +SI:localuser:root
อนุญาต root ใช้ X server- การรัน Wine จาก PHP จะต้องอยู่บน session GUI ของ user mill
- ปลอดภัยที่สุดคือ ไม่ใช้ root รัน Wine แต่ให้ PHP รันด้วย user mill โดยตรง
ถ้าต้องการทำเป็นปุ่ม
นี่เป็น PHP + HTML หน้าเดียว ที่มีปุ่ม “Restart WebViewer” และแสดง log output หลังจากกดปุ่ม
<?php
$output = [];
$status = null;
// ถ้ามีการกดปุ่ม
if (isset($_POST['restart'])) {
// ตั้งค่า DISPLAY
putenv('DISPLAY=:0');
// อนุญาต root ใช้ X server (ถ้า script ใช้ sudo)
exec('xhost +SI:localuser:root 2>&1', $xhost_output, $xhost_status);
// รันสคริปต์ restart_webviewer.sh
exec('sudo DISPLAY=:0 /home/mill/restart_webviewer.sh 2>&1', $output, $status);
}
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<title>Restart WebViewer</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
button { padding: 10px 20px; font-size: 16px; }
pre { background: #f0f0f0; padding: 10px; border: 1px solid #ccc; }
</style>
</head>
<body>
<h2>Restart WebViewer</h2>
<form method="post">
<button type="submit" name="restart">Restart WebViewer</button>
</form>
<?php if (!empty($output)): ?>
<h3>Output:</h3>
<pre><?php echo htmlspecialchars(implode("\n", $output)); ?></pre>
<?php endif; ?>
</body>
</html>
วิธีใช้งาน
1.บันทึกไฟล์เป็น restart_webviewer.php
ในเว็บเซิร์ฟเวอร์
2.ตรวจสอบ sudoers ให้ mill
หรือ user เว็บรัน /home/mill/restart_webviewer.sh
ได้โดยไม่ต้องใส่ password:
sudo visudo
# เพิ่มบรรทัด:
apache ALL=(ALL) NOPASSWD: /home/mill/restart_webviewer.sh
# หรือถ้าใช้ nginx: www-data ALL=(ALL) NOPASSWD: /home/mill/restart_webviewer.sh
3.เปิดเบราว์เซอร์เข้า:
http://[server-ip]/restart_webviewer.php
4.กดปุ่ม “Restart WebViewer”
- ระบบจะรันสคริปต์
- แสดง log output หลังปุ่ม
💡 หมายเหตุสำคัญ
- X server ของ user
mill
ต้องรันอยู่ (DISPLAY=:0
) - การให้ PHP รันสคริปต์ด้วย sudo มีความเสี่ยงด้านความปลอดภัย
- แนะนำให้ จำกัดไฟล์นี้เข้าถึงเฉพาะผู้เชื่อถือได้