#!/bin/bash # --- Cấu hình Biến Database --- DB_USER="gradio_user" # BẠN NÊN THAY ĐỔI MẬT KHẨU NÀY! DB_PASS="local_password_123" DB_NAME="gradio_db" DB_PORT="5432" # 1. Khởi động PostgreSQL echo "Starting PostgreSQL service..." service postgresql start echo "Waiting for PostgreSQL to start (5s)..." sleep 5 # 2. Khởi tạo Database (Chỉ chạy nếu DB chưa tồn tại) # Script này chạy với tư cách 'root', vì vậy chúng ta dùng 'su' # để chuyển sang người dùng 'postgres' if ! su - postgres -c "psql -lqt" | cut -d \| -f 1 | grep -qw $DB_NAME; then echo "Database $DB_NAME not found. Initializing..." su - postgres -c "psql -c \"CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';\"" su - postgres -c "psql -c \"CREATE DATABASE $DB_NAME OWNER $DB_USER;\"" echo "Database $DB_NAME and user $DB_USER created." else echo "Database $DB_NAME already exists." fi # 3. Khởi động Nginx echo "Starting Nginx..." service nginx start # 4. Khởi động Streamlit (với tư cách user 'pn' an toàn hơn) echo "Starting Streamlit on port 8501 as user 'pn'..." # Chuyển sang user 'pn' và chạy trong thư mục app su - pn -c "cd /home/pn/app && streamlit run app_streamlit.py --server.port 8501 --server.address 0.0.0.0 --server.baseUrlPath /streamlit" & # 5. Khởi động Gradio/FastAPI (với tư cách user 'pn') echo "Starting Gradio/FastAPI on port 7860 as user 'pn'..." # 'exec' để nó trở thành quy trình chính, giữ container chạy # Chuyển sang user 'pn' và chạy trong thư mục app exec su - pn -c "cd /home/pn/app && uvicorn app_gradio:app --port 7860 --host 0.0.0.0"