title: Need Help Setting up HTTPS on WSL2
body: <p>I really need some help configuring WSL to work using HTTPS for local Apache sites. I've spent 2 days trying to get this to work. I've used the following articles for reference (among other similar ones):</p>
  <p><a href="https://jitheshkt.medium.com/enable-ssl-on-wsl2-apache-windows-10-bcdfef71024a" rel="nofollow noreferrer">https://jitheshkt.medium.com/enable-ssl-on-wsl2-apache-windows-10-bcdfef71024a</a></p>
  <p><a href="https://gist.github.com/dennisameling/8317b9dc6b7d971860a4797c64f16eaf" rel="nofollow noreferrer">https://gist.github.com/dennisameling/8317b9dc6b7d971860a4797c64f16eaf</a> (the Prepare SSL Certificate section)</p>
  <p>mkcert is installed on both Windows and WSL2</p>
  <p>I ran the following in Power Shell (Admin) to create the certs in C:\Users\YOUR_WINDOWS_USERNAME\AppData\Local\mkcert:</p>
  <pre><code>mkcert -install
  mkcert localhost 127.0.0.1 ::1 0.0.0.0
  </code></pre>
  <p>That seems to have worked as I can see the certificates in the folder.</p>
  <p>However, the following did not work for me when run in Power Shell:</p>
  <pre><code>setx CAROOT “$(mkcert -CAROOT)”; If ($Env:WSLENV -notlike “*CAROOT*”) { setx WSLENV “CAROOT/up:$Env:WSLENV” }
  </code></pre>
  <p>So instead I did this in the WSL terminal:</p>
  <pre><code>echo 'export CAROOT=/mnt/c/Users/YOUR_WINDOWS_USERNAME/AppData/Local/mkcert' &gt;&gt; ~/.profile
  </code></pre>
  <p>To make it so the WSL mkcert would use the certificates on the Windows side, and it seemed to work, as now if I enter the following terminal command:</p>
  <pre><code>mkcert -CAROOT
  </code></pre>
  <p>I get:</p>
  <pre><code>/mnt/c/Users/YOUR_WINDOWS_USERNAME/AppData/Local/mkcert
  </code></pre>
  <p>But there are some things not working and that I'm definitely not understanding.</p>
  <p>For example, after doing just the above, now https://localhost works (I had to click to accept risk of untrusted certificate). I don't understand why this is working when I had not yet modified the default-ssl.conf file. Here are the contents of the file:</p>
  <pre><code>&lt;IfModule mod_ssl.c&gt;
      &lt;VirtualHost _default_:443&gt;
          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
          
      SSLOptions +StdEnvVars
          &lt;/FilesMatch&gt;
          &lt;Directory /usr/lib/cgi-bin&gt;
                  SSLOptions +StdEnvVars
          &lt;/Directory&gt;
  
      &lt;/VirtualHost&gt;
  &lt;/IfModule&gt;
  </code></pre>
  <p>With that, it's loading the default Apache page. But if I try to change the DocumentRoot to the location where the files for the site are stored on the Windows side:</p>
  <pre><code>DocumentRoot /mnt/c/YOUR_WINDOWS_USERNAME/dev/www/example.com/public_html
  </code></pre>
  <p>If I load https://localhost in the browser, I get &quot;Connection failed: No such file or directory&quot; (there is both an index.php and index.html file inside this folder just to be sure).</p>
  <p>Any idea why it is unable to load the files from that DocumentRoot location?</p>
  <p>It does not help if I also change the following as well to the correct locations of the certificate and key:</p>
  <pre><code>SSLCertificateFile     /mnt/c/Users/YOUR_WINDOWS_USERNAME/AppData/Local/mkcert/localhost+3.pem
  SSLCertificateKeyFile  /mnt/c/Users/YOUR_WINDOWS_USERNAME/AppData/Local/mkcert/localhost+3-key.pem
  </code></pre>
  <p>When I restart Apache, it loads fine, so I know I'm entering the directory paths correctly and to folders that exist.</p>
  <p>But my real goal is to be able to use the HTTPS versions of websites with custom domains, like <a href="https://example.com" rel="nofollow noreferrer">https://example.com</a>.</p>
  <p>I ran the following to create my certificate and key files and confirmed they were created:</p>
  <pre><code>mkcert example.com
  </code></pre>
  <p>I have my Windows Hosts file configured:</p>
  <pre><code>127.0.0.1   example.com
  127.0.0.1   www.example.com
  </code></pre>
  <p>And the following example.com.conf is enabled:</p>
  <pre><code>&lt;VirtualHost *:80&gt;
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin webmaster@localhost
    DocumentRoot /mnt/c/Users/YOUR_WINDOWS_USERNAME/dev/www/example.com/public_html
    ErrorLog /mnt/c/Users/YOUR_WINDOWS_USERNAME/dev/www/example.com/logs/error.log
    CustomLog /mnt/c/Users/YOUR_WINDOWS_USERNAME/dev/www/example.com/logs/access.log combined
  
    &lt;Directory /mnt/c/Users/YOUR_WINDOWS_USERNAME/dev/www/example.com/public_html&gt;
      Require all granted
    &lt;/Directory&gt;
  &lt;/VirtualHost&gt;
  </code></pre>
  <p>And the following example.com.ssl.conf is enabled:</p>
  <pre><code>&lt;IfModule mod_ssl.c&gt;
    &lt;Directory /mnt/c/Users/YOUR_WINDOWS_USERNAME/dev/www/example.com/public_html&gt;
      Require all granted
      DirectoryIndex index.php index.html
    &lt;/Directory&gt;
    &lt;VirtualHost example.com:443&gt;
      SSLEngine on
      SSLCertificateFile    /mnt/c/Users/YOUR_WINDOWS_USERNAME/AppData/Local/mkcert/example.com.pem
      SSLCertificateKeyFile /mnt/c/Users/YOUR_WINDOWS_USERNAME/AppData/Local/mkcert/example.com-key.pem
      ServerName example.com
      ServerAlias www.example.com
  
      ServerAdmin webmaster@localhost
      DocumentRoot /mnt/c/Users/YOUR_WINDOWS_USERNAME/dev/www/example.com/public_html
  
      ErrorLog /mnt/c/Users/YOUR_WINDOWS_USERNAME/dev/www/example.com/logs/error.log
      CustomLog /mnt/c/Users/YOUR_WINDOWS_USERNAME/dev/www/example.com/logs/access.log combined
  
      &lt;FilesMatch &quot;\.(cgi|shtml|phtml|php)$&quot;&gt;
          SSLOptions +StdEnvVars
      &lt;/FilesMatch&gt;
      &lt;Directory /usr/lib/cgi-bin&gt;
          SSLOptions +StdEnvVars
      &lt;/Directory&gt;
      &lt;/VirtualHost&gt;
  &lt;/IfModule&gt;
  </code></pre>
  <p>And if I try to load example.com or <a href="https://example.com" rel="nofollow noreferrer">https://example.com</a> in the browser, in Chrome I get the &quot;This site can't be reached / ERR_CONNECTION_REFUSED&quot; error and in Firefox it's the &quot;Unable to connect&quot; error.</p>
  <p>Any help that anyone could offer on this would be really appreciated. I'm honestly about to pull out my non-existent hair (I'm bald) and then put my head through my monitor at this point.</p>
  <p>Note for the file paths below: YOUR_WINDOWS_USERNAME is just a placeholder I'm using and I've entered my correct windows username in the paths on my end.</p>
  <p>** UPDATE **</p>
  <p>Changing the hosts file to:</p>
  <pre><code>127.0.0.1   example.com
  127.0.0.1   www.example.com
  
  ::1         example.com
  ::1         www.example.com
  </code></pre>
  <p>and changing the DocumentRoot (and all other file paths) to a location on the WSL side of things:</p>
  <pre><code>DocumentRoot /home/LINUX_USERNAME/dev/www/example.com/public_html
  </code></pre>
  <p>Has allowed the Apache2/Ubuntu Default page to load. But it's not loading the actual content of the index.php file in the public_html folder. If I view <a href="https://example.com/index.php" rel="nofollow noreferrer">https://example.com/index.php</a> I get a Not Found error.</p>
  <p>Thanks for any help!</p>