问题描述
我想在Ubuntu环境中的本地主机上安装SSL证书,因为我不能直接在生产服务器上工作。我必须根据页面是HTTP还是HTTPS在我的代码中加入一些条件。
我怎样才能做到这一点?
最佳解决方法
键入以下命令启用apache模块:
sudo a2enmod ssl
启用SSL后,您必须重新启动Web服务器才能识别更改:
sudo service apache2 restart
让我们首先在Apache的配置层次结构中创建一个子目录,以放置我们将要制作的证书文件:
sudo mkdir /etc/apache2/ssl
现在我们有了放置密钥和证书的位置,我们可以通过键入以下步骤创建它们:
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt
问题部分看起来像这样:
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:New York
Locality Name (eg, city) []:New York City
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Your Company
Organizational Unit Name (eg, section) []:Department of Kittens
Common Name (e.g. server FQDN or YOUR name) []:your_domain.com
Email Address []:your_email@domain.com
立即使用root权限打开文件:
sudo nano /etc/apache2/sites-available/default-ssl.conf
删除注释后,文件看起来像这样:
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
BrowserMatch "MSIE [2-6]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
</VirtualHost>
</IfModule>
最后,它看起来像这样。条目是从原始文件修改的:
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin admin@example.com
ServerName your_domain.com
ServerAlias www.your_domain.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /var/www/html>
SSLOptions +StdEnvVars
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
BrowserMatch "MSIE [2-6]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
</VirtualHost>
</IfModule>
完成后保存并退出文件。现在我们已经配置了SSL-enabled虚拟主机,我们需要启用它。
sudo a2ensite default-ssl.conf
然后我们需要重新启动Apache来加载我们的新虚拟主机文件:
sudo service apache2 restart
现在它用https运行你的网站.. !!