目 录CONTENT

文章目录

java项目部署案例

vanegoo
2024-04-23 / 0 评论 / 0 点赞 / 14 阅读 / 7801 字 / 正在检测是否收录...

环境部署

Tomcat部署

安装jdk

[root@demo ~]# yum -y install java-11-openjdk-devel.x86_64

测试jre环境

[root@demo tomcat]# jps
3943867 Jps

部署Tomcat

# 下载Tomcat
[root@demo ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.86/bin/apache-tomcat-9.0.86.tar.gz
# 部署
[root@demo ~]# tar -xf apache-tomcat-9.0.86.tar.gz
[root@demo ~]# mv apache-tomcat-9.0.76 /usr/local/tomcat
[root@demo ~]# cd /usr/local/tomcat/

启动Tomcat并测试

[root@demo tomcat]# ./bin/startup.sh 
[root@demo tomcat]# jps
3946189 Bootstrap
3959845 Jps
[root@demo tomcat]# ss -antupl | grep java
tcp   LISTEN 0      1      [::ffff:127.0.0.1]:8005             *:*    users:(("java",pid=3946189,fd=51))                           
tcp   LISTEN 0      100                     *:8080             *:*    users:(("java",pid=3946189,fd=43))   

# 访问测试
[root@demo tomcat]# curl -I http://localhost:8080/
HTTP/1.1 200 
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 24 Apr 2024 02:42:54 GMT

Tomcat配置:Tomcat配置详解

部署meven

安装maven

[root@demo tomcat]# yum -y install maven.noarch
[root@demo tomcat]# mvn -v
Apache Maven 3.6.3 (Red Hat 3.6.3-15)
Maven home: /usr/share/maven
Java version: 11.0.22, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-11.0.22.0.7-2.el9.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.14.0-362.24.1.el9_3.x86_64", arch: "amd64", family: "unix"

配置maven仓库(使用国内镜像源,这里使用阿里云中央仓库)

[root@demo ~]# vim /etc/maven/settings.xml

  <mirror>
    <id>nexus-aliyun</id>
    <mirrorOf>*</mirrorOf>
    <name>Nexus aliyun</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
  </mirror>
</mirrors>    #本行是原本存在的

部署mysql并配置root密码

[root@demo ~]# yum -y install mysql mysql-server
[root@demo ~]# mysql
mysql> ALTER USER root@"localhost" IDENTIFIED BY "<<密码>>";

项目文件、数据库准备

点这里下载项目文件 提取码:cr0v 。然后上传项目文件到root目录

[root@demo tomcat]# tar -xf english.tar.gz
[root@demo tomcat]# cd english/
[root@demo english]# ls
javaweb-maven-01.iml  LICENSE  pom.xml  README.md  sql  src

[root@demo english]# ls sql/
English.sql

[root@demo english]# mysql -hlocalhost -uroot -p'123456' < sql/English.sql 
[root@demo english]# mysql -hlocalhost -uroot -p'123456' -e "SHOW TABLES FROM english;"
+-------------------+
| Tables_in_english |
+-------------------+
| 单词              |
| 用户              |
| 记录              |
+-------------------+

[root@demo english]# vim src/main/java/com/yang/util/DBUtil.java 
[root@demo english]# sed -rn '16,21p' src/main/java/com/yang/util/DBUtil.java
public static Connection getConn() throws Exception {
        //2.得到一个连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/english?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false",
                "root", "123456");
        return conn;
    }
[root@demo english]# vim src/main/java/com/yang/util/DBUtil.java
# 配置数据库地址和 用户、密码为实际的

部署应用

打包

[root@demo english]# ls pom.xml 
[root@demo english]# mvn clean package

[root@demo english]# ls
javaweb-maven-01.iml  LICENSE  pom.xml  README.md  sql  src  target
[root@demo english]# ls target/
classes  english  english.war  generated-sources  maven-archiver  maven-status

[root@english english]# file target/english.war 
target/english.war: Zip archive data, at least v2.0 to extract

部署war包(Tomcat运行情况下 war包自动解包)

[root@demo ~]# cp english/target/english.war /usr/local/tomcat/webapps/
[root@demo ~]# ls /usr/local/tomcat/webapps/
docs  english  english.war  examples  host-manager  manager  ROOT
[root@demo ~]# 

测试访问

访问URL:http://服务器IP:8080/english/

调整默认站点

[root@demo tomcat]# vim conf/server.xml 
[root@demo tomcat]# sed -rn '161,163p' conf/server.xml 
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Context path="" docBase="english" />
[root@demo tomcat]# ./bin/catalina.sh stop
[root@demo tomcat]# ./bin/catalina.sh start
[root@demo tomcat]#  

浏览器访问测试,访问URL:http://服务器IP:8080/

0

评论区