我想在Ubuntu环境中的本地主机上安装SSL证书,因为我无法直接在生产服务器上工作。我必须根据页面是HTTP还是HTTPS来为我的代码添加一些条件。
我该怎么做?
答案 0 :(得分:77)
键入以下命令启用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的虚拟主机,我们需要启用它。
sudo a2ensite default-ssl.conf
然后我们需要重新启动Apache以加载我们的新虚拟主机文件:
sudo service apache2 restart
现在用https来运行你的网站!! !!
答案 1 :(得分:0)
不要在生产环境中使用它,而是让我们在生产模式下进行加密。
openssl req -x509 -nodes -new -sha256 -days 1024 -newkey rsa:2048 -keyout localhost.key -out localhost.pem -subj "/C=US/CN=localhost"
openssl x509 -outform pem -in localhost.pem -out localhost.crt
它将输出三个文件localhost.key
,localhost.pem
和localhost.crt