欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux环境下JDK,Tomcat,MySQL的安装 Java Web项目部署

程序员文章站 2024-03-25 20:31:10
...

Linux(CentOS8操作系统)

JDK安装

  1. 使用CRT工具远程连接Linux(CentOS),Alt + P 打开上传窗口,拖入jdk-8u261-linux-x64.tar.gz,上传 到Linux的/root目录
  2. 创建/usr/local/java目录
    [[email protected] ~]#mkdir -p /usr/local/java
  3. 解压jdk-8u261-linux-x64.tar.gz到/usr/local/java
    [[email protected] ~]#tar -zxvf jdk-8u261-linux-x64.tar.gz -C /usr/local/java
  4. [[email protected] ~]#vim /etc/profile
    在第二行添加环境变量
export JAVA_HOME=/usr/local/java/jdk1.8.0_261
export CLASSPATH=.:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin
  1. 使用source命令让环境变量生效
    [[email protected] ~]#source /etc/profile
  2. 验证JDK是否生效
    [[email protected] ~]#java -version

Tomcat

  1. 使用CRT工具远程连接Linux(CentOS),Alt + P 打开上传窗口,拖入apache-tomcat-9.0.39.tar.gz,上传到Linux的/root

  2. 将apache-tomcat-9.0.39.tar.gz解压到/usr/local目录下
    [[email protected] ~]#tar -zxvf apache-tomcat-9.0.39.tar.gz -C /usr/local

  3. 将解压后的apache-tomcat-9.0.39目录修改为tomcat
    [[email protected] ~]#cd /usr/local
    [[email protected] local]#mv apache-tomcat-9.0.39 tomcat

  4. 启动tomcat
    [[email protected] local]#cd tomcat/bin
    [[email protected] bin]#./startup.sh
    [[email protected] bin]#ps -ef | grep tomcat

  5. Window*问Tomcat
    http://192.168.45.128:8080 访问不了
    1).考虑Linux上的防火墙是否开启。
    [[email protected] bin]#systemctl stop firewalld #停止防火墙
    [[email protected] bin]#systemctl status firewalld #查看防火墙状态
    [[email protected] bin]#systemctl disable firewalld #禁用防火墙

    2).查看tomcat启动日志 /usr/local/tomcat/logs
    [[email protected] ~]#tail -f /usr/local/tomcat/logs/catalita.out

  6. 停止tomcat
    [[email protected] local]#cd tomcat/bin
    [[email protected] bin]#./shutdown.sh

Tomcat 开机自启动

  1. 修改bin目录下的catalina.sh文件
    第二行添加如下内容
    [[email protected] bin]# vim /usr/local/tomcat/bin/catalina.sh
export CATALINA_HOME=/usr/local/tomcat
export CATALINA_BASE=/usr/local/tomcat
#add tomcat pid
CATALINA_PID="${CATALINA_BASE}/tomcat.pid"
#add java opts
JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"
  1. 增加tomcat.service
    在/usr/lib/systemd/system目录下增加tomcat.service
    [[email protected] bin]#cd /usr/lib/systemd/system
    [[email protected] system]#touch /usr/lib/systemd/system/tomcat.service
    [[email protected] system]#vim /usr/lib/systemd/system/tomcat.service
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]  
Type=forking  
Environment="JAVA_HOME=/usr/local/java/jdk1.8.0_261"
PIDFile=/usr/local/tomcat/tomcat.pid    
ExecStart= /usr/local/tomcat/bin/startup.sh
ExecStop= /usr/local/tomcat/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit= yes
[Install]
WantedBy=multi-user.target
  1. 使用tomcat.service
    修改tomcat.service文件后需要执行下面命令使其生效:
    [[email protected] system]# systemctl daemon-reload #重新载入修改后配置文件
    [[email protected] system]# systemctl enable tomcat #配置开机启动
    [[email protected] system]# systemctl start tomcat #启动tomcat
    [[email protected] system]# systemctl status tomcat #查看tomcat状态
    [[email protected] system]# systemctl stop tomcat #停止tomcat
    [[email protected] system]# systemctl restart tomcat #重启tomcat

因为配置pid,在启动的时候会再tomcat根目录生成tomcat.pid文件,停止之后删除。
同时tomcat在启动时候,执行start不会启动两个tomcat,保证始终只有一个tomcat服务在运行。 多个tomcat可以配置在多个目录下,互不影响。

  1. 测试Tomcat开机自启动
    [[email protected] ~]#reboot #重启Linux系统
    [[email protected] ~]#systemctl status tomcat.service

  2. 查询tomcat 进程
    [[email protected]~]#ps -ef | grep tomcat
    如果多个tomcat,则拷贝到不同的目录,使用不同的端口。
    tomcat.service文件名不同即可。
    例如:tomcat1.service、 tomcat2.service、 tomcat3.service

MySQL

  1. 使用CRT工具远程连接Linux(CentOS),Alt + P 打开上传窗口,拖入mysql-8.0.22-1.el8.x86_64.rpm-bundle.tar,上传到Linux的/root

  2. 卸载CentOS上已经安装过的MySQL(CentOS7默认会安装一个MySQL)
    [[email protected] ~]#rpm -qa | grep mysql #查询Linux系统是否安装过MySQL
    [[email protected] ~]#rpm -e --nodeps 包名 #强制卸载

  3. 创建MySQL安装目录
    [[email protected] ~]#mkdir -p /usr/local/mysql
    [[email protected] ~]#chmod -R 777 /usr/local/mysql

  4. 将mysql-8.0.22-1.el8.x86_64.rpm-bundle.tar解压到/usr/local/mysql
    [[email protected] ~]#tar -xvf mysql-8.0.22-1.el8.x86_64.rpm-bundle.tar -C /usr/local/mysql
    [[email protected] ~]#cd /usr/local/mysql
    [[email protected] mysql]#ll
    mysql-community-common-8.0.22-1.el8.x86_64.rpm
    mysql-community-client-plugins-8.0.22-1.el8.x86_64.rpm
    mysql-community-libs-8.0.22-1.el8.x86_64.rpm
    mysql-community-client-8.0.22-1.el8.x86_64.rpm
    mysql-community-server-8.0.22-1.el8.x86_64.rpm
    [[email protected] mysql]#rpm -ivh mysql-community-common-8.0.22-1.el8.x86_64.rpm
    [[email protected] mysql]#rpm -ivh mysql-community-client-plugins-8.0.22-1.el8.x86_64.rpm
    [[email protected] mysql]#rpm -ivh mysql-community-libs-8.0.22-1.el8.x86_64.rpm
    [[email protected] mysql]#rpm -ivh mysql-community-client-8.0.22-1.el8.x86_64.rpm
    [[email protected] mysql]#rpm -ivh mysql-community-server-8.0.22-1.el8.x86_64.rpm

  5. 验证MySQL是否安装成功
    [[email protected] mysql]#rpm -qa | grep mysql

  6. 初始化MySQL
    [[email protected] mysql]#mysqld --initialize-insecure #初始化无密码
    [[email protected] mysql]#chown mysql:mysql /var/lib/mysql -R

  7. 启动MySQL
    [[email protected] mysql]#systemctl start mysqld
    [[email protected] mysql]#systemctl status mysqld

  8. 登录MySQL
    [[email protected] mysql]#mysql -u root -p
    直接回车
    mysql>alter user ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘root’;
    mysql>flush privileges;

  9. 开启远程连接
    mysql>use mysql;
    mysql>update user set host=’%’ where user=‘root’;
    mysql>grant all privileges on . to ‘root’@’%’;
    mysql>flush privileges;

  10. Window上使用SQLyog远程连接Linux系统上的MySQL服务器。
    新建连接,连接192.168.45.128服务器上的MySQL

p2p_management打包war部署到Linux服务器

  1. window上使用SQLYog连接Linux服务器上的MySQL,执行p2p.sql

  2. IDEA将p2p_management工程打包为p2p.war
    a.修改工程中的druid.properties的数据库服务器地址。
    b.File–Project Structure…—Artifacts—点击+号----选择Web Application:Archive----选择第二个For “p2p_manange”…
    c.修改name为p2p, 输出路径E:\p2p_management\out\artifacts\p2p
    d.点击OK
    e.点击Build----Build Artifacts…选择p2p—点击Build
    f.war包输出到E:\p2p_management\out\artifacts\p2p\p2p.war

  3. 将p2p.war上传到Linux服务器上tomcat/webapps目录下。
    [[email protected] ~]# mv p2p.war /usr/local/tomcat/webapps/

  4. 启动Tomcat。

  5. Window上打开浏览器访问http://192.168.45.128:8080/p2p

相关标签: java linux