我采用的是最傳統(tǒng)的方法把項目打包war,上傳到服務器tomcat webapps 目錄下。服務器Ubuntu系統(tǒng)
1. 阿里云服務器配置
先安裝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';授權root用戶遠程訪問mysql。
執(zhí)行netstat -an | grep 3306檢測3306端口
tcp6 0 0 :::3306 :::* LISTEN 3306前面為0表示已經(jīng)可以遠程訪問了。
但是阿里云服務器3306端口沒有開啟,需要 安全組->配置規(guī)則里面添加3306.如圖


然后修改項目數(shù)據(jù)庫地址
spring.datasource.url=jdbc:mysql://*.*.*.*:3306/databasename?autoReconnect=true&useSSL=false
運行項目,如果沒有問題,就進行下一步打包。
2. 項目打包
spring boot 打war包?;静襟E
修改pom文件,
jar中jar改成war。修改web 依賴 去掉內嵌的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/就可以訪問了。
可能遇到的問題
如果不能訪問,可能是阿里云服務器默認80端口沒開啟,去配置一下就行。
如果返回404,去tomcat/logs 目錄下,查看一下log,查找原因。