在 CentOS 上,通常使用的是 Apache HTTP Server(httpd),而不是 Ubuntu 中的 apache2。下面以 CentOS 7/8 为例,说明如何开启 HTTPS。
# CentOS 7
yum install httpd mod_ssl -y
# CentOS 8
dnf install httpd mod_ssl -y
mod_ssl 是用于支持 HTTPS 的模块。
systemctl start httpd
systemctl enable httpd
mkdir -p /etc/httpd/ssl
openssl req -x509 -nodes -days 365 \
-newkey rsa:2048 \
-keyout /etc/httpd/ssl/server.key \
-out /etc/httpd/ssl/server.crt
将你的:
server.crtserver.keyca_bundle.crt放到:
/etc/httpd/ssl/
编辑:
vi /etc/httpd/conf.d/ssl.conf
关键配置示例:
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/httpd/ssl/server.crt
SSLCertificateKeyFile /etc/httpd/ssl/server.key
# SSLCertificateChainFile /etc/httpd/ssl/ca_bundle.crt
</VirtualHost>
# firewalld
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
httpd -t
systemctl restart httpd
浏览器访问:
https://你的服务器IP或域名
在 /etc/httpd/conf/httpd.conf 或站点配置中加入:
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
如果你用的是 CentOS Stream / Rocky / AlmaLinux,或想用 Let’s Encrypt 免费证书,我也可以继续帮你写。