阿里云ECS服务器网站配置HTTPS连接

文章目录

环境

  • 阿里云ECS服务器:CentOS 8.5 64位 UEFI版
  • Apache 2.4.37

背景

域名 chinatt.org.cn 已经绑定到了一个阿里云ECS服务器上,可以通过 http://chinatt.org.cn 来访问网站。

出于安全性的考虑,现在希望添加https支持,应该怎么弄?

步骤

获取SSL证书

有多种方式可以获取SSL证书,本例中,使用的是阿里云的免费证书。其有效期是3个月,到期后可以再次申请。

参考这篇文档: https://developer.aliyun.com/article/1650540

首先登录阿里云数字证书管理服务页面: https://yundun.console.aliyun.com/?spm=a2c6h.12873639.article-detail.5.392262cdIPJYNj&userCode=r3yteowb&p=cas#/overview/cn-hangzhou

按照官方文档指引,在页面左边选择 SSL证书管理 ,然后在右边选择 个人测试证书(原免费证书) ,然后点击下方的 立即购买 按钮:

在接下来的页面里,无需修改任何选项,确保选择的是 个人测试证书(免费版) ,然后勾选"我已阅读并同意",并点击"立即购买"按钮:

接下来,回到SSL证书管理页面,点击 创建证书 按钮:

在右边选择 个人测试证书(免费版) ,并输入域名名称,然后选中 快捷签发 选项:

把联系人和所在地添加一下,其它配置我用的都是默认设置。

DNS验证

提交审核后,在SSL证书管理页面,找到证书,并点击右侧的 验证

在接下来的页面里会有 TXT/CNAME 类型的 主机记录记录值

在域名管理控制台(可能不是阿里云的域名,图略),配置好主机记录和记录值(生效可能需要几分钟时间),然后回到该页面,点击 验证 按钮,确保验证成功。

验证成功后,就可以下载证书了。

在证书右侧,点击 更多

然后在接下来的页面里,点击 下载 页签,然后找到适合的服务器类型,点击 下载

我用的是Apache,所以下载的是Apache类型的证书。

实际下载的是一个名为 20541636_chinatt.org.cn_apache.zip 的文件,将其解压后,生成3个文件:

  • chinatt.org.cn.key :私钥文件
  • chinatt.org.cn_public.crt :域名证书文件
  • chinatt.org.cn_chain.crt :证书链文件

Apache配置

把上述3个文件上传到ECS服务器的 /etc/httpd/ssl 目录(若不存在该目录则手工创建),

接下来,检查配置文件中是否已加载SSL模块。我在Apache的配置里( /etc/httpd/conf/httpd.conf/etc/httpd/conf.d/*.conf )没找到SSL相关的配置,所以,得先安装 mod_ssl 包:

bash 复制代码
sudo yum install mod_ssl

安装完成后,就会自动生成 /etc/httpd/conf.d/ssl.conf 文件。

备份该文件(备份时注意后缀名要修改一下,比如叫 ssl.conf.bak ),然后修改文件如下:

powershell 复制代码
Listen 443 https

SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog

SSLSessionCache         shmcb:/run/httpd/sslcache(512000)
SSLSessionCacheTimeout  300

SSLCryptoDevice builtin

<VirtualHost *:443>
    ServerAdmin webmaster@localhost
    DocumentRoot "/var/www/html/match_export/frontend"
    ServerName chinatt.org.cn
    ServerAlias www.chinatt.org.cn

    # SSL 基本配置
    SSLEngine on
    SSLCertificateFile /etc/httpd/ssl/chinatt.org.cn_public.crt
    SSLCertificateKeyFile /etc/httpd/ssl/chinatt.org.cn.key
    SSLCertificateChainFile /etc/httpd/ssl/chinatt.org.cn_chain.crt

    # SSL 安全优化
    SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
    SSLHonorCipherOrder on

    # 前端静态文件目录配置
    <Directory "/var/www/html/match_export/frontend">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
        DirectoryIndex index.html
    </Directory>

    # API路由配置:将所有/api/请求转发到ThinkPHP后端
    Alias /api "/var/www/html/match_export/backend/public"
    <Directory "/var/www/html/match_export/backend/public">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
        DirectoryIndex index.php

        # ThinkPHP重写规则
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
    </Directory>

    # 错误日志和访问日志路径
    ErrorLog /var/log/httpd/match_export_ssl_error.log
    CustomLog /var/log/httpd/match_export_ssl_access.log combined

    # PHP处理配置(与HTTP版本保持一致)
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
    </FilesMatch>
</VirtualHost>

注:原来有很多注释,我都删除了。

注意: ssl.conf 文件内容一定要好好看一下,里面有一部分内容照搬就行,有一部分内容是需要根据实际项目情况而定的。

修改完成后,使用 sudo httpd -t 检查一下,如果出现 Syntax OK 的字样,就说明没有语法问题。

最后,别忘了重启Apache:

bash 复制代码
sudo systemctl restart httpd

注意:HTTPS默认使用的端口是443,而ECS服务器的443端口默认是没有开放的,需要开放一下。

登录到阿里云ECS控制台,点击实例,选择 安全组 ,点击 管理规则

在接下来的页面里,点击 增加规则 按钮,并选择 HTTP(443) ,如下图:

注:如果不这么做,而是直接在服务器上通过 iptables 命令来打开443端口,是否也行呢?我不是很确定。

bash 复制代码
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

测试一下443端口的连通性:在PowerShell里,运行:

powershell 复制代码
PS C:\Users\duke_> ncat -zv 47.93.146.192 443
Ncat: Version 7.98 ( https://nmap.org/ncat )
Ncat: Connected to 47.93.146.192:443.
Ncat: 0 bytes sent, 0 bytes received in 0.17 seconds.

注: ncat 是用 choco install nmap 命令安装的(需要以管理员身份运行PowerShell)。

测试HTTPS访问

一切OK,就可以测试效果了。

打开浏览器,访问 https://chinatt.org.cn

可见,URL栏左侧有个锁的图标,表示这是SSL安全连接。

注:URL栏显示 chinatt.org.cn/login.html ,是因为网站的默认页面是 index.html ,然后因为没有登录,所以自动跳转到了 login.html 页面。这是网站的逻辑,和SSL以及Apache无关。

点击小锁,然后点击 安全连接

如下,显示 您已安全地连接至此网站

点击 更多信息 ,如下:

设置HTTP重定向到HTTPS

现在,使用HTTP和HTTPS都能访问网站,为了安全起见,我们可以强制使用HTTPS,把HTTP请求重定向到HTTPS。

之前是用 /etc/httpd/conf.d/match_export.conf 文件配置的。内容如下:

powershell 复制代码
<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot "/var/www/html/match_export/frontend"

    ServerName 47.93.146.192

    <Directory "/var/www/html/match_export/frontend">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
        DirectoryIndex index.html
    </Directory>

    # API路由配置:将所有/api/请求转发到ThinkPHP后端
    Alias /api "/var/www/html/match_export/backend/public"
    <Directory "/var/www/html/match_export/backend/public">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
        DirectoryIndex index.php

        # ThinkPHP重写规则
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
    </Directory>

    # 错误日志和访问日志路径,方便排查问题
    ErrorLog /var/log/httpd/match_export_error.log
    CustomLog /var/log/httpd/match_export_access.log combined

    # 确保Apache能正确处理PHP文件(如果全局已配置则可省略)
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
        # SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>
</VirtualHost>

备份该文件(注意备份时换个后缀名),然后修改文件如下:

powershell 复制代码
<VirtualHost *:80>
    ServerName chinatt.org.cn
    ServerAlias www.chinatt.org.cn
    # 永久重定向到HTTPS
    Redirect permanent / https://chinatt.org.cn/

</VirtualHost>

<VirtualHost 47.93.146.192:80>
    ServerAdmin webmaster@localhost

    DocumentRoot "/var/www/html/match_export/frontend"

    <Directory "/var/www/html/match_export/frontend">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
        DirectoryIndex index.html
    </Directory>

    # API路由配置:将所有/api/请求转发到ThinkPHP后端
    Alias /api "/var/www/html/match_export/backend/public"
    <Directory "/var/www/html/match_export/backend/public">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
        DirectoryIndex index.php

        # ThinkPHP重写规则
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
    </Directory>

    # 错误日志和访问日志路径,方便排查问题
    ErrorLog /var/log/httpd/match_export_error.log
    CustomLog /var/log/httpd/match_export_access.log combined

    # 确保Apache能正确处理PHP文件(如果全局已配置则可省略)
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
        # SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>
</VirtualHost>

请自行比较这两个文件。

修改完成后,使用 httpd -t 检查配置,确保没有语法错误。

最后,重启Apache服务:

bash 复制代码
systemctl restart httpd

现在,访问 http://chinatt.org.cn ,就会自动跳转到 https://chinatt.org.cn/login.html

参考

  • https://developer.aliyun.com/article/1650540
相关推荐
科技峰行者2 小时前
阿里云瓴羊发布企业级AI智能体服务平台AgentOne,成就AI时代的“超级公司”
人工智能·阿里云·ai·云计算·羚羊
zhuwei_clark2 小时前
Spring AI(七)Spring AI 的RAG实现集合火山向量模型+阿里云Tair(企业版)
人工智能·阿里云·云计算
王源骏2 小时前
Unity实现网页端 打开本地文件选择图片上传到阿里云的OSS
unity·阿里云·游戏引擎
博睿谷IT99_3 小时前
Linux 备份与恢复常用命令
java·linux·服务器
wuk9983 小时前
在Ubuntu 18.04/20.04 LTS设置静态DNS服务器
linux·服务器·ubuntu
“愿你如星辰如月”3 小时前
Linux C缓冲区机制全解析
linux·运维·服务器·c语言·vscode
2401_865854884 小时前
搭建个人博客:云服务器IP如何使用
运维·服务器·tcp/ip
探云抛雾؁ۣۖ4 小时前
SSH安全 白名单配置限制实战:AllowUsers 限制指定 IP 登录
linux·运维·服务器
饶了我吧,放了我吧5 小时前
数据通信与计算机网络—有线局域网:以太网
运维·服务器·网络