阿里云 Centos7 部署Java web [jar/war/nginx+域名服务]教程

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: 阿里云 Centos7 部署Java web [jar/war/nginx+域名服务]教程

(一)第一部分:最基本的jar包运行(无需配置tomcat)

1、配置阿里云(Esc学生服务器、镜像Centos7.7),并远程连接进入终端。
2、安装并配置JDK(参考https://www.jianshu.com/p/093413f2a04f)
申请阿里云服务时,可以使用2000元阿里云代金券,阿里云官网领取网址:https://dashi.aliyun.com/site/yun/youhui
a)安装jdk。
b)寻找jdk路径配置环境变量。
c)简单的Java hello world测试确保安装成功。

3、安装并配置mysql(mariadb)
a)安装mysql、mariadb server。
b)systemctl 开启服务。
c) 打开端口。
d) 更改mysql 的root密码,对应于项目配置文件中的密码。

阿里云服务器1核2G低至82元/年

4、利用scp进行文件传输
a)传输spring maven 的快照版本用于测试。
b)传输数据库sql文件。

5、导入数据库
a)创建sql文件对应的数据库。
b)利用文件重定向运行sql文件。
c)检查数据库是否导入成功

6、运行jar文件,控制台获取公网IP,本机输入IP:8081测试。
7、设置后台运行
a)contrl+c中止。
b)然后通过nohup 和 & 来后台运行。
c)ps通过pid来停止后台运行进程。

运行结果
整体过程:

[root@iZ2ze4r3b4xcztbcsey08cZ ~]# history

1   MAKRER=SHOW_LOCALE;printf $MAKRER""; locale; MAKRER=SHOW_LOCALE;printf $MAKRER"";
2  yum install -y mysql
3  yum install -y mariadb-server mariadb
4  systemctl start mariadb
5  systemctl enable mariadb
6  yum install -y mysql-devel
7  firewall-cmd --zone=public --add-port=3306/tcp --permanent
8   CHECK_TYPE=SHELL; echo "INFO=${CHECK_TYPE} PID=$$ PPID=$PPID TTY=$(tty) SHELL=$0 HOME=$HOME PWD=$PWD| CHECK_SHELL_END"
9  ls

10 ifconfig
11 ls
12 yum list
13 java -version
14 ls
15 yum search java-1.8
16 yum -y install java-1.8.0-openjdk-devel.x86_64
17 java -version
18 cd /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.el7_8.x86_64/
19 pwd
20 vim /etc/profile
21 . /etc/profile
22 vim Demo.java
23 javac Demo.java
24 java Demo
25 su
26 MAKRER=SHOW_LOCALE;printf $MAKRER""; locale; MAKRER=SHOW_LOCALE;printf $MAKRER"";
27 CHECK_TYPE=SHELL; echo "INFO=${CHECK_TYPE} PID=$$ PPID=$PPID TTY=$(tty) SHELL=$0 HOME=$HOME PWD=$PWD| CHECK_SHELL_END"
28 mysql -u root
29 ls
30 pwd
31 ifconfig
32 ls
33 mysql
34 mysql -u root jpetstore < jpetstore.sql
35 mariadb
36 mysql
37 java -jar mypetstore-0.0.3-SNAPSHOT.jar
38 nohup java -jar mypetstore-0.0.3-SNAPSHOT.jar &
39 ps
40 history
[root@iZ2ze4r3b4xcztbcsey08cZ ~]#
控制台开端口

利用scp传文件
利用scp传文件.png

运行mysql 文件

[root@iZ2ze4r3b4xcztbcsey08cZ ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database jpetstore;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> quit
Bye
[root@iZ2ze4r3b4xcztbcsey08cZ ~]# mysql -u root jpetstore < jpetstore.sql
[root@iZ2ze4r3b4xcztbcsey08cZ ~]# mariadb
-bash: mariadb: command not found
[root@iZ2ze4r3b4xcztbcsey08cZ ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
Database
information_schema
jpetstore
mysql
performance_schema
test

5 rows in set (0.00 sec)

MariaDB [(none)]> use jpetstore;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

MariaDB [jpetstore]> show tables;
Tables_in_jpetstore
account
addlog
bannerdata
browselog
cart
cartitem
category
inventory
item
lineitem
orders
orderstatus
product
profile
sequence
signon
supplier

17 rows in set (0.00 sec)

访问http.png

(二)第二部分:利用Tomcat容器对war包进行处理(需要Tomcat)

1、安装Tomcat
a)本地下载tomcat,然后利用scp文件传输
b)设置访问权限
c)设置服务配置
d)输入IP:8080测试是否已经开启服务

2、数据库/端口配置
a)数据库root 密码 和服务器对应
b)端口配置,在上面已经开启了8080端口

3、利用scp传输war包,放到webapp里边(直接会解析出文件)
4、重启tomcat服务
a)bin下的shutdown.sh
b)bin下的startup.sh

5、本机测试访问
a)注意要添加访问的资源的路径/myJPetStore_war/index.jsp,然后tomcat容器会自动到webapp下去寻找
b) 已设置tomcat容器

运行结果
整体过程

52 ls
53 sudo mkdir /opt/tomcat
54 sudo tar xvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
55 cd /opt/tomcat
56 sudo chgrp -R tomcat /opt/tomcat
57 sudo chmod -R g+r conf
58 sudo chmod g+x conf
59 sudo chown -R tomcat webapps/ work/ temp/ logs/
60 sudo vi /etc/systemd/system/tomcat.service
61 sudo systemctl daemon-reload
62 sudo systemctl start tomcat
63 sudo systemctl status tomcat
64 sudo systemctl enable tomcat
65 ls
66 cd webapps/
67 pwd
68 ls
69 sudo systemctl restart tomcat
70 cd ../bin/
71 ls
72 sh shutdown.sh
73 sh startup.sh
74 history
[root@iZ2ze4r3b4xcztbcsey08cZ bin]#
利用systemctl查看tomcat的状态
利用systemctl查.png

验证是否开启tomcat服务
验证是否开启t.png

将war包直接传入webapp中
将war包直接.png

然后webapp会自动解析war包(即myJPetStore_war)

[root@iZ2ze4r3b4xcztbcsey08cZ webapps]# pwd
/opt/tomcat/webapps
[root@iZ2ze4r3b4xcztbcsey08cZ webapps]# ls
docs examples host-manager manager myJPetStore_war myJPetStore_war.war ROOT
访问
jsp.png

(三)第三部分:指定域名进行访问

1、配置virmach vps,达到可以通过IP进行访问
a)过程同第一步,最后设置为后台运行。
b) 已配置

2、服务器安装web server
3、在域名提供商 name(或者cloudflare)进行DNS域名解析,进行请求中转。
a)ping 域名(ping crf.codes)查看是否绑定成功。
4、修改nginx 配置文件
a)Web server 设定域名
b)Location 配置index界面
c)开放Linux系统防火墙
d)访问crf.code

运行结果
域名提供商(name)配置dns
域名提供商(name).png

验证dns是否配置成功
ping crf.codes,可以看到
可以看到.png

修改nginx配置文件

server {

listen       80;
server_name   crf.codes;

#charset koi8-r;
access_log  /var/log/nginx/host.access.log  main;

location / {
    # root   /usr/share/nginx/html;
    index  index.html index.htm;
    proxy_pass http://127.0.0.1:8081/;
    # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}

访问crf.codes
访问crf.png

相关文章
|
15天前
|
监控 Linux PHP
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
65 20
|
5月前
|
Linux 网络安全 Python
linux centos上安装python3.11.x详细完整教程
这篇文章提供了在CentOS系统上安装Python 3.11.x版本的详细步骤,包括下载、解压、安装依赖、编译配置、解决常见错误以及版本验证。
3113 1
linux centos上安装python3.11.x详细完整教程
|
5月前
|
Linux 数据安全/隐私保护 Perl
CentOS7中升级OpenSSL详细教程
这篇文章提供了在CentOS 7系统中升级OpenSSL到3.2版本的详细步骤,包括备份现有配置、安装依赖、下载安装新版本以及验证安装结果。
1129 2
|
5月前
|
Kubernetes Java Docker
利用(K8S)配合Docker部署jar包
通过Docker打包并部署到Kubernetes(K8S)集群的过程。首先,通过SpringBoot生成jar包,接着在K8S环境中创建并编辑Dockerfile文件。随后构建Docker镜像,并将其推送到镜像仓库。最后,通过一系列kubectl命令(如get pods、get svc、logs等),展示了如何在K8S中管理应用,包括查看Pod状态、服务信息、Pod日志以及重启Pod等操作。
264 3
|
1月前
|
弹性计算 运维 安全
阿里云操作系统迁移最佳实践|飞天技术沙龙-CentOS 迁移替换专场
本次方案的主题是阿里云操作系统迁移最佳实践,Alibaba Cloud Linux /Anolis OS 兼容 CentOS 生态,因此能够很丝滑的进行迁移替换。无论是对企业的运维人员,还是对企业操作系统的使用者来说,相对简化了它的维护成本。通过 SMC 操作系统迁移实践带用户深入了解,不仅阐述了原地迁移方案的独特优势,还针对不同的迁移场景,逐步剖析了整个迁移流程,力求使复杂的操作变得直观易懂,实现了真正的“白屏化”体验。 1. CentOS 迁移背景 2. 操作系统迁移实践 3. 迁移故障处理
|
1月前
|
监控 安全 Linux
龙蜥社区及阿里云CentOS迁移方案|飞天技术沙龙-CentOS 迁移替换专场
本次分享的主题是龙蜥社区及阿里云 CentOS 迁移方案|飞天技术沙龙- CentOS 迁移替换专场,由阿里云产品专家周絮分享。主要分为三个部分: 1.背景介绍 2.方案选型 3.迁移支持
|
2月前
|
缓存 Linux
解决CentOS 7停止更新后yum源失效问题【图文教程】
以上步骤完成后,你的 CentOS 7 系统就会使用阿里云的 yum 源,更换yum以后就可以正常使用啦。
2131 2
|
4月前
|
Linux Docker 容器
Centos安装docker(linux安装docker)——超详细小白可操作手把手教程,包好用!!!
本篇博客重在讲解Centos安装docker,经博主多次在不同服务器上测试,极其的稳定,尤其是阿里的服务器,一路复制命令畅通无阻。
5121 4
Centos安装docker(linux安装docker)——超详细小白可操作手把手教程,包好用!!!
|
4月前
|
存储 数据可视化 Java
震惊!如何在linux下部署项目,部署/运行jar包 超详细保姆级教程!
如何在Linux系统下部署和运行Java项目jar包,包括传输文件到Linux、使用nohup命令运行jar包、查看端口状态、杀死进程和查看项目运行状态,以及如何解决“没有主清单属性”的错误。
955 2
震惊!如何在linux下部署项目,部署/运行jar包 超详细保姆级教程!
|
4月前
|
监控 安全 Linux
CentOS7下安装配置ntp服务的方法教程
通过以上步骤,您不仅能在CentOS 7系统中成功部署NTP服务,还能确保其配置合理、运行稳定,为系统时间的精确性提供保障。欲了解更多高级配置或遇到特定问题,提供了丰富的服务器管理和优化资源,可作为进一步学习和求助的平台。
508 1

热门文章

最新文章