spring boot 項目部署到阿里云ECS服務器小結

我采用的是最傳統(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.如圖

用MySQLWorkbench遠程連接mysql 填寫服務器公網(wǎng)IP,mysql 用戶名 root,和密碼 測試連

然后修改項目數(shù)據(jù)庫地址
spring.datasource.url=jdbc:mysql://*.*.*.*:3306/databasename?autoReconnect=true&useSSL=false
運行項目,如果沒有問題,就進行下一步打包。

2. 項目打包

spring boot 打war包?;静襟E

  1. 修改pom文件,jar 中jar改成war。

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

  1. 修改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;
}
  1. 這個時候如果直接點擊運行按鈕的話,會出錯,這是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,查找原因。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容