80 lines
3.3 KiB
Bash
80 lines
3.3 KiB
Bash
#!/bin/bash
|
|
# แก้ 403 realtimechat + ตั้ง Nginx ใหม่ — รันบนเซิร์ฟเวอร์เท่านั้น: sudo bash DO-ON-SERVER-ONCE.sh
|
|
|
|
set -e
|
|
echo "=== 1. สิทธิ์โฟลเดอร์ realtimechat & Game ==="
|
|
chown -R www-data:www-data /var/www/html/realtimechat 2>/dev/null || true
|
|
chown -R www-data:www-data /var/www/html/Game 2>/dev/null || true
|
|
find /var/www/html/realtimechat -type d -exec chmod 755 {} \; 2>/dev/null
|
|
find /var/www/html/realtimechat -type f -exec chmod 644 {} \; 2>/dev/null
|
|
find /var/www/html/Game -type d -exec chmod 755 {} \; 2>/dev/null
|
|
find /var/www/html/Game -type f -exec chmod 644 {} \; 2>/dev/null
|
|
|
|
echo "=== 2. Backup + อัปเดต Nginx config ==="
|
|
CONF="/etc/nginx/sites-available/srv1361159.hstgr.cloud"
|
|
[ -f "$CONF" ] && cp -a "$CONF" "${CONF}.bak.$(date +%Y%m%d%H%M%S)"
|
|
|
|
cat > "$CONF" << 'NGINX_EOF'
|
|
server {
|
|
server_name srv1361159.hstgr.cloud;
|
|
root /var/www/html;
|
|
index index.php index.html index.htm;
|
|
location / { try_files $uri $uri/ =404; }
|
|
location ~ \.php$ {
|
|
include snippets/fastcgi-php.conf;
|
|
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
|
|
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
|
|
include fastcgi_params;
|
|
}
|
|
location ~ /\.ht { deny all; }
|
|
location /realtimechat/ {
|
|
alias /var/www/html/realtimechat/public/;
|
|
index index.html;
|
|
try_files $uri $uri/ /realtimechat/index.html;
|
|
}
|
|
location /realtimechat/socket.io/ {
|
|
proxy_pass http://127.0.0.1:3000;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection "upgrade";
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
}
|
|
location /Game/ {
|
|
alias /var/www/html/Game/public/;
|
|
index index.html;
|
|
try_files $uri $uri/ /Game/index.html;
|
|
}
|
|
location /Game/socket.io/ {
|
|
proxy_pass http://127.0.0.1:3001;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection "upgrade";
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
}
|
|
listen [::]:443 ssl ipv6only=on;
|
|
listen 443 ssl;
|
|
ssl_certificate /etc/letsencrypt/live/srv1361159.hstgr.cloud/fullchain.pem;
|
|
ssl_certificate_key /etc/letsencrypt/live/srv1361159.hstgr.cloud/privkey.pem;
|
|
include /etc/letsencrypt/options-ssl-nginx.conf;
|
|
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
|
|
}
|
|
server {
|
|
if ($host = srv1361159.hstgr.cloud) { return 301 https://$host$request_uri; }
|
|
listen 80 default_server;
|
|
listen [::]:80 default_server;
|
|
server_name srv1361159.hstgr.cloud;
|
|
return 404;
|
|
}
|
|
NGINX_EOF
|
|
|
|
echo "=== 3. ทดสอบ + reload Nginx ==="
|
|
nginx -t && systemctl reload nginx
|
|
echo ""
|
|
echo "เสร็จ — ลองเปิด https://srv1361159.hstgr.cloud/realtimechat/"
|
|
echo "ถ้า index อยู่ที่ realtimechat/ ไม่ใช่ realtimechat/public/ ให้แก้ใน config เป็น alias /var/www/html/realtimechat/;" |