在 CentOS 上给 Apache 做“加密”,通常分为 两种场景,最常见的是 HTTPS(SSL/TLS 加密),也可能是 目录/文件访问加密(密码保护)。我分别说明:
yum install httpd mod_ssl openssl -y
openssl req -new -x509 -days 365 -nodes \
-keyout /etc/pki/tls/private/apache.key \
-out /etc/pki/tls/certs/apache.crt
按提示填写信息即可。
如果是生产环境,建议使用 Let’s Encrypt 免费证书
编辑或新建:
vi /etc/httpd/conf.d/ssl.conf
确保关键配置:
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/apache.crt
SSLCertificateKeyFile /etc/pki/tls/private/apache.key
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
systemctl restart httpd
访问:
https://你的服务器IP
htpasswd -c /etc/httpd/.htpasswd user1
<Directory "/var/www/html/secret">
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user
</Directory>
systemctl restart httpd
yum install epel-release -y
yum install certbot python2-certbot-apache -y
certbot --apache
自动配置 HTTPS 并续期。
我可以给你 精确配置文件示例。