下面以 CentOS 7 + LAMP(Apache + MariaDB/MySQL + PHP) 为例,介绍如何开启 HTTPS(SSL),步骤清晰、可直接照做。
systemctl status httpd
如未安装:
yum install -y httpd
systemctl start httpd
systemctl enable httpd
确保域名(如 example.com)已解析到服务器公网 IP。
yum install -y mod_ssl
安装完成后会自动生成:
/etc/httpd/conf.d/ssl.conf
yum install -y epel-release
yum install -y certbot python2-certbot-apache
certbot --apache -d example.com -d www.example.com
按提示操作:
✅ 自动配置 Apache,无需手动改配置文件
certbot renew --dry-run
Let’s Encrypt 证书 90 天有效期,系统会自动续期。
mkdir -p /etc/httpd/ssl
cd /etc/httpd/ssl
openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 \
-keyout server.key \
-out server.crt
填写信息示例:
Country Name: CN
State: Beijing
Organization Name: Test
Common Name: example.com
编辑:
vim /etc/httpd/conf.d/ssl.conf
修改关键配置:
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/httpd/ssl/server.crt
SSLCertificateKeyFile /etc/httpd/ssl/server.key
<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
确保 TCP 443 已放行。
systemctl restart httpd
编辑 HTTP 虚拟主机:
vim /etc/httpd/conf/httpd.conf
或新建:
vim /etc/httpd/conf.d/http-to-https.conf
内容:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
重启 Apache:
systemctl restart httpd
浏览器访问:
https://example.com
✅ 显示小锁即成功
journalctl -xe
httpd -t
如果你用的是 CentOS 8 / Rocky / Alma / 宝塔 / LNMP,可以告诉我,我可以给你对应版本的具体方案。