본문 바로가기
AWS Cloud School 8기/서버가상화_클라우드 이미지

web + db + tomcat 실습(코드만)

by YUNZEE 2025. 2. 6.
728x90
실습) 10.10.3.80/tomcat/dbtest.jsp를 쳤을때 was-db connection success가 뜨도록 한번 구성해보세요. 서버는 3대를 다 새로 만드세요.

필요한 서버 3개
DB, WEB(10.10.3.80), TOMCAT

 

✅초기 설정

더보기

초기 설정

sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
 reboot

 

systemctl stop firewalld
systemctl disable firewalld

 

cat <<EOF> /etc/yum.repos.d/CentOS-Base.repo

[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
baseurl=https://vault.centos.org/7.9.2009/os/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
baseurl=https://vault.centos.org/7.9.2009/updates/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
baseurl=https://vault.centos.org/7.9.2009/extras/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
baseurl=https://vault.centos.org/7.9.2009/centosplus/x86_64/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib&infra=$infra
baseurl=https://vault.centos.org/7.9.2009/contrib/x86_64/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

 

✅DB

더보기

yum install -y mariadb-server

 

systemctl restart mariadb

systemctl enable mariadb

 

mysql_secure_installation

[root@was-db ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

 

mysql -u root -p1234

 

MariaDB [(none)]> create database tomcatdb ;

 

MariaDB [(none)]> grant all privileges on tomcatdb.* to tomcatuser@'%' identified by '1234';

 

✅ tomcat

더보기

yum install -y java-11-openjdk wget unzip

 

https://tomcat.apache.org/download-1wge0.cgi

- 직접 복붙해야 잘 받아와짐

 

wget  https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.34/bin/apache-tomcat-10.1.34.zip

 

unzip apache-tomcat-10.1.34.zip > /dev/null

 

mv apache-tomcat-10.1.34/  tomcat

cd tomcat/


chmod 777 -R /root/tomcat

 

sh ./bin/startup.sh

 

- 잘 받아와 지는걸 확인할 수 있음

 

https://tomcat.apache.org/tomcat-6.0-doc/appdev/sample/

 

Sample Application

Sample Application The example app has been packaged as a war file and can be downloaded here (Note: make sure your browser doesn't change file extension or append a new one). The easiest way to run this application is simply to move the war file to your C

tomcat.apache.org

직접 복붙해야 잘 받아와짐 

wget https://tomcat.apache.org/tomcat-6.0-doc/appdev/sample/sample.war

 

[root@was-tom-db tomcat]# vi /root/tomcat/webapps/ROOT/test.jsp

 

[root@was-tom-db tomcat]# ./bin/shutdown.sh

[root@was-tom-db tomcat]# ./bin/startup.sh


 - DB 설정한 후 

 

yum install -y mysql

# mysql 명령어 설치

 

mysql -u tomcatuser -p1234 -h 10.10.3.80

# db 접속 확인

cat <<EOF > dbtest.jsp
> 
> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
> <%@ page import="java.sql.*"%>
> <h1>DB</h2>
> <%
>         Connection conn=null;
>         try{
>                 String Url="jdbc:mysql://10.10.3.90/tomcatdb";
>                 String Id="tomcatuser";
>                 String Pass="1234";
> 
>                 Class.forName("com.mysql.jdbc.Driver");
>                 conn=DriverManager.getConnection(Url,Id,Pass);
>                 out.println("was-db Connection Success!");
>         }catch(Exception e) {
>                 e.printStackTrace(); 
> }
> %>
> 
> EOF

[root@tomcat tomcat]# mv dbtest.jsp /root/tomcat/webapps/ROOT/dbtest.jsp

 

https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.23

wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.23/mysql-connector-java-8.0.23.jar

 

 mv mysql-connector-java-8.0.23.jar /root/tomcat/lib

 

[root@was-tom-db tomcat]# ./bin/shutdown.sh

[root@was-tom-db tomcat]# ./bin/startup.sh

 

✅ WEB

더보기

yum install -y httpd

 

[root@was-db ~]# systemctl restart httpd


[root@was-db ~]# systemctl enable httpd


[root@was-db ~]# echo 'was-db connection success' > /var/www/html/index.html

 

[root@was-db ~]# curl 10.10.3.80
was-db connection success


[root@was-db ~]# vi /etc/httpd/conf/httpd.conf

LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

<VirtualHost *:80> # 80번 포트로 들어오는 모든 ip에 대한 설정
    ProxyRequests Off
    ProxyPreserveHost On
    <Proxy *> 
        Order deny,allow
        Allow from all
    </Proxy>

    ProxyPass /tomcat http://211.183.3.88:8080/
    ProxyPassReverse /tomcat http://211.183.3.88:8080/
</VirtualHost>


[root@was-db ~]# systemctl restart httpd

 

[root@was-db ~]# curl localhost:80/jsp/dbtest.jsp

728x90