要在Ubuntu上使用Apache2实现HTTPS访问,您需要首先获取SSL证书,然后配置Apache2以使用该证书。以下是详细步骤:
sudo apt update
sudo apt install apache2
sudo a2enmod ssl
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt
按照提示输入相关信息,如国家、组织名称等。
sudo nano /etc/apache2/sites-available/yourdomain.com-le-ssl.conf
将yourdomain.com
替换为您的实际域名。
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster@yourdomain.com
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/yourdomain.com
SSLEngine on
SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
<Directory /var/www/yourdomain.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
</IfModule>
保存并关闭文件。
启用新的SSL虚拟主机配置:
sudo a2ensite yourdomain.com-le-ssl.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
现在,您的网站应该可以通过HTTPS访问了。请注意,由于我们使用的是自签名证书,浏览器可能会显示安全警告。在生产环境中,您应该使用Let’s Encrypt等免费证书颁发机构获取有效的SSL证书。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:Ubuntu Apache2如何限制访问