欢迎光临
我们一直在努力

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问


总算是弄出来了,先写下来供自己以后查阅。

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目录下,

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

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

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

2)如果没有配置的话,点“添加域名”,把你的域名添加进去就可以了。添加成功的话,就会看到下面的域名被配置好了。

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

3)点击“解析设置”就可以到看自己配置信息了。我里面的第一条是配置了ssl才有的。

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

4)顺带也把SSL证书一起认证了把。在第三步骤的位置“解析设置”右边有个“更多”有个SSL证书。选择免费的就行,可能你会一时间找不到这个免费的,页面有很多选项,你多点点就会出来的。

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

5)审核通过后,你就可以下载后面需要的nginx证书和tomcat证书。因为Springboot默认使用tomcat的。所以都先下载下来。

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

----------------------------------------------------

接下来先配置好自己的springboot2.0的项目

1)项目结构:打箭头的就是tomcat的证书,放在项目的根目录下。

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

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目录下。

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

5) 上传到阿里云服务器上,我放在了/home/foreign/jar下面。注意:这里也需要存一份tomcat证书。反正证书和项目在同一个文件夹下即可。我在mac上使用scp命令传到linux服务器。

scp 项目jar文件地址 root@ip:/home/foreign/jar

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

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下

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

2)创建cert文件夹 mkdir cert

3)把你之前下载的SSL证书拷贝到cert目录下。

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

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的访问:

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

 

 Https的访问:

Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问

 

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问》
文章链接:https://www.456zj.com/5221.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址