我采用的是最傳統(tǒng)的方法把項目打包war,上傳到服務(wù)器tomcat webapps 目錄下陵刹。服務(wù)器Ubuntu系統(tǒng)
1. 阿里云服務(wù)器配置
先安裝mysql
apt-get install mysql-server
然后修改mysql配置 讓3306端口可以遠程訪問迂求,修改/etc/mysql/mysql.conf.d/mysqld.cnf
文件,把bind-address = 127.0.0.1
這行注釋掉碾盐。
然后grant all on *.* to root@'%' identified by 'password';
授權(quán)root用戶遠程訪問mysql。
執(zhí)行netstat -an | grep 3306
檢測3306端口
tcp6 0 0 :::3306 :::* LISTEN
3306前面為0表示已經(jīng)可以遠程訪問了揩局。
但是阿里云服務(wù)器3306端口沒有開啟毫玖,需要 安全組->配置規(guī)則里面添加3306.如圖
然后修改項目數(shù)據(jù)庫地址
spring.datasource.url=jdbc:mysql://*.*.*.*:3306/databasename?autoReconnect=true&useSSL=false
運行項目付枫,如果沒有問題,就進行下一步打包驰怎。
2. 項目打包
spring boot 打war包励背。基本步驟
修改pom文件砸西,
jar
中jar改成war叶眉。修改web 依賴 去掉內(nèi)嵌的tomcat址儒。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
- 修改Application.java 文件
// 繼承SpringBootServletInitializer
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
// 重寫方法
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
private static Class applicationClass = Application.class;
}
- 這個時候如果直接點擊運行按鈕的話,會出錯衅疙,這是Idea的bug莲趣,需要
mvn spring-boot:run
一遍就好了。然后打war包饱溢。
3.上傳
上傳
scp ~/Desktop/demo.war root@x.x.x.x:/usr/java/tomcat/apache-tomcat-8.5.15/webapps/
4. 訪問
在瀏覽器中輸入http:x.x.x.x/demo/
就可以訪問了喧伞。
可能遇到的問題
如果不能訪問,可能是阿里云服務(wù)器默認80端口沒開啟绩郎,去配置一下就行潘鲫。
如果返回404,去tomcat/logs 目錄下肋杖,查看一下log溉仑,查找原因。