在 CentOS 上给 Apache 做“加密”,通常有两种常见含义,我分别说明,你可以按需选择:
目的:让网站通过 https:// 访问,数据传输加密
yum install httpd -y
systemctl start httpd
systemctl enable httpd
yum install mod_ssl -y
yum install epel-release -y
yum install certbot python2-certbot-apache -y
certbot --apache -d yourdomain.com
按提示操作,证书会自动配置
编辑:
vi /etc/httpd/conf.d/ssl.conf
关键配置:
SSLEngine on
SSLCertificateFile /path/to/cert.pem
SSLCertificateKeyFile /path/to/privkey.pem
SSLCertificateChainFile /path/to/chain.pem
重启 Apache:
systemctl restart httpd
目的:访问目录时需要账号密码
htpasswd -c /etc/httpd/.htpasswd user1
在网站目录创建 .htaccess:
AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user
编辑 Apache 配置:
<Directory "/var/www/html">
AllowOverride All
</Directory>
重启:
systemctl restart httpd
<VirtualHost *:80>
ServerName yourdomain.com
Redirect permanent / https://yourdomain.com/
</VirtualHost>
如果你能说明:
我可以给你更精准的配置示例。