본문 바로가기

리눅스

[리눅스] nginx tomcat 설치 및 연동

반응형

nginx tomcat 설치 및 연동

테스트 환경

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.1 LTS
Release:	22.04
Codename:	jammy

nginx 설치

nginx repository 등록

echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
apt update

nginx 패키지 설치(최신 안정화 버전)

apt install -y nginx
nginx -v
systemctl --now enable nginx
curl -I localhost

java 설치

java(azul) repository 등록

apt-key adv \
  --keyserver hkp://keyserver.ubuntu.com:80 \
  --recv-keys 0xB1998361219BD9C9
curl -sO https://cdn.azul.com/zulu/bin/zulu-repo_1.0.0-3_all.deb
apt-get install -y ./zulu-repo_1.0.0-3_all.deb
apt-get update
apt-cache search zulu

java(jdk) 패키지 설치((openjdk 17 LTS 버전)

apt-get install zulu17-jdk
java -version

java 환경변수 등록

vim /etc/profile
export JAVA_HOME=/usr/lib/jvm/zulu17
export PATH="$PATH:$JAVA_HOME/bin"
source /etc/profile

tomcat 설치

작업 디렉터리 생성

mkdir -pv /apps/tomcat

tomcat 사용자 및 그룹 생성

groupadd tomcat
useradd -s /bin/false -g tomcat -d /apps/tomcat tomcat

 

tomcat 10 버전 다운로드

 Download URL : https://downloads.apache.org/tomcat/tomcat-10/v10.1.2/bin/

cd /usr/local/src/

tomcat(apache tomcat 10) 내려받기

wget -q https://downloads.apache.org/tomcat/tomcat-10/v10.1.2/bin/apache-tomcat-10.1.2.tar.gz
tar xfz apache-tomcat-10.1.2.tar.gz -C /apps/tomcat --strip-components=1
chown -R tomcat: /apps/tomcat
chmod +x /apps/tomcat/bin/*.sh

 

tomcat 버전 정보 확인

/apps/tomcat/bin/version.sh

systemd 서비스 파일 생성

vim /etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat webs servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat
RestartSec=10
Restart=always
Environment="JAVA_HOME=/usr/lib/jvm/zulu17"
Environment="JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"

Environment="CATALINA_BASE=/apps/tomcat"
Environment="CATALINA_HOME=/apps/tomcat"
Environment="CATALINA_PID=/apps/tomcat/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/apps/tomcat/bin/startup.sh
ExecStop=/apps/tomcat/bin/shutdown.sh

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl --now enable tomcat
systemctl status tomcat --no-pager -l

웹 브라우저 : http://192.168.0.61:8080

 

nginx + tomcat 연동

nginx upstream 설정

upstream tomcat {
        ip_hash;
        server 127.0.0.1:8080 max_fails=10 fail_timeout=1s;
        keepalive 100;
    }
server {
    listen 80;
    server_name localhost;
    root /apps/tomcat/webapps/ROOT;
    index index.jsp;
    charset utf-8;
    location / {
	    proxy_pass http://tomcat;
    }
...
}
cat /etc/nginx/conf.d/default.conf

 

웹 브라우저 : http://192.168.0.61

 

index.jsp 편집

<%@ page
    language="java"
    contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
%><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><title>TOMCAT devsp3(Instance1) - TOMCAT TEST</title></head>
<body><pre>
운영체제: <%=System.getProperty("os.arch")%>
데이터모델: <%=System.getProperty("sun.arch.data.model")%>
클래스경로: <%=System.getProperty("java.class.path",".")%>
라이브러리경로: <%=System.getProperty("java.library.path", ".")%>
파라메터: <%=request.getParameter("param")%> ('한글'이라고 표기되어야 함)

파일인코딩: <%=System.getProperty("file.encoding")%>
시스템캐릭터셋: <%=java.nio.charset.Charset.defaultCharset().name()%>
페이지캐릭터셋: <%=response.getCharacterEncoding()%>

서블릿 경로: <%=application.getRealPath("")%>
</pre></body>
</html>

 

참고URL

- nginx tomcat 설치 및 연동 : https://scbyun.com/365

- nginx + tomcat(멀티 인스턴스) 설치 : https://scbyun.com/617

- CentOS 6.4 Linux + Apache + Apache Tomcat 연동 : https://scbyun.com/272

- azul docs : https://docs.azul.com/core/zulu-openjdk/install/debian

- Download Azul Zulu Builds of OpenJDK : https://www.azul.com/downloads/?package=jdk#download-openjdk

- Download the Red Hat build of OpenJDK : https://developers.redhat.com/products/openjdk/download

- Java Downloads(oracle) : https://www.oracle.com/java/technologies/downloads

 

728x90
반응형