Linux-tomcat environment construction, jpress deployment practice, nginx reverse proxy

♥️Author : Xiao Liu at Station C

♥️Personal homepage:  Xiao Liu's homepage 

♥️Efforts may not necessarily pay off, but there will be gains! Come on! Work hard together for a better life!

♥️ Learn the operation and maintenance experience summed up in two years, and a full set of network experiment tutorials for Cisco simulators. Column: Cloud Computing Technology

♥️Xiao Liu private message can ask casually, as long as you know it, you will not be stingy, thank you CSDN for letting you and me meet!

Table of contents

What is tomcat?

tomcat installation

1. Deploy the java environment

2. Deploy tomcat

3. Tomcat directory introduction

4. Start and shut down tomcat

5. Add tomcat system service

1. Install and configure mariadb database

2. Upload jpress code

3. Web page configuration jpress

tomcat multiple instances

1. Tomcat multi-instance introduction

2. Copy directory

3. Modify the configuration file

4. Start multiple instances

5. Check whether the service is started

6. Copy the new jpress to webapps

7.mysql new database, jpress1 and jpress2

8. The client accesses 8081, 8082 and installs jpress

9. Copy the content of the jpress file to ROOT

nginx reverse proxy (192.168.8.128)

1. Experimental environment preparation

2. Install and configure nginx

3. Create a proxy configuration file

4. Check and start nginx

5. Modify the client 192.168.8.128 hosts, domain name access


What is tomcat?

Tomcat is a core project in the Jakarta project of the Apache Software Foundation (Apache Software Foundation), jointly developed by Apache, Sun and some other companies and individuals. Thanks to Sun's participation and support, the latest Servlet and JSP specifications can always be reflected in Tomcat, and Tomcat 5 supports the latest Servlet 2.4 and JSP 2.0 specifications. Because Tomcat has advanced technology, stable performance, and is free, it is deeply loved by Java enthusiasts and recognized by some software developers, becoming a relatively popular Web application server. Tomcat server is a free and open source web application server, which is a lightweight application server. It is widely used in small and medium-sized systems and occasions where there are not many concurrent access users. It is the first choice for developing and debugging JSP programs. For a beginner, it can be considered that when the Apache server is configured on a machine, it can be used to respond to the access request of the HTML (an application under the standard general markup language) page. In fact, Tomcat is an extension of the Apache server, but it runs independently at runtime, so when the company runs tomcat, it actually runs as a separate process from Apache. The trick is that, when configured correctly, Apache serves HTML pages, while Tomcat actually runs JSP pages and servlets. In addition, Tomcat, like web servers such as IIS, has the function of processing HTML pages. In addition, it is also a Servlet and JSP container. The independent Servlet container is the default mode of Tomcat. However, Tomcat's ability to handle static HTML is not as good as the Apache server. The latest version of Tomcat is 10.0.23.

tomcat installation

1. Deploy the java environment

yum install java-1.8.0 -y
java -version

2. Deploy tomcat

mkdir /data/soft -p
cd /data/soft/
copy tomcat package to /data/soft

tar zxf apache-tomcat-8.5.64.tar.gz -C /opt/
cd /opt
ln -s apache-tomcat-8.5.64 tomcat
/opt/tomcat/bin/startup.sh                 #启动tomcat

netstat -lntup|grep 8080 #View port
tail -1 /opt/tomcat/logs/catalina.out #View log
curl -I 127.0.0.1:8080 #Local access test

3. Tomcat directory introduction

List

cd /opt/tomcat/
tree -L 1
├── bin #Scripts (.bat and .sh) used to start and close Tomcat or other script functions
├── conf #Used to configure Tomcat's XML and DTD files
├── lib #Store JAR packages that web applications can access
├── logs #Catalina and other web application log files
├── temp #temporary file
├── webapps #Web application root directory
└── work #Used to generate the .java and .class files of the Servlet compiled by JSP

webapps directory
cd webapps/
ll
total usage 8
drwxr-x--- 14 root root 4096 August 10 16:37 docs #tomcat help document drwxr-x--- 6 root root
78 August 10 16:37 examples #web application drwxr-x---
5 root root 82 August 10 16:37 host-manager #Manage drwxr
-x--- 5 root root 97 August 10 16:37 manager #Manage
drwxr-x--- 3 root root 4096 August 10 16:37 ROOT #Default website root directory

bin directory
script function
startup.sh open tomcat script
shutdown.sh close tomcat script
catalina.sh core management script, after jvm optimization parameters and related configuration, modify tomcat startup parameters

4. Start and shut down tomcat

Script mode:
/opt/tomcat/bin/startup.sh
/opt/tomcat/bin/shutdown.sh


5. Add tomcat system service

cat >> /opt/tomcat/bin/setenv.sh << "END"
# Set tomcat pid
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
# Set java parameters to improve performance
JAVA_OPTS="-server -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=1024m -Xms512M -Xmx1024M -X X:MaxNewSize=256m"
END

#Set permissions
chmod +x /opt/tomcat/bin/setenv.sh
#Create tomcat users and groups
groupadd -r tomcat
useradd -r -d /opt/tomcat -s /bin/nologin -g tomcat tomcat
chown -R tomcat:tomcat /opt/tomcat

#配置systemctl管理tomcat
cat >> /usr/lib/systemd/system/tomcat.service << "END"
[Unit]
Description=Apache Tomcat 8
After=syslog.target network.target
 
[Service]
Type=forking
PIDFile=/opt/tomcat/tomcat.pid
ExecStart=/opt/tomcat/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target
END

Start tomcat:
systemctl start tomcat

Note: If an error is reported, you can't start: CHMOD -R 777 /OPT /TOMCAT
#############################################################

1. Install and configure mariadb database

rpm -ivh http://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server -y
systemctl start mysqld
systemctl enable mysqld
mysqladmin -uroot -p password 123456

mysql -uroot -p123456
> create database jpress default charset utf8;
>  flush privileges;

2. Upload jpress code

mv jpress.war /opt/tomcat/webapps/

3. Web page configuration jpress

Open the browser: http://192.168.8.129:8080/jpress/In
the installation process, the database user uses root

Background login page: http://192.168.8.129:8080/jpress/admin


##############################################################

tomcat multiple instances

1. Tomcat multi-instance introduction

Its essence is to copy multiple tomcat directories, then modify them to different ports and start the
same code, but share a database

2. Copy directory

cd /opt/
cp -a apache-tomcat-8.5.66 tomcat1
cp -a apache-tomcat-8.5.66 tomcat2
delete jpress in the webapps of tomcat1 and tomcat2
rm -rf /opt/tomcat1/webapps/jpress*
rm -rf /opt/tomcat2/webapps/jpress*

3. Modify the configuration file

修改端口号
sed -i 's#8005#8006#g'  tomcat1/conf/server.xml
sed -i 's#8009#8010#g'  tomcat1/conf/server.xml
sed -i 's#8080#8081#g'  tomcat1/conf/server.xml
sed -i 's#8005#8007#g'  tomcat2/conf/server.xml
sed -i 's#8009#8011#g'  tomcat2/conf/server.xml
sed -i 's#8080#8082#g'  tomcat2/conf/server.xml

4. Start multiple instances

/opt/tomcat1/bin/startup.sh

/opt/tomcat2/bin/startup.sh


5. Check whether the service is started

netstat -anput |grep java

6. Copy the new jpress to webapps

cp /data/soft/jpress.war  /opt/tomcat1/webapps/
cp /data/soft/jpress.war  /opt/tomcat1/webapps/

7.mysql new database, jpress1 and jpress2

mysql -uroot -p123456
create database jpress1 default charset utf8;
create database jpress2 default charset utf8;

8. The client accesses 8081, 8082 and installs jpress

9. Copy the content of the jpress file to ROOT

cp -a  /opt/tomcat1/webapps/jpress/*   /opt/tomcat1/webapps/ROOT
cp -a  /opt/tomcat1/webapps/jpress/*   /opt/tomcat1/webapps/ROOT

######################################################################################

nginx reverse proxy (192.168.8.128)

1. Experimental environment preparation

a. Multi-instance tomcat 8081 8082
b. The database uses the shared db 192.168.8.129:3306
c. The code uses the jpress of their respective directories
d. Use nginx reverse proxy to the 2 ports of the backend

2. Install and configure nginx

yum -y install epel-release
yum -y install nginx

3. Create a proxy configuration file

vim  /etc/nginx/conf.d/proxy.conf
添加:
upstream java {
    server 192.168.8.129:8081;
    server 192.168.8.129:8082;
}
server {
    listen       80;
    server_name  www.jpress.com;
    root   html;
    index  index.html index.htm;
    location / {
        proxy_pass http://java;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
保存退出

4. Check and start nginx

nginx -t
systemctl start nginx

5. Modify the client 192.168.8.128 hosts, domain name access

vim  /etc/hosts
192.168.8.128 www.jpress.com

Open the browser: www.jpress.com

♥️Following is the driving force for my creation

♥️Like, is the greatest recognition for me

♥️This is Xiaoliu, I am inspiring to do every article well, thank you everyone

Guess you like

Origin blog.csdn.net/lzl10211345/article/details/131870108