Mkcert on Laravel Homestead for trusted SSL certs

You can use an SSL certificate created with mkcert on Laravel Homestead. mkcert is a tool to create locally-trusted development certificates, and you can use it to create an SSL certificate for your Laravel Homestead environment.

Here’s how you can set up mkcert on Laravel Homestead:

1. Install mkcert (if you haven’t already)

Install mkcert on your local machine following the instructions from the official repository:

  • On macOS: brew install mkcert brew install nss # If you use Firefox
  • On Windows or Linux: Follow instructions from mkcert GitHub repository.

2. Create the SSL Certificate

Generate the SSL certificate for your Homestead site:

   mkcert example.test "*.example.test" 127.0.0.1 ::1

Replace example.test with the domain you are using for your Homestead project. The command generates a certificate for both example.test and its wildcard subdomains (*.example.test).

This will create two files:

  • example.test.pem (the certificate)
  • example.test-key.pem (the private key)

3. Move Certificates to Homestead

Copy the certificate files to your Laravel Homestead environment. I like to drop them in a folder inside my code and add the files to gitignore.

   cp example.test.pem ~/projects/my-cool-project/ssl-certs
   cp example.test-key.pem ~/projects/my-cool-project/ssl-certs

4. Update Homestead Nginx Configuration

Update your Homestead Nginx configuration to use the new certificate.
You can do this manually, or you can use the user-customizations.sh or after.sh script for homestead to have this automatically on boot of your VM
The Nginx configuration is typically found in /etc/nginx/sites-available.

the following sed commands will do it for you, please adjust the paths to suit

sed -i 's|ssl_certificate[[:space:]]*/etc/ssl/certs/homestead.test.crt|ssl_certificate /home/vagrant/code/ssl-certs/example.test.pem|' /etc/nginx/sites-available/homestead.test

sed -i 's|ssl_certificate_key[[:space:]]*/etc/ssl/certs/homestead.test.key|ssl_certificate_key /home/vagrant/code/ssl-certs/example.test-key.pem|' /etc/nginx/sites-available/homestead.test

5. Restart Nginx

After updating the Nginx configuration, restart Nginx to apply the changes [make sure to add this to your user-customizations.sh or after.sh script ]:

   sudo service nginx restart

6. Trust the Certificate on Your Local Machine

If you haven’t already, run the following command to trust the local root CA generated by mkcert:

   mkcert -install

This command ensures your local machine trusts the certificates generated by mkcert.

7. Update /etc/hosts

Make sure your hosts file contains the domain you’re using in Homestead (for example, example.test). I use gasmask for this

On your local machine, edit your /etc/hosts file:

   sudo nano /etc/hosts

Add the line:

   192.168.10.10 example.test

Replace 192.168.10.10 with the IP of your Homestead box (typically found in your Homestead.yaml file).

8. Access Your Site via HTTPS

Now you should be able to access your Laravel Homestead site using HTTPS:

   https://example.test

If everything is set up correctly, your browser should no longer show SSL warnings, and the SSL certificate should be trusted.

Summary:

  • Install mkcert and create SSL certificates.
  • Copy the certificates to your Homestead environment.
  • Update the Nginx configuration to use the new SSL certificates.
  • Trust the certificate on your local machine.
  • Restart Nginx and access your site via HTTPS.
  • You now have ssl certs from mkcert on Laravel Homestead

This setup will allow you to securely use SSL on your local Laravel Homestead development environment with an mkcert certificate.

Leave a Reply