云服务器使用和服务部署(阿里云ECS+域名注册+SSL证书)
云服务器购买
这部分参考博客: 记录第一次Vue项目部署到阿里云Linux服务器上线的艰难历程
借此文章记录一下自己的踩坑问题。
域名申请
参考博客: 如何申请域名、购买证书并在Nginx服务器上实现HTTPS?
域名在阿里上买的,xyz后缀域名,首年6块钱(挺便宜)
之后需要走实名认证,很快,
但比较坑的是阿里实名认证完还得入库工信部系统,这得花2-3天的时间,
如果工信部系统没有入库域名,域名网站就无法备案,
没有备案的网站在网上挂一段时间就会被限制:
Nginx
Nginx基础配置
配置Nginx大多数时候都是以下2个步骤:
配置nginx.conf文档
1
vim /etc/nginx/nginx.conf
重启nginx
1
systemctl restart nginx
其他命令用的比较多的也就是下面这些:
启动nginx
1
systemctl start nginx
结束nginx
1
systemctl end nginx
查看所有开启的nginx服务进程
1
ps -ef | grep nginx
杀死所有nginx进程
1
killall nginx
检查nginx.conf配置文件是否有语法错误
1
nginx -t
这里我在80/8091/8092三个端口上配了服务:
1 | Home |
就是非常普通的配置,但是实际上,一开始我觉得后面的端口号不好看,
想用路由映射的方式映射到不同的服务上,
似乎用proxy_pass或者alias都能实现,
proxy_pass配置
1 | server { |
alias配置
1 | server { |
但是实际这样配置的时候却发现,静态文件无法加载出来,
打开网页控制台报错:
1 | Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. |
这个问题我找了很久的解决方案,反正现在还是没有解决,
所以干脆就恢复了端口的写法。
Nginx搭建静态资源服务器
添加虚拟主机时,将root映射到本机存储资源的目录下,
开启浏览目录权限:
1 | server{ |
端口配置
参考博客: 阿里云服务器开放端口的正确方式
需要外界访问的端口服务需要再云服务器中进行开放,
这个可以在阿里云服务器上配置,
也可以通过命令行配置:
开放指定端口
1
firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙
1
systemctl restart firewalld.service
Tomcat
- 操作系统 CentOS 7.9 64位
- Java 1.7.0
- tomcat 8.5.99
参考博客: CentOS 7 安装和配置java环境 | tomcat启动“成功”,但是浏览器无法访问
需要注意的是TomCat的版本和Java的版本一定要匹配,
否则tomcat运行不起来,
我是报缺包错误:
1 | Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/catalina/startup/Bootstrap |
检测Tomcat是否开启的方式:
1 | ps -ef | grep tomcat |
SSL配置
- 配置ACME
- 使用ACME安装证书
- nginx配置ssl服务
配置ACME
- ACME:自动证书管理环境,是证书的签发工具
- CA:证书颁发机构
参考内容:
安装acme.ch,顺便注册邮箱
1 | curl https://get.acme.sh | sh -s email=my@example.com |
安装到root目录下:
1 | ~/.acme.sh/ |
在root目录下创建一个.bashrc:
1 | vim .bashrc |
在bash中配置一个shell的alias:
1 | alias acme.sh=~/.acme.sh/acme.sh |
这样使用acme.sh命令更加方便:
1 | acme.sh -h |
安装证书
一共有2种生成证书的方式:
- http
- dns
这里使用的是http方式生成证书
需要将ca服务器修改为letsencrypt,默认是zerossl:
1 | acme.sh --set-default-ca --server letsencrypt |
指定域名和网站根目录,就可以生成证书了:
1 | acme.sh --issue -d mydomain.com -d www.mydomain.com --webroot /home/wwwroot/mydomain.com/ |
指定证书的安装位置:
Apache
1 | acme.sh --install-cert -d example.com \ |
Nginx
1 | acme.sh --install-cert -d example.com \ |
检查已安装证书信息:
1 | acme.sh --info -d example.com |
Nginx配置Https
检查nginx是否存在ssl模块:
1 | cd /usr/sbin |
如果configure arguments存在–with-https_ssl_module,说明存在ssl模块,
可以直接进行配置
注意要检查服务器的443端口是否开放