Skip to content

Instantly share code, notes, and snippets.

@annibuliful
Created June 8, 2025 04:16
Show Gist options
  • Save annibuliful/ae9bdb78a8a39c942f6e5e497f4a1f4f to your computer and use it in GitHub Desktop.
Save annibuliful/ae9bdb78a8a39c942f6e5e497f4a1f4f to your computer and use it in GitHub Desktop.
#!/bin/bash
# PHP Server Runner Script
# รันเซิร์ฟเวอร์ PHP โดยลบ service เดิมก่อน และกำหนด port ได้
# ตั้งค่า default port
DEFAULT_PORT=3000
PORT=${1:-$DEFAULT_PORT}
# ตรวจสอบว่า port เป็นตัวเลข
if ! [[ "$PORT" =~ ^[0-9]+$ ]]; then
echo "❌ Error: Port must be a number"
echo "Usage: $0 [port]"
echo "Example: $0 3000"
exit 1
fi
# ตรวจสอบว่า port อยู่ในช่วงที่ถูกต้อง
if [ "$PORT" -lt 1024 ] || [ "$PORT" -gt 65535 ]; then
echo "❌ Warning: Port $PORT might require special privileges or be invalid"
echo "Recommended range: 1024-65535"
fi
echo "🚀 Starting PHP Development Server on port $PORT"
echo "📁 Document Root: $(pwd)"
echo "🌐 URL: http://localhost:$PORT"
echo ""
# ฟังก์ชันสำหรับหา process ที่ใช้ port
find_process_by_port() {
local port=$1
if command -v lsof >/dev/null 2>&1; then
lsof -ti:$port 2>/dev/null
elif command -v netstat >/dev/null 2>&1; then
netstat -tlnp 2>/dev/null | grep ":$port " | awk '{print $7}' | cut -d'/' -f1
elif command -v ss >/dev/null 2>&1; then
ss -tlnp | grep ":$port " | grep -o 'pid=[0-9]*' | cut -d'=' -f2
else
echo ""
fi
}
# ฟังก์ชันสำหรับ kill process
kill_process() {
local pid=$1
if [ -n "$pid" ] && [ "$pid" != "0" ]; then
if ps -p $pid > /dev/null 2>&1; then
echo "🔄 Killing existing process (PID: $pid) on port $PORT..."
kill -TERM $pid 2>/dev/null
sleep 2
# ถ้ายัง alive อยู่ ใช้ kill -9
if ps -p $pid > /dev/null 2>&1; then
echo "⚡ Force killing process (PID: $pid)..."
kill -9 $pid 2>/dev/null
sleep 1
fi
# ตรวจสอบอีกครั้งว่า process ตายแล้วหรือยัง
if ps -p $pid > /dev/null 2>&1; then
echo "❌ Failed to kill process $pid"
return 1
else
echo "✅ Process $pid terminated successfully"
return 0
fi
fi
fi
return 0
}
# ตรวจสอบและลบ service เดิมที่ใช้ port นี้
echo "🔍 Checking for existing services on port $PORT..."
EXISTING_PID=$(find_process_by_port $PORT)
if [ -n "$EXISTING_PID" ] && [ "$EXISTING_PID" != "0" ]; then
echo "⚠️ Found existing service on port $PORT (PID: $EXISTING_PID)"
# ตรวจสอบว่าเป็น PHP server หรือไม่
if ps -p $EXISTING_PID -o comm= 2>/dev/null | grep -q "php"; then
echo "📝 Existing PHP server detected - will terminate"
else
echo "📝 Non-PHP service detected - will terminate"
echo " Process info: $(ps -p $EXISTING_PID -o pid,ppid,comm,args --no-headers 2>/dev/null || echo 'Unable to get process info')"
fi
if ! kill_process $EXISTING_PID; then
echo "❌ Failed to free port $PORT. Exiting."
exit 1
fi
# รอสั้นๆ เพื่อให้ port ว่าง
sleep 1
else
echo "✅ Port $PORT is available"
fi
# ตรวจสอบว่ามี PHP หรือไม่
if ! command -v php >/dev/null 2>&1; then
echo "❌ Error: PHP is not installed or not in PATH"
echo "Please install PHP first:"
echo " - Ubuntu/Debian: sudo apt-get install php"
echo " - CentOS/RHEL: sudo yum install php"
echo " - macOS: brew install php"
echo " - Windows: Download from https://php.net"
exit 1
fi
# แสดงข้อมูล PHP
PHP_VERSION=$(php -v | head -n 1 | cut -d ' ' -f 2)
echo "📋 PHP Version: $PHP_VERSION"
# ตรวจสอบว่ามีไฟล์ index ในไดรเคทอรี่หรือไม่
if [ ! -f "index.php" ] && [ ! -f "index.html" ]; then
echo "ℹ️ No index.php or index.html found in current directory"
echo " Server will show directory listing or 404 for root path"
fi
# ฟังก์ชันสำหรับเปิด browser
open_browser() {
local url=$1
echo "🌐 Opening browser at: $url"
# ตรวจสอบ OS และใช้คำสั่งที่เหมาะสม
if command -v xdg-open >/dev/null 2>&1; then
# Linux
xdg-open "$url" >/dev/null 2>&1 &
elif command -v open >/dev/null 2>&1; then
# macOS
open "$url" >/dev/null 2>&1 &
elif command -v start >/dev/null 2>&1; then
# Windows (Git Bash/WSL)
start "$url" >/dev/null 2>&1 &
elif [ -n "$BROWSER" ]; then
# ใช้ environment variable BROWSER
$BROWSER "$url" >/dev/null 2>&1 &
else
echo " Cannot detect browser command. Please open manually."
return 1
fi
return 0
}
cleanup() {
echo ""
echo "🛑 Shutting down PHP server..."
if [ -n "$SERVER_PID" ]; then
kill $SERVER_PID 2>/dev/null
wait $SERVER_PID 2>/dev/null
fi
echo "✅ Server stopped"
exit 0
}
# ตั้งค่า trap สำหรับ cleanup
trap cleanup SIGINT SIGTERM
echo "🎯 Starting PHP built-in server..."
echo " Press Ctrl+C to stop the server"
echo " Access your site at: http://localhost:$PORT"
echo ""
echo "📊 Server Logs:"
echo "=================="
# รัน PHP server และเก็บ PID
php -S localhost:$PORT &
SERVER_PID=$!
# ตรวจสอบว่า server เริ่มต้นสำเร็จหรือไม่
sleep 2
if ! ps -p $SERVER_PID > /dev/null 2>&1; then
echo "❌ Failed to start PHP server on port $PORT"
echo " Please check if the port is available or try a different port"
exit 1
fi
echo "✅ PHP server is running (PID: $SERVER_PID)"
# เปิด browser หลังจาก server พร้อมใช้งาน
SERVER_URL="http://localhost:$PORT"
open_browser "$SERVER_URL"
echo ""
# รอให้ server ทำงาน
wait $SERVER_PID
@annibuliful
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment