Springboot2.0部署阿里雲伺服器(nginx+域名+SSL)供Http和Https訪問
從下午1點弄到晚上11點,花了10個小時,但是總算是弄出來了,先寫下來供自己以後查閱。
1)首先你要有一個阿里雲伺服器,我用的是Centos7學生認證,10元/月,很便宜也很好用。
2)購買了域名,首年9元,很划算。域名買來之後經歷了拍照備案,前前後後花了1個月的時間把,但是阿里雲給我多續了一個月的時間,真的很良心,謝謝咯。
3)先從安裝nginx開始把,新的伺服器安裝nginx可能需要安裝一些別的依賴,我的步驟是如下的。
1. yum install gcc-c++
2. yum install -y pcre-devel
3. yum install -y zlib-devel
4. yum install -y openssl openssl-revel
以上的步驟,你就朝著linux的黑框框敲就對了,別問為什麼,因為我就知道如果你直接安裝nginx的花,會報需要這些外掛。所以,就是這麼操作的。
4)下載nginx。至於怎麼下載,我就不說了,去nginx的官網自己找到你需要的版本,解壓。
5)進入你解壓完的nginx目錄下,
6)編譯,生成prefix的nginx安裝目錄等。 我這裡面引數帶有ssl模組。待會要用到。
./configure \ --with-http_ssl_module \ --prefix=/usr/local/nginx \--pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi
7)執行make命令,並生成makefile目錄
8)make install安裝nginx
9)如果安裝成功的話,在/usr/local下有個nginx目錄。
10)進入/usr/local/nginx/sbin目錄下,執行./nginx
11)ps aux | grep nginx 檢視是否正常啟動。如果啟動成功的話,瀏覽器訪問ip 就會看到歡迎頁面。nginx預設埠是80
12)停止nginx的命令: 在sbin目錄下 ./nginx -s stop
13)平滑重啟命令:在sbin目錄下./nginx -s reload
14) nginx可能安裝會有很多問題,要有耐心,就一定能安裝成功。
-------------------------------------------------------------
接下來先把你的域名解析到你的伺服器IP。
1)在阿里雲官網,登陸你自己的阿里雲賬戶,然後搜尋dns
2)如果沒有配置的話,點“新增域名”,把你的域名新增進去就可以了。新增成功的話,就會看到下面的域名被配置好了。
3)點選“解析設定”就可以到看自己配置資訊了。我裡面的第一條是配置了ssl才有的。
4)順帶也把SSL證書一起認證了把。在第三步驟的位置“解析設定”右邊有個“更多”有個SSL證書。選擇免費的就行,可能你會一時間找不到這個免費的,頁面有很多選項,你多點點就會出來的。
5)稽核通過後,你就可以下載後面需要的nginx證書和tomcat證書。因為Springboot預設使用tomcat的。所以都先下載下來。
----------------------------------------------------
接下來先配置好自己的springboot2.0的專案
1)專案結構:打箭頭的就是tomcat的證書,放在專案的根目錄下。
2) 配置你的springboot啟動類,新增解析https和http
1 package com.foreign.smallcode; 2 3 import org.apache.catalina.connector.Connector; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; 7 import org.springframework.boot.web.servlet.server.ServletWebServerFactory; 8 import org.springframework.context.annotation.Bean; 9 10 @SpringBootApplication 11 //@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class) 12 public class SmallCodeApplication { 13 14 public static void main(String[] args) { 15 SpringApplication.run(SmallCodeApplication.class, args); 16 } 17 18 // springboot2 寫法 19 @Bean 20 public ServletWebServerFactory servletContainer() { 21 TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(); 22 tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 新增http 23 return tomcat; 24 } 25 26 // 配置http 27 private Connector createStandardConnector() { 28 Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); 29 connector.setPort(7777); 30 return connector; 31 } 32 33 }
3)你的yml檔案需要配置ssl
server: port: 8888 ssl: enable: true key-store: 你自己的tomcat證書名字 key-store-password: 你下載下來tomcat證書裡的密碼
4) 使用maven的 package打包。打包完成的jar檔案在 專案的target目錄下。
5) 上傳到阿里雲伺服器上,我放在了/home/foreign/jar下面。注意:這裡也需要存一份tomcat證書。反正證書和專案在同一個資料夾下即可。我在mac上使用scp命令傳到linux伺服器。
scp 專案jar檔案地址 [email protected]:/home/foreign/jar
6)在專案目錄下,使用java -jar XXX.jar在前臺啟動你的springboot專案。
7)或者使用 nohup java -jar xxx.jar --server.port=8090 & 來後臺啟動你的專案,這樣你的命令列關閉了 也沒關係。
------------------------------------------
接下來就是配置你的nginx.conf檔案了,讓我們可以訪問到https和http的網站。
1)進入到你安裝完成的nginx目錄。我的在/usr/local/nginx/conf下
2)建立cert資料夾 mkdir cert
3)把你之前下載的SSL證書拷貝到cert目錄下。
4)修改conf下的nginx.conf檔案。配置你的域名。
1 # HTTPS server 2 # 3 server { 4 listen 443 ssl; 5 server_name 你的域名; 6 ssl on; 7 ssl_certificate cert/cert.pem; #你的證書 8 ssl_certificate_key cert/cert.key; #你的證書 9 10 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; 11 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 12 ssl_prefer_server_ciphers on; 13 ssl_session_cache shared:SSL:1m; 14 ssl_session_timeout 5m; 15 16 location / { 17 root html; 18 index index.html index.htm; 19 proxy_pass http://你的ip:7777; 20 } 21 } 22 23 server { 24 listen 80; 25 server_name 你的域名; 26 27 location /images/{ 28 root /usr/; 29 autoindex on; 30 } 31 location /app{ 32 proxy_pass http://你的ip:7777; 33 } 34 }
---------------------------------------
大功搞成。不出意外就可以訪問到你的網站了。寫得不好,時間比較趕,明天還上班,砥礪共勉。
Http的訪問:
Https的訪問: