Setup HTTPS on wildcard subdomain on localhost

2019-04-29 19:36发布

问题:

I have created a Laravel application on my local Ubuntu machine. But I want to use HTTPS on all of its wild card sub-domains. How can I do this locally? By the way, I have added my site URL on etc/hosts so I don't need to type localhost but instead www.mysite.loc.

Answer in this question How can I install SSL on localhost in Ubuntu?, I think will only work on the main domain.

回答1:

No, the answer with this question How can I install SSL on localhost in Ubuntu? is working fine. But you need to modify few lines of code in your conf file.

I manage to try it now and works fine but I got this irritating message from my browser that the site I am accessing is not secure. Though it's okay, since it is just self signed certificate.

In my /etc/hosts I added several sub domains for my local site since it will not work even you configured your virtual host properly because the site your developing is not yet accessible online.

Say, www.mydomain.com, hello.mydomain.com, world.mydomain.com

Now, we need to enable SSL module

sudo a2enmod ssl

Restart Apache

sudo service apache2 restart

Create a folder for Self-signed SSL Certificates

sudo mkdir /etc/apache2/ssl

Generate key and other stuffs for SSL

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt

Answer questions their and use your domain say mydomain.com as Common name

Now I edited the conf file of my virtual host which resides in /etc/apache2/sites-available/mydomain.com.conf

This is the what's inside

<IfModule mod_ssl.c>
<VirtualHost _default_:443>
    ServerAdmin admin@example.com
    ServerName www.mydomain.com
    ServerAlias mydomain.com *.mydomain.com
    DocumentRoot /home/me/projects/www/mysite_folder
    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 "/home/me/projects/www/mysite_folder">
       SSLOptions +StdEnvVars
       Order allow,deny
       Allow from all
       # New directive needed in Apache 2.4.3: 
       Require all granted
       AllowOverride 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>

If you already enable your virtual host, you need to skip this step. Else, type

sudo a2ensite mydomain.com.conf

Lastly, you need to restart Apache again by

sudo service apache2 restart

Hope it helps you! You can now access your site using https

Ex. https://www.mydomain.com, https://hello.mydomain.com, https://world.mydomain.com