Files
cafe24-testserver/vm/setup_common.sh

142 lines
4.7 KiB
Bash

#!/bin/bash
# 테스트 서버용 공통 설치 스크립트 (Rocky Linux 9)
# Tailscale 호환, 외부 미노출 아님, 최소 권한, Apache + FTP + Vim + Bash 설정
# 홈 디렉토리 권한 문제 해결 포함
WEB_USER=$USER
WEB_HOME=$(eval echo "~$WEB_USER")
WEB_ROOT="$WEB_HOME/www"
echo "설치 사용자: $WEB_USER"
echo "웹 루트: $WEB_ROOT"
# 1. 시스템 업데이트
sudo dnf update -y
# 2. firewalld 비활성화
sudo systemctl stop firewalld
sudo systemctl disable firewalld
echo "firewalld 비활성화 완료"
# 3. 홈 디렉토리 권한 조정 (Apache가 진입 가능하도록)
chmod 711 "$WEB_HOME"
# 4. www 폴더 생성, 소유자/권한 제한
mkdir -p "$WEB_ROOT"
chmod 750 "$WEB_ROOT"
chown $WEB_USER:$WEB_USER "$WEB_ROOT"
# 5. Apache 설치
sudo dnf install -y httpd
sudo systemctl enable --now httpd
# Apache DocumentRoot 및 PHP-FPM 설정
APACHE_CONF="/etc/httpd/conf.d/${WEB_USER}.conf"
APACHE_PHP_CONF="/etc/httpd/conf.modules.d/10-php-${WEB_USER}.conf"
# PHP-FPM 프록시 설정
sudo bash -c "cat > $APACHE_PHP_CONF" <<'EOF'
<IfModule mod_proxy_fcgi.c>
<IfModule mod_setenvif.c>
SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
</IfModule>
<FilesMatch "\.php$">
SetHandler "proxy:unix:/run/php-fpm/WEB_USER.sock|fcgi://localhost"
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
</IfModule>
</IfModule>
EOF
# WEB_USER 치환
sudo sed -i "s/WEB_USER/$WEB_USER/g" "$APACHE_PHP_CONF"
# VirtualHost 설정
sudo bash -c "cat > $APACHE_CONF" <<EOF
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot $WEB_ROOT
<Directory $WEB_ROOT>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog logs/${WEB_USER}-error.log
CustomLog logs/${WEB_USER}-access.log combined
</VirtualHost>
EOF
# Apache 모듈 활성화
sudo a2enmod proxy_fcgi setenvif rewrite 2>/dev/null || sudo dnf install -y mod_proxy_fcgi
sudo systemctl restart httpd
# 6. FTP 설치 및 설정 (vsftpd)
sudo dnf install -y vsftpd
sudo systemctl enable --now vsftpd
# FTP 설정
sudo sed -i 's/anonymous_enable=YES/anonymous_enable=NO/' /etc/vsftpd/vsftpd.conf
sudo sed -i 's/#chroot_local_user=YES/chroot_local_user=YES/' /etc/vsftpd/vsftpd.conf
sudo bash -c "echo 'allow_writeable_chroot=YES' >> /etc/vsftpd/vsftpd.conf"
sudo bash -c "echo 'listen_address=127.0.0.1' >> /etc/vsftpd/vsftpd.conf"
sudo systemctl restart vsftpd
# 7. SSH 설정 강화
sudo sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config
sudo systemctl restart sshd
# 8. Vim 및 Bash 설정 파일 다운로드 (git에서)
# Gitea raw 파일 경로: https://git.siane.kr/firstgarden/cafe24-testserver/raw/branch/main/...
DOTFILES_BASE_URL="https://git.siane.kr/firstgarden/cafe24-testserver/raw/branch/main"
if command -v curl &>/dev/null; then
echo "dotfiles 다운로드 중..."
curl -fsSL -o "$WEB_HOME/.vimrc" "$DOTFILES_BASE_URL/dotfiles/vimrc" 2>/dev/null || echo "경고: .vimrc 다운로드 실패"
# .bashrc_addon 다운로드
if curl -fsSL -o "$WEB_HOME/.bashrc_addon" "$DOTFILES_BASE_URL/dotfiles/bashrc_addon" 2>/dev/null; then
# 기존 .bashrc 백업
if [ -f "$WEB_HOME/.bashrc" ]; then
cp "$WEB_HOME/.bashrc" "$WEB_HOME/.bashrc.bak"
fi
# 추가 설정 병합
cat "$WEB_HOME/.bashrc_addon" >> "$WEB_HOME/.bashrc"
rm "$WEB_HOME/.bashrc_addon"
fi
else
echo "경고: curl이 설치되어 있지 않습니다. dotfiles 수동 설정 필요"
fi
# 파일 소유권 설정
sudo chown $WEB_USER:$WEB_USER "$WEB_HOME/.vimrc" "$WEB_HOME/.bashrc"
# 10. SELinux 웹 루트 접근 허용
sudo chcon -R -t httpd_sys_content_t "$WEB_ROOT"
# 11. 오류 처리 확인
if ! sudo systemctl is-active --quiet httpd; then
echo "에러: Apache 서비스가 실행 중이 아닙니다."
sudo systemctl status httpd
exit 1
fi
if ! sudo systemctl is-active --quiet php-fpm; then
echo "경고: PHP-FPM이 설치되지 않았습니다. install_php.sh 실행 필요"
fi
echo "=== 테스트 서버용 공통 설정 완료! ==="
echo "✓ Apache + FTP + Vim + Bash 환경 구성됨"
echo "✓ 웹 루트: $WEB_ROOT (권한: 750)"
echo "✓ 홈 디렉토리: $WEB_HOME (권한: 711)"
echo ""
echo "다음 단계:"
echo " 1. PHP 설치: ./install_php.sh 8.2 또는 8.4"
echo " 2. 웹 접속: http://$(hostname -I | awk '{print $1}')/"
echo " 3. 로그 확인: tail -f /var/log/httpd/${WEB_USER}-*.log"