CentOS VM에 웹 서버(httpd)를 설치하고, HTML 템플릿을 배포하는 과정이다.
# 프로젝트 디렉토리 생성
mkdir website
cd website
# 로컬에 다운로드된 box 목록 확인
vagrant box list
# CentOS box로 Vagrantfile 초기화
vagrant init bandit145/centos_stream9_arm
vagrant box list는 로컬에 이미 다운로드된 Vagrant box 목록을 보여준다. vagrant init은 지정한 box 기반으로 Vagrantfile을 생성한다.
# VM 접속
vagrant ssh
# 패키지 설치
sudo yum install httpd wget vim unzip zip -y
# httpd 시작 및 부팅 시 자동 실행 등록
sudo systemctl start httpd
sudo systemctl enable httpd
systemctl start는 지금 바로 서비스를 시작하고, systemctl enable은 VM 재부팅 시에도 자동으로 시작되도록 등록한다.
httpd를 실행해도 호스트 PC 브라우저에서 접속이 안 될 수 있다.
맥에서 CentOS는 기본적으로 firewalld가 활성화되어 있어서 허용하지 않은 포트는 외부 접근을 차단하기 때문이다.
# HTTP(80번 포트) 허용
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
실습 환경에서는 방화벽 자체를 비활성화할 수도 있다.
sudo systemctl stop firewalld
sudo systemctl disable firewalld
tooplate.com에서 템플릿을 다운로드한 뒤 압축을 풀고, /var/www/html/에 파일을 배치한다.
# 템플릿 다운로드 및 압축 해제
cd /tmp
wget <템플릿 URL>
unzip <파일명>.zip
# 웹 서버 문서 루트에 배포
sudo cp -r <템플릿 폴더>/* /var/www/html/
# httpd 재시작
sudo systemctl restart httpd
호스트 PC 브라우저에서 VM의 IP(192.168.33.10)로 접속하면 템플릿 페이지가 표시된다.
위 과정을 매번 수동으로 하면 반복 작업이 된다. Vagrantfile에 provisioning 스크립트를 추가하면 vagrant up 한 번으로 전체 과정을 자동화할 수 있다.
config.vm.provision "shell", inline: <<-SHELL
echo "1. 패키지 설치"
yum install httpd wget unzip vim -y
echo "2. 서비스 시작"
systemctl start httpd
systemctl enable httpd
echo "3. 웹 페이지 생성"
echo "3.1. tooplate 웹 페이지 다운로드"
mkdir -p /tmp/htmltemplate
cd /tmp/htmltemplate
wget https://www.tooplate.com/zip-templates/2159_mochi_space.zip
echo "3.2. 웹 페이지 압축 해제"
unzip -o 2159_mochi_space.zip
echo "3.3. 웹 페이지 복사"
cp -r 2159_mochi_space/* /var/www/html/
echo "4. httpd 서비스 재시작"
systemctl restart httpd
echo "5. 방화벽 해제"
systemctl stop firewalld
systemctl disable firewalld
echo "6. tmp 디렉토리 삭제"
rm -rf /tmp/htmltemplate
SHELL
-y (yum install): 설치 확인을 자동 승인. 프로비저닝은 자동 실행이므로 대화형 프롬프트가 나오면 멈춘다.mkdir -p: 상위 디렉토리가 없으면 함께 생성. 경로가 확실하지 않을 때 안전하게 사용하는 습관.unzip -o: 덮어쓰기 확인 없이 압축 해제. 마찬가지로 대화형 프롬프트 방지.CentOS는 기본적으로 firewalld가 활성화되어 있어 외부에서 80번 포트 접근이 차단된다. 수동 세팅 때 빠뜨리기 쉬운 부분이므로 프로비저닝 스크립트에 포함시킨다.
systemctl stop firewalld
systemctl disable firewalld