部署springboot项目到服务器上

一、免费领取阿里云ECS服务器

先进入阿里云官网

1

然后看上面,开发者->成长计划

img

进去后,点学生专享->免费领取

1

然后点立即领取,要先做题,这些题都很简单。做完之后点领取(0元购买),配置自己选择就行(我选的是ubuntu20.04)。

然后可以通过以下方式进入管理ECS面板。

拖到最后点击云服务器ECS

2

点击管理控制台即可进入

3

首次登录,需要先重置密码,点击管理->重置实例密码

4

重置之后,便可以尝试本地连接ECS,使用xshell6连接。下载地址:https://xshell.en.softonic.com/

5

然后输入用户名和刚才改过的密码就行。

6

这样就算成功了!!!

二、配置jdk

下载地址:https://www.oracle.com/java/technologies/javase-downloads.html

我下载的是jdk1.8,下载前需要注册一个oracle账号

7

选择linux版本,

8

下载好后使用xftp6传到服务器上。

进入xshell ,先解压压缩包

tar -zxvf jdk-8u11-linux-x64.tar.gz

更改etc/profile或~/.bashrc (G直接到最后一行,gg到第一行)

vim /etc/profile或
vim ~/.bashrc

添加java的配置

export JAVA_HOME=/usr/jdk1.8.0_291
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JRE_HOME=$JAVA_HOME/jre

使环境生效

source /etc/profile或
source ~/.bashrc

三、配置tomcat(可选)

若部署jar包可以忽略这一步。

部署war包需要配置tomcat。

下载Tomcat,下载地址:https://tomcat.apache.org/download-90.cgi

9

选择core下的linux版本

使用xftp传到服务器上

先将压缩包解压

sudo tar -zxvf apache-tomcat-8.5.31.tar.gz

开权限(主要是为了之后执行startup.sh开启执行权限):

sudo chmod 755 -R apache-tomcat-8.5.31

进入startup.sh,

vim startup.sh

配置环境

#set java environment
export JAVA_HOME=/usr/jdk1.8.0_291
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:%{JAVA_HOME}/lib:%{JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

#tomcat
export TOMCAT_HOME=/usr/apache-tomcat-9.0.45

启动服务

sudo ./startup.sh

测试:在xshell 输入

curl localhost:8080

或在浏览器中输入

公网ip:8080

四、配置mysql数据库(可选)

① 可以考虑使用阿里云的rds云数据库,就不用在本地装MySQL(略)

② 在本地装mysql (8.0.23)

mysql在ubuntu的apt软件包存储库中,先更新服务器的包索引再安装

sudo apt-get update
sudo apt-get install mysql-server

初始化配置

sudo mysql_secure_installation
#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的选项)

#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)

#3
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)

#4
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y (我的选项)

#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)

#6
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (我的选项)

检查mysql 服务状态

systemctl status mysql.service

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o0oPVhAF-1619877089698)(https://gitee.com/alexander1/pic/raw/master/搭建服务器/20210501204718.png)]

之后,便可以使用根用户登陆了:

sudo mysql -u root -p

2

由于ubuntu默认只允许本地访问,可以进行如下配置:

可以先把绑定端口关掉

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
注释掉bind-address = 127.0.0.1, 即#bind-address = 127.0.0.1
重启服务
sudo /etc/init.d/mysql restart
查看端口状态
netstat -apn |grep 3306
此时应为:tcp6 0 0 :::3306 :::* LISTEN -

修改用户表

当远程访问出现not allowed的提示消息时,说明远程用户无权限,则需要修改用户表:
1. 登录数据库
mysql -u root -p
2. 选择数据库
use mysql;
3. 修改root用户可以在所有机器登录(root只是举例,%表示所有机器)
update user set host = '%' where user = 'root';
4. 重启服务
sudo /etc/init.d/mysql restart

五、部署jar或war包

区别:

jar包的方式:不需要外置的tomcat,可以直接运行,如果关掉shell窗口服务就会停止,需要另外再配置

war包的方式:需要排除springboot自带的tomcat,关掉shell窗口服务不会停止。

1.jar包

3

在pom.xml中加入打包工具

4

点击右上角边上maven,先clean,再package,生成出来的jar包在target文件夹下

然后丢到服务器上执行

java -jar covermanager-0.0.2-SNAPSHOT.jar

便可以运行了

2.war包

先让启动类继承SpringBootServletInitializer,并实现configure方法

@SpringBootApplication
public class CovermanagerApplication extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(CovermanagerApplication.class, args);
		System.out.println("******************************启动成功********************************");
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(CovermanagerApplication.class);
	}

}

在依赖中排除springboot自带的tomcat

5

然后如jar包一样,clean之后在package,生成的war包在target文件夹下

将war包解压缩,并将文件名改为ROOT

6

进入服务器的tomcat的webapps中,删除原有的ROOT/

cd /usr/apache-tomcat-9.0.45/webapps/
rm -r ROOT/

将解压后的ROOT包利用xftp传到服务器的/usr/apache-tomcat-9.0.45/webapps/目录下

然后进入/usr/apache-tomcat-9.0.45/bin/启动tomcat服务器即可

./startup.sh
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值