发布于 

云服务器使用和服务部署(阿里云ECS+域名注册+SSL证书)

云服务器购买

云服务器购买

这部分参考博客: 记录第一次Vue项目部署到阿里云Linux服务器上线的艰难历程

借此文章记录一下自己的踩坑问题。

域名申请

域名申请

参考博客: 如何申请域名、购买证书并在Nginx服务器上实现HTTPS?

域名在阿里上买的,xyz后缀域名,首年6块钱(挺便宜)
之后需要走实名认证,很快,
但比较坑的是阿里实名认证完还得入库工信部系统,这得花2-3天的时间,
如果工信部系统没有入库域名,域名网站就无法备案,

没有备案的网站在网上挂一段时间就会被限制:

有了域名之后就可以将域名和解析记录联系在一起,通过域名直接访问到指定ip地址:

Nginx

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
配置文件 nginx.conf

这里我在80/8091/8092三个端口上配了服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Home
server {
listen 80;
listen [::]:80;
server_name www.e-duck.xyz;
location /{
root /var/myapp/home/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

# ThreeJs
server {
listen 8091;
server_name www.e-duck.xyz;

location /{
root /var/myapp/three/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
}
# Lottery
server {
listen 8092;
server_name www.e-duck.xyz;

location /{
root /var/myapp/lottery/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
}

就是非常普通的配置,但是实际上,一开始我觉得后面的端口号不好看,
想用路由映射的方式映射到不同的服务上,
似乎用proxy_pass或者alias都能实现,

proxy_pass配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server {
listen 80;
server_name www.e-duck.xyz;
location /{
root /var/myapp/home/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
location /lottery/{
proxy_pass: www.e-duck.xyz:8091;
}
}
server {
listen 8091;
server_name www.e-duck.xyz;

location /{
root /var/myapp/three/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
}
alias配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80;
server_name www.e-duck.xyz;
location /{
root /var/myapp/home/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
location /lottery/{
root /var/myapp/three/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
}

但是实际这样配置的时候却发现,静态文件无法加载出来,
打开网页控制台报错:

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
2
3
4
5
6
7
8
server{
listen 8008;
server_name localhost;
location / {
root /root/pic_lib;
autoindex on;
}
}

端口配置

端口配置

参考博客: 阿里云服务器开放端口的正确方式

需要外界访问的端口服务需要再云服务器中进行开放,
这个可以在阿里云服务器上配置,
也可以通过命令行配置:

  • 开放指定端口

    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
配置Java

参考博客: 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配置

流程简述
  1. 配置ACME
  2. 使用ACME安装证书
  3. nginx配置ssl服务

配置ACME

参考内容:

acme.sh的安装

安装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

安装证书

使用acme.sh安装证书

一共有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
2
3
4
5
acme.sh --install-cert -d example.com \
--cert-file /path/to/certfile/in/apache/cert.pem \
--key-file /path/to/keyfile/in/apache/key.pem \
--fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \
--reloadcmd "service apache2 force-reload"

Nginx

1
2
3
4
acme.sh --install-cert -d example.com \
--key-file /path/to/keyfile/in/nginx/key.pem \
--fullchain-file /path/to/fullchain/nginx/cert.pem \
--reloadcmd "service nginx force-reload"

检查已安装证书信息:

1
acme.sh --info -d example.com

Nginx配置Https

参考博客

检查nginx是否存在ssl模块:

1
2
cd /usr/sbin
./nginx -V

如果configure arguments存在–with-https_ssl_module,说明存在ssl模块,
可以直接进行配置
注意要检查服务器的443端口是否开放

配置模版
配置模版
将80端口重定向到443端口
将80端口重定向到443端口