總算是弄出來了,先寫下來供自己以后查閱。
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文件地址 root@ip:/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 server { 2 listen 443 ssl; 3 server_name www.foreignkey.top; 4 ssl on; 5 ssl_certificate cert/cert.pem; 6 ssl_certificate_key cert/cert.key; 7 8 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; 9 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 10 ssl_prefer_server_ciphers on; 11 ssl_session_cache shared:SSL:1m; 12 ssl_session_timeout 5m; 13 14 location /images/{ 15 root /usr/; 16 autoindex on; 17 } 18 19 location /app { 20 root html; 21 index index.html index.htm; 22 proxy_pass http://112.74.188.59:7777; 23 } 24 } 25 26 server { 27 listen 80; 28 server_name www.foreignkey.top; 29 30 rewrite ^(.*)$ https://$host$1 permanent; 31 }
---------------------------------------
大功告成。不出意外就可以訪問到你的網站了。寫得不好,時間比較趕,明天還上班,砥礪共勉。
Http的訪問:
Https的訪問: