[Vagrant] Centos7 - Apache Server(httpd) 설치하기

식빵·2023년 10월 29일
0

CI CD

목록 보기
5/8
post-thumbnail

회사에서 개발서버 구축을 하는데 Vagrant 를 통한
Apache Server 를 세팅해야될 일이 있었는데,
그 작업 내용을 기록한 글입니다.


Apache Server(httpd) 설치

2가지 방법이 있습니다.
하나는 yum, apt install 을 통해서 컴파일이 모두 된 완성본을 사용하는 것과
다른 하나는 apache server source 를 받고 수동으로 컴파일하는 것입니다.

일단 수동 컴파일를 통한 설치 방법부터 알아보고,
다음으로 yum install 방식을 알아보겠습니다.

yum install 은 딱히 대단한게 없지만,
port forwarding 과 방화벽(firewall-cmd) 세팅
방법에 대해서는 참고하면 좋을 것이다.


수동설치

여기 나오는 수동 설치 방법은 https://lilo.tistory.com/52?category=930056
을 많이 참고했습니다.

Vagrant.configure("2") do |config|
  ### APACHE SERVER
  config.vm.define "apache_server" do |apache_server|
    apache_server.vm.box = "generic/centos7"
    apache_server.vm.hostname = 'apache-server'
    apache_server.vm.provider "virtualbox" do |vb|
      vb.name = 'apache_server'
      vb.cpus = '2'
      vb.memory = '1024'
    end
    # ssh 로그인 가능하도록 설정
    apache_server.vm.provision :shell, :inline => "sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config; sudo systemctl restart sshd;", run: "always"
    
    # 본격적인 프로그램 설치 시작
    apache_server.vm.provision "shell", inline: <<-SHELL
      yum -y update
      yum install -y expat-devel gcc-c++ gcc openssl-devel openldap-devel zlib-devel make pcre-devel

      # 소스 설치를 위한 사전 파일 준비
      curl -LO https://archive.apache.org/dist/httpd/httpd-2.4.58.tar.gz
      # 참고: Apache는 2.4부터 apr, apr-util이 빠졌다. 별도로 설치해주어야 한다.
      curl -LO https://archive.apache.org/dist/apr/apr-1.7.4.tar.gz
      curl -LO https://archive.apache.org/dist/apr/apr-util-1.6.3.tar.gz

      # 압축 해제 후 디렉토리 정리
      tar -xzf httpd-2.4.58.tar.gz; mv httpd-2.4.58 apache2
      tar -xzf apr-1.7.4.tar.gz; mv apr-1.7.4 apr
      tar -xzf apr-util-1.6.3.tar.gz; mv apr-util-1.6.3 apr-util

      mkdir /APP
      mv apache2 /usr/local/src/
      mv {apr,apr-util} /usr/local/src/apache2/srclib # httpd 의 configure 시에 --with-included-apr 덕분에 컴파일 안해도 됨.
      
      # Apache 소스 트리 구성 및 설치
      cd /usr/local/src/apache2
      ./configure --prefix=/APP/apache2 --enable-module=so --enable-mods-shared=all --enable-ssl --enable-so --enable-deflate --enable-rewrite --with-included-apr --with-mpm=worker --enable-mpm-shared=all
      make
      make install
      /APP/apache2/bin/apachectl -V # 제대로 설치되었는지 가볍게 확인

	  # 이거 안하면 warning 문구가 보여서, 억지로 해줌. 필수는 아니다.
      sed -i 's/#ServerName www.example.com:80/ServerName localhost/' /APP/apache2/conf/httpd.conf
      
      # 방화벽 설정
      firewall-cmd --zone=public --permanent --add-port=80/tcp # http 포트 개방
      firewall-cmd --zone=public --permanent --add-port=443/tcp # https 포트 개방
      firewall-cmd --reload # 방화벽 설정 적용
    SHELL
    
    # apache server 를 서비스로 등록
    apache_server.vm.provision "shell", path: "register_service.sh"
    
    # 호스트 PC 에서는 127.0.0.1:10080 또는 192.168.31.10:80 으로 접근 가능
    apache_server.vm.network "private_network", ip: "192.168.31.10"
    apache_server.vm.network "forwarded_port", guest: 80, host: 10080
  end
end
  • 참고로 추후에 Apache Tomcat 과 연동을 위해서 apxs 디렉토리 경로를 알야아하는데,
    위치는 아파치 설치 경로/bin/apxs 에 있습니다.
  • 위의 설정에서는 /APP/apache2/bin/apxs 에 있겠네요.
  • 이 경로는 apache 관련 make install 을 해야 생성되는 것입니다!

참고로 apache_server.vm.provision "shell", path: "register_service.sh"
에서 사용되는 register_service.sh 는 Vagrantfile 과 동일한 경로 상에 생성하고,
내용은 아래와 같습니다.

cp /APP/apache2/bin/apachectl /etc/init.d/httpd
sed -i 's~#!/bin/sh~#!/bin/sh\
# chkconfig: 2345 90 90\
# description: init file for Apache server daemon\
# processname: /APP/apache2/bin/apachectl\
# config: /APP/apache2/conf/httpd.conf\
# pidfile: /APP/apache2/logs/httpd.pid~' /etc/init.d/httpd
chkconfig --add httpd
systemctl start httpd # && systemctl enable httpd

이 명령어들만 따로 sh 파일로 작성하는 이유는 sed 명령어가
Vagrantfile 에서는 이상하게 오동작하는 경우가 많아서 그렇습니다.
한시간동안 삽질하다가 그냥 sh 파일로 따로 빼서 실행시켰습니다.



자동설치

Vagrant.configure("2") do |config|
  ### APACHE SERVER
  config.vm.define "apache_server" do |apache_server|
    apache_server.vm.box = "generic/centos7"
    apache_server.vm.hostname = 'apache-server'
    apache_server.vm.provider "virtualbox" do |vb|
      vb.name = 'apache_server'
      vb.cpus = '2'
      vb.memory = '1024'
    end
    apache_server.vm.provision :shell, :inline => "sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config; sudo systemctl restart sshd;", run: "always"
    apache_server.vm.provision "shell", inline: <<-SHELL
      # 설치
      yum -y update
      yum install httpd -y
      
      # 추후에 apx 와 관련된 작업도 필요하다면 httpd-devel 도 설치해주세요.
      # yum install httpd-devel -y # /usr/bin/apxs 파일이 생성됩니다!

      systemctl start httpd
      systemctl enable httpd
      curl http://localhost # request 테스트
      
      # 방화벽 설정
      firewall-cmd --zone=public --permanent --add-port=80/tcp # http 포트 개방
      firewall-cmd --zone=public --permanent --add-port=443/tcp # https 포트 개방
      firewall-cmd --reload # 방화벽 설정 적용
    SHELL
    # 호스트 PC 에서는 127.0.0.1:10080 또는 192.168.31.10:80 으로 접근 가능
    apache_server.vm.network "private_network", ip: "192.168.31.10"
    apache_server.vm.network "forwarded_port", guest: 80, host: 10080
  end
end



참고링크:

profile
백엔드를 계속 배우고 있는 개발자입니다 😊

0개의 댓글