[Linux] 우분투 Apache, tomcat 연동 (feat. mod_jk)
2023. 5. 9. 14:03

1. apache2, tomcat9, jdk 설치

$ apt update
$ apt install apache2
$ apt install default-jdk
$ apt install tomcat9 tomcat9-admin

- 톰캣을 사용하려면 java가 설치되어있어야 함.

 

2. 연동을 위한 mod_jk 설치 및 내용 수정

$ apt-get install libapache2-mod-jk

 

- mod_jk를 설치하면 /etc/libapache2-mod-jk/workers.properties 설정 파일이 생성되는데 말고

아파치 설치경로에 새로 설정파일을 만들어 사용하기.

workers.properties 기본 위치 변경은 jk.conf파일의 경로를 변경하면 된다.

$ vi /etc/apache2/workers.properties

----------------------------------------
workers.properties 아래 내용 추가


#톰캣 설치경로
workers.tomcat_home=/usr/share/tomcat9
#JDK 설치경로
workers.java_home=/usr/lib/jvm/java-11-openjdk-amd64  
 
worker.list=tomcat1
 
worker.tomcat1.port = 8009
#톰캣이 다른 서버에 설치되어있으면 ip 입력
worker.tomcat1.host = localhost   
worker.tomcat1.type = ajp13
worker.tomcat1.lbfactor = 1

- jk.conf 파일에 workers.rproerties 기본경로를 아파치 홈으로 변경해주기

$ vi /etc/apache2/mods-available/jk.conf

	JkWorkersFile /etc/apache2/workers.properties

 

 

3. DocumentRoot 및 톰캣 요청 설정

$ vi /etc/apache2/sites-available/000-default.conf

- 000-default.conf 파일을 열어 아래 내용처럼 수정

ServerAdmin webmaster@localhost
#DocumentRoot /var/www/html
DocumentRoot /var/lib/tomcat9/webapps/ROOT/

#JkMount /* tomcat1

JkMount /*.jsp tomcat1
JkMount /*.json tomcat1
JkMount /*.xml tomcat1
JkMount /*.do tomcat1

- documentroot은 아파치 홈경로인데 톰캣의 홈경로와 똑같이 변경

 

 

4. AJP 프로토콜 설정

$ vi /etc/tomcat9/server.xml

- 주석 해제후 아래와 같이 수정

 

 

5. 아파치, 톰캣 재시작

$ service apache2 restart
$ service tomcat9 restart

 

6. 연동 테스트

출처 - https://sansan2.tistory.com/26

html 파일은 아파치가 jsp 파일은 톰캣이 요청 받게 설정하였다.
/var/lib/tomcat9/webapps/ROOT 경로에 html 파일과 jsp 파일을 만들어 접속해보자

tomcat을 stop 시켜도 html 파일은 연결되고 jsp파일은 연결이 안된다.
반대로 apache를 stop시키면 html, jsp 둘다 접속이 안된다.

아파치-톰캣 연동 방식은 클라이언트가 아파치로 서비스 요청을하면 html은 아파치가 처리하고 jsp파일은 톰캣에서 처리하고 다시 아파치로 넘겨주는 방식이다.

 


- 내장 톰캣인 경우(스프링부트)

 

0. apache2, jdk 설치

1. mod_jk 모듈 설치

sudo apt-get install libapache2-mod-jk

 

2. 프로젝트에서 AJPConfig 클래스 파일 생성

@Configuration
public class AJPConfig {
	
	@Value("${tomcat.ajp.port}")
	private int port;
	
	@Value("${tomcat.ajp.protocal}")
	String ajpProtocal;
	
	@Bean
	public ServletWebServerFactory servletContainer() {
		 
		TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
		
		tomcat.addAdditionalTomcatConnectors(createAjpConnector());
		
		return tomcat;
		
	}
	
	private Connector createAjpConnector() {
		Connector ajpConnector = new Connector(ajpProtocal);
		ajpConnector.setPort(port);
		ajpConnector.setSecure(false);
		ajpConnector.setAllowTrace(false);
		ajpConnector.setScheme("http");
		((AbstractAjpProtocol<?>)ajpConnector.getProtocolHandler()).setSecretRequired(false);
		return ajpConnector;
	}

}

 

3. application.yml 파일 내용 추가

- application.yml

server:
  port: 8001
  servlet:
    context-path: /
    encoding:
      charset: UTF-8
      enabled: true
      force: true  
  
tomcat:
  ajp:
    port: 9001
    protocal: AJP/1.3
    enabled: true

 

4. 로드 밸런싱을 위한 workers.properties 작성

$ nano /etc/apache2/workers.properties

# Define the worker nodes
worker.node1.port=9001
worker.node1.host=127.0.0.1
worker.node1.type=ajp13
worker.node1.lbfactor=1

worker.node2.port=9002
worker.node2.host=127.0.0.1
worker.node2.type=ajp13
worker.node2.lbfactor=1

# Define the load balancer
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2
worker.loadbalancer.sticky_session=1

- 톰캣 프로젝트 복사해서 서버포트 ajp포트 수정

 

- 000-default.conf 파일 작성

$ nano /etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
	# The ServerName directive sets the request scheme, hostname and port that
	# the server uses to identify itself. This is used when creating
	# redirection URLs. In the context of virtual hosts, the ServerName
	# specifies what hostname must appear in the request's Host: header to
	# match this virtual host. For the default virtual host (this file) this
	# value is not decisive as it is used as a last resort host regardless.
	# However, you must set it for any further virtual host explicitly.
	#ServerName www.example.com
	
	ServerName localhost
	ServerAdmin webmaster@localhost
	#DocumentRoot /var/www/html
	DocumentRoot /home/mingook/apps/laptop_java_s1/target/
	

	# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
	# error, crit, alert, emerg.
	# It is also possible to configure the loglevel for particular
	# modules, e.g.
	#LogLevel info ssl:warn

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

	# For most configuration files from conf-available/, which are
	# enabled or disabled at a global level, it is possible to
	# include a line for only one particular virtual host. For example the
	# following line enables the CGI configuration for this host only
	# after it has been globally disabled with "a2disconf".
	#Include conf-available/serve-cgi-bin.conf

	# ProxyPass 설정 추가
    ProxyPass / http://localhost:8001/
    ProxyPassReverse / http://localhost:8001/
</VirtualHost>

* DocumentRoot에서 html에파일이 아닌 war파일로 열려면 ProxyPass와 ProxyPassReverse 작성 필수

 

5. apache2.conf 설정 파일 수정

- /etc/apache/apache2.conf

.
.
기존 내용
.
.

LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so

# mod_jk 설정
JkWorkersFile /etc/apache2/workers.properties

-&nbsp; ajp 설정 확인

 


레퍼런스

https://sansan2.tistory.com/26

 

우분투 리눅스 아파치2/ 톰캣9 연동

1. apache2, tomcat9, jdk 설치 apt update apt install apache2 apt install default-jdk apt install tomcat9 tomcat9-admin 간단하게 아파치, 톰캣이 설치된다 *톰캣을 사용하라면 java가 설치되어있어야 됨 2. 연동 방법 선택 아

sansan2.tistory.com