如何在Ubuntu的localhost上安装SSL? [英] How can I install SSL on localhost in Ubuntu?
问题描述
我想在Ubuntu环境中的本地主机上安装SSL证书,因为我不能直接在生产服务器上工作.我必须根据页面是HTTP还是HTTPS在代码中添加一些条件.
I want to install an SSL certificate on my localhost in Ubuntu environment because I can't work on the production server directly. I have to put some conditions in my code on the basis of whether the page is HTTP or HTTPS.
我该怎么做?
推荐答案
通过键入以下内容启用apache模块:
Enable the apache module by typing:
sudo a2enmod ssl
启用SSL后,必须重新启动Web服务器才能识别更改:
After you have enabled SSL, you'll have to restart the web server for the change to be recognized:
sudo service apache2 restart
让我们首先在Apache的配置层次结构中创建一个子目录来放置我们将要制作的证书文件:
Let's start off by creating a subdirectory within Apache's configuration hierarchy to place the certificate files that we will be making:
sudo mkdir /etc/apache2/ssl
现在我们可以放置密钥和证书了,我们可以通过键入以下步骤一步一步地创建它们:
Now that we have a location to place our key and certificate, we can create them both in one step by typing:
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt
问题部分看起来像这样:
The questions portion looks something like this:
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权限打开文件:
Open the file with root privileges now:
sudo nano /etc/apache2/sites-available/default-ssl.conf
删除注释后,文件看起来像这样:
With the comments removed, the file looks something like this:
<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>
最后,它将看起来像这样.这些条目是从原始文件中修改的:
In the end, it will look something like this. The entries were modified from the original file:
<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的虚拟主机,我们需要启用它.
Save and exit the file when you are finished. Now that we have configured our SSL-enabled virtual host, we need to enable it.
sudo a2ensite default-ssl.conf
然后,我们需要重新启动Apache来加载新的虚拟主机文件:
We then need to restart Apache to load our new virtual host file:
sudo service apache2 restart
就是这样,现在使用https .. !!!! p来运行您的网站.
That's it now run your site with https..!!
这篇关于如何在Ubuntu的localhost上安装SSL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!