0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AlmaLinuxで構築するWeb3層構造:APサーバ編

Posted at

はじめに

今回はWeb3層構造の中核ともいえるAPサーバとして、Apache Tomcatを導入します。
TomcatはJava ServletやJSPの実行環境として広く利用されており、Webアプリケーションのバックエンド処理を担います。

1.前提環境

ホスト名 役割 IPアドレス
webserver Webサーバ 192.168.56.10
apserver APサーバ 192.168.56.20
dbserver DBサーバ 192.168.56.30
  • OS:AlmaLinux 9.x
  • SELinux:無効化済み
  • 実行ユーザー:root または sudo権限を持つユーザー

2.名前解決の設定

2.1 /etc/nsswitch.conf の編集

パラメータ 設定値
passwd: files
shadow: files
group: files
hosts: files dns myhostname
services: files
netgroup: files
automount: files

→ /etc/hosts を優先して名前解決できるようにします。

2.2 /etc/hosts の追記

192.168.56.30  dbserver
192.168.56.10  webserver
192.168.56.20  apserver

3.ファイアウォールの設定

Tomcatが使用するポート(8080, 8009)を開放します。

firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=8009/tcp --permanent
firewall-cmd --reload

4.NTP(時刻同期)設定

/etc/chrony.conf に以下を追記し、時刻の正確性を確保します。

server ntp.nict.jp iburst
server ntp1.jst.mfeed.ad.jp iburst
server time.google.com iburst

allow 192.168.56.0/24

Tomcat構築手順

5.1 必要なユーザとJava環境を準備

useradd tomcat
dnf -y install java-1.8.0-openjdk.x86_64

5.2 Tomcatのダウンロードと展開

wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.41/bin/apache-tomcat-10.1.41.tar.gz
tar -zxvf apache-tomcat-10.1.41.tar.gz
mv apache-tomcat-10.1.41 /opt
chown -R tomcat:tomcat /opt/apache-tomcat-10.1.41
ln -s /opt/apache-tomcat-10.1.41 /opt/tomcat

5.3 追加パッケージとMySQL JDBCドライバの導入

dnf -y install tomcat-webapps
dnf install -y java-headless

wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-j-8.4.0-1.el9.noarch.rpm
rpm -ivh mysql-connector-j-8.4.0-1.el9.noarch.rpm
cp -p /usr/share/java/mysql-connector-java.jar /opt/tomcat/lib/
chown -R tomcat:tomcat /opt/tomcat/lib/mysql-connector-java.jar

6.Tomcatの起動設定

6.1 systemd用サービスファイルの設定

/etc/systemd/system/tomcat.service を以下の内容で作成します:

[Unit]
Description=Apache Tomcat 10
After=syslog.target network.target

[Service]
User=tomcat
Group=tomcat
Type=oneshot
PIDFile=/opt/tomcat/tomcat.pid
RemainAfterExit=yes

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

[Install]
WantedBy=multi-user.target

6.2 サービスの起動・自動起動設定

chmod 755 /etc/systemd/system/tomcat.service
systemctl daemon-reexec
systemctl start tomcat
systemctl enable tomcat

7.動作確認

7.1 ブラウザでTomcatにアクセス

http://192.168.56.20:8080/

Tomcatのホーム画面が表示されれば成功です。
tomcat_8080.png

7.2 セキュリティ強化のため8080ポートを閉じる

firewall-cmd --zone=public --remove-port=8080/tcp --permanent
firewall-cmd --reload

8.Webサーバとの連携設定

8.1 /opt/tomcat/conf/server.xml を編集

以下のように設定変更します(HTTPコネクタをコメントアウトし、AJPコネクタを有効化):

~ 一部省略 ~
    <!--
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />
    -->

~ 一部省略 ~
    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector protocol="AJP/1.3"
               address="0.0.0.0"
               secretRequired="false"
               tomcatAuthentication="false" URIEncoding="UTF-8"
               port="8009"
               redirectPort="8443"
               />

~ 一部省略 ~

##8.2 Webサーバ側の設定
/etc/httpd/conf/httpd.confに以下を追記する。

ProxyPass /test/ ajp://<APサーバのIPアドレス>:8009/
ProxyPassReverse /test/ ajp://<APサーバのIPアドレス>:8009/

##8.3 ブラウザでWebサーバ経由の動作確認

http://192.168.56.10/test/

webとの連携.png

Tomcatのホーム画面が表示されれば成功です。

まとめ

Tomcatの導入により、JSPページの表示が可能になりました。Webサーバとの連携も整ってきました。
次回は、MySQLを使ったDBサーバの構築と、サンプルデータベース「sakila」の導入を行います。

本記事は「AlmaLinuxで構築するWeb3層構造」シリーズの第3弾です。
前回までの内容はこちらからどうぞ:

  • 第1回:仮想環境とAlmaLinux構築
  • 第2回:Webサーバ(httpd)構築
  • 第4回:DBサーバ(MySQL + sakila)構築
  • 第5回:Web/AP/DB連携
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?