Files
cafe24-testserver/vm/setup_common.sh

220 lines
7.9 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 테스트 서버용 공통 설치 스크립트 (Rocky Linux 9)
# 오류 처리: 실패 시 변경사항 롤백, 중복 설정 처리 포함
# Tailscale 호환, 외부 미노출 아님, 최소 권한, Apache + FTP + Vim + Bash 설정
set -euo pipefail
WEB_USER=$USER
WEB_HOME=$(eval echo "~$WEB_USER")
WEB_ROOT="$WEB_HOME/www"
# 오류 처리 함수
cleanup() {
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo "❌ 에러 발생 ($EXIT_CODE). 변경사항을 롤백합니다..."
# 설치된 패키지 및 서비스 복원
echo "Apache 설정 롤백 중..."
sudo rm -f /etc/httpd/conf.d/${WEB_USER}.conf
sudo rm -f /etc/httpd/conf.modules.d/10-php-${WEB_USER}.conf
# 설정 파일 복원
sudo systemctl restart httpd 2>/dev/null || true
echo "변경사항이 롤백되었습니다. 처음부터 다시 실행하세요."
fi
exit $EXIT_CODE
}
trap cleanup EXIT
echo "설치 사용자: $WEB_USER"
echo "웹 루트: $WEB_ROOT"
# 사전 확인: 이미 설정된 경우
if [ -f "/etc/httpd/conf.d/${WEB_USER}.conf" ]; then
echo "⚠️ ${WEB_USER}의 Apache 설정이 이미 존재합니다."
read -p "기존 설정을 삭제하고 진행하시겠습니까? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "기존 설정을 삭제합니다..."
sudo rm -f /etc/httpd/conf.d/${WEB_USER}.conf
sudo rm -f /etc/httpd/conf.modules.d/10-php-${WEB_USER}.conf
else
echo "설정을 건너뜁니다."
fi
fi
# 1. 시스템 업데이트
echo "시스템 업데이트 중..."
sudo dnf update -y || { echo "❌ dnf update 실패"; exit 1; }
# 2. firewalld 비활성화 (이미 비활성화된 경우 무시)
sudo systemctl stop firewalld 2>/dev/null || true
sudo systemctl disable firewalld 2>/dev/null || true
echo "✓ firewalld 비활성화 완료"
# 3. 홈 디렉토리 권한 조정 (Apache가 진입 가능하도록)
chmod 711 "$WEB_HOME"
# 4. www 폴더 생성, 소유자/권한 제한
if [ ! -d "$WEB_ROOT" ]; then
mkdir -p "$WEB_ROOT"
chmod 750 "$WEB_ROOT"
chown $WEB_USER:$WEB_USER "$WEB_ROOT"
echo "✓ 웹 루트 디렉토리 생성 완료"
else
echo " 웹 루트 디렉토리가 이미 존재합니다"
fi
# 5. Apache 설치
if ! command -v httpd &>/dev/null; then
echo "Apache 설치 중..."
sudo dnf install -y httpd || { echo "❌ Apache 설치 실패"; exit 1; }
fi
sudo systemctl enable --now httpd || { echo "❌ Apache 서비스 활성화 실패"; exit 1; }
echo "✓ Apache 활성화 완료"
# 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
echo "✓ Apache 설정 파일 생성 완료"
# Apache 모듈 활성화
if ! sudo httpd -M 2>/dev/null | grep -q proxy_fcgi; then
sudo dnf install -y mod_proxy_fcgi || { echo "❌ mod_proxy_fcgi 설치 실패"; exit 1; }
fi
echo "✓ Apache 프록시 모듈 확인 완료"
sudo systemctl restart httpd || { echo "❌ Apache 재시작 실패"; exit 1; }
echo "✓ Apache 재시작 완료"
# 6. FTP 설치 및 설정 (vsftpd)
if ! command -v vsftpd &>/dev/null; then
echo "vsftpd 설치 중..."
sudo dnf install -y vsftpd || { echo "❌ vsftpd 설치 실패"; exit 1; }
fi
sudo systemctl enable --now vsftpd || { echo "❌ vsftpd 서비스 활성화 실패"; exit 1; }
# FTP 설정 (이미 설정된 경우 무시)
if ! grep -q "allow_writeable_chroot=YES" /etc/vsftpd/vsftpd.conf; then
sudo sed -i 's/anonymous_enable=YES/anonymous_enable=NO/' /etc/vsftpd/vsftpd.conf 2>/dev/null || true
sudo sed -i 's/#chroot_local_user=YES/chroot_local_user=YES/' /etc/vsftpd/vsftpd.conf 2>/dev/null || true
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 || { echo "❌ vsftpd 재시작 실패"; exit 1; }
echo "✓ vsftpd 설정 완료"
else
echo " vsftpd 설정이 이미 존재합니다"
fi
# 7. SSH 설정 강화 (이미 설정된 경우 무시)
if grep -q "^PermitRootLogin yes" /etc/ssh/sshd_config; then
sudo sed -i 's/^PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sudo systemctl restart sshd
echo "✓ SSH 설정 강화 완료"
else
echo " SSH 설정은 이미 강화되어 있습니다"
fi
# 8. Vim 및 Bash 설정 파일 다운로드 (git에서)
DOTFILES_BASE_URL="https://git.siane.kr/firstgarden/cafe24-testserver/raw/branch/main"
if command -v curl &>/dev/null; then
echo "dotfiles 다운로드 중..."
# .vimrc 다운로드 (기존 백업)
if [ -f "$WEB_HOME/.vimrc" ]; then
cp "$WEB_HOME/.vimrc" "$WEB_HOME/.vimrc.bak"
fi
curl -fsSL -o "$WEB_HOME/.vimrc" "$DOTFILES_BASE_URL/dotfiles/vimrc" 2>/dev/null || echo "⚠️ .vimrc 다운로드 실패"
# .bashrc_addon 다운로드 및 병합
if curl -fsSL -o "/tmp/.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
# 이미 추가된 경우 제거하고 다시 추가
grep -v "$(head -1 /tmp/.bashrc_addon)" "$WEB_HOME/.bashrc" > "$WEB_HOME/.bashrc.tmp" 2>/dev/null || cp "$WEB_HOME/.bashrc" "$WEB_HOME/.bashrc.tmp"
mv "$WEB_HOME/.bashrc.tmp" "$WEB_HOME/.bashrc"
cat "/tmp/.bashrc_addon" >> "$WEB_HOME/.bashrc"
rm "/tmp/.bashrc_addon"
echo "✓ Bash 설정 병합 완료"
else
echo "⚠️ .bashrc_addon 다운로드 실패"
fi
else
echo "⚠️ curl이 설치되어 있지 않습니다. dotfiles 수동 설정 필요"
fi
# 파일 소유권 설정
sudo chown $WEB_USER:$WEB_USER "$WEB_HOME/.vimrc" "$WEB_HOME/.bashrc" 2>/dev/null || true
# 10. SELinux 웹 루트 접근 허용
sudo chcon -R -t httpd_sys_content_t "$WEB_ROOT" 2>/dev/null || true
# 11. 최종 확인
echo ""
echo "=== 최종 상태 확인 중... ==="
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 2>/dev/null; then
echo " PHP-FPM이 설치되지 않았습니다. install_php.sh 실행 필요"
fi
echo ""
echo "✅ 테스트 서버용 공통 설정 완료!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✓ Apache + FTP + Vim + Bash 환경 구성됨"
echo "✓ 웹 루트: $WEB_ROOT (권한: 750)"
echo "✓ 홈 디렉토리: $WEB_HOME (권한: 711)"
echo ""
echo "📋 다음 단계:"
echo " 1. PHP 설치: ./vm/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"