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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: (一)第一部分:最基本的jar包运行(无需配置tomcat) [http://60.205.183.114:8081/] 1、配置阿里云(Esc学生服务器、镜像Centos7.7),并远程连接进入终端。

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

[http://60.205.183.114:8081/]

a)安装jdk。
b)寻找jdk路径配置环境变量。
c)简单的Java hello world测试确保安装成功。

a)安装mysql、mariadb server。
b)systemctl 开启服务。
c) 打开端口。(阿里云要在控制台打开,参考https://yq.aliyun.com/articles/701181)
d) 更改mysql 的root密码,对应于项目配置文件中的密码。

a)传输spring maven 的快照版本用于测试。
b)传输数据库sql文件。

  • 5、导入数据库

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

  • 6、运行jar文件,控制台获取公网IP,本机输入IP:8081测试。
  • 7、设置后台运行(已设置:http://60.205.183.114:8081/)

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传文件
image

运行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://60.205.183.114:8081/

image

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

[http://60.205.183.114:8080/myJPetStore_war/index.jsp]

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容器:http://60.205.183.114:8080/myJPetStore_war/index.jsp

运行结果
整体过程

   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的状态
image

验证是否开启tomcat服务
image

将war包直接传入webapp中
image

然后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

访问http://60.205.183.114:8080/myJPetStore_war/index.jsp

image

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

[http://crf.codes/]

  • 1、配置virmach vps,达到可以通过IP进行访问

a)过程同第一步,最后设置为后台运行。
b) 已配置:http://198.12.120.212:8081/

a)Web server 设定域名
b)Location 配置index界面
c)开放Linux系统防火墙
d)访问crf.code http://crf.codes/

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

image

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

修改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

image

相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
目录
相关文章
|
29天前
|
应用服务中间件 nginx
Nginx中如何配置中文域名?
Nginx中如何配置中文域名?
39 0
|
1月前
|
弹性计算 前端开发 小程序
微信小程序上传文件至阿里云OSS直传(java后端签名+前端直传)
当前的通用文件上传方式是通过前端上传到服务器,再由服务器转存至对象存储。这种方式在处理小文件时效率尚可,但大文件上传因受限于服务器带宽,速度较慢。例如,一个100MB的文件在5Mbps带宽的阿里云ECS上上传至服务器需160秒。为解决此问题,可以采用后端签名的方式,使微信小程序直接上传文件到阿里云OSS,绕过服务器中转。具体操作包括在JAVA后端引入相关依赖,生成签名,并在微信小程序前端使用这个签名进行文件上传,注意设置正确的请求头和formData参数。这样能提高大文件上传的速度。
|
1月前
|
弹性计算 运维 安全
2024年阿里云一键搭建部署幻兽帕鲁服务器解决方案
幻兽帕鲁火了,为了确保畅快体验游戏,构建高效、稳定的游戏服务器至关重要。幸运的是,阿里云为您提供了快速、简便的服务器搭建解决方案,即使您对技术知识了解有限,也能在短短一分钟内轻松完成《幻兽帕鲁》游戏的联机服务器搭建!
1450 4
|
1天前
|
Java 应用服务中间件 Linux
在阿里云服务器上部署Tomcat详细图文详解
本文介绍了在阿里云服务器上安装和配置JDK和Tomcat的步骤。首先,需要注册阿里云账号并进行实名认证,然后购买并设置服务器。接着,通过File Zilla连接服务器,创建Java和Tomcat的安装目录,并将JDK和Tomcat的tar.gz文件上传到服务器,解压并重命名。之后,配置JDK的环境变量,将catalina.sh复制到/etc/init.d/目录下,并修改相关配置。最后,启动Tomcat并配置安全组规则,确保可以通过公网访问。
|
2天前
|
弹性计算 JavaScript Java
阿里云服务器搭建部署宝塔详细流程
以下是内容的摘要: 本文主要介绍了在阿里云上创建和配置服务器环境的步骤,包括注册阿里云账号、实名认证、购买和设置服务器、域名的获取与备案、以及使用宝塔面板安装和配置环境。首先,用户需要注册阿里云账号并进行实名认证,选择合适的服务器配置。接着,购买服务器后,要准备并备案域名,以便通过友好的网址访问网站。在服务器上安装宝塔面板,可以方便地管理和配置LAMP/LNMP/Tomcat/Node.js等应用环境。完成这些步骤后,用户还需要在宝塔面板中安装MySQL、Redis等数据库,部署Java或Vue项目,并配置相关端口。最后,将前端项目打包上传至服务器,并设置站点,即可实现网站的上线。
|
2天前
|
应用服务中间件 Linux 开发工具
如何在阿里云服务器快速搭建部署Nginx环境
以下是内容的摘要: 本文档主要介绍了在阿里云上购买和配置服务器的步骤,包括注册阿里云账号、实名认证、选择和购买云服务器、配置安全组、使用Xshell和Xftp进行远程连接和文件传输,以及安装和配置Nginx服务器的过程。在完成这些步骤后,你将能够在服务器上部署和运行自己的网站或应用。
|
4天前
|
NoSQL 关系型数据库 MySQL
阿里云服务器部署项目流程
本文主要讲解阿里云服务器的部署,如何选择配置等
|
7天前
|
Java 应用服务中间件 Linux
阿里云服务器部署多个tomcat
阿里云服务器部署多个tomcat
|
18天前
|
Java
java中jar启动设置内存大小java -jar 设置堆栈内存大小
java中jar启动设置内存大小java -jar 设置堆栈内存大小
11 1
|
22天前
|
存储 缓存 负载均衡
阿里云DNS常见问题之域名DNS跳转有问题如何解决
阿里云DNS(Domain Name System)服务是一个高可用和可扩展的云端DNS服务,用于将域名转换为IP地址,从而让用户能够通过域名访问云端资源。以下是一些关于阿里云DNS服务的常见问题合集: