how do you enable ssl using laravel 8 sail
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Enabling SSL with Laravel 8 Sail: A Comprehensive Guide
Setting up an SSL certificate for your local development environment can be challenging, especially when using Docker and Laravel Sail. However, it is possible to achieve a secure connection between your localhost and remote server using various methods. In this blog post, we will explore the options available and provide step-by-step instructions on how to enable SSL for your Laravel 8 project powered by Laravel Sail.
Option 1: Self‑Signed Certificate with Docker
A self‑signed certificate is a valid but untrusted SSL certificate that you generate yourself. To create one, you can use tools like OpenSSL or Mkcert. First, add the following code to your docker-compose.yml file:
services:
webserver:
image: nginx:stable-alpine
ports:
- "80:80"
- "443:443" # Add this line to map port 443 on your localhost to the docker container's port 443
volumes:
- ./src:/var/www
- ./ssl:/etc/ssl
- ./nginx.conf:/etc/nginx/conf.d/default.conf # Add this line if you have a custom nginx.conf file
links:
- php:8.1-fpm
- mysql:MySQLServer
Next, run the following commands to generate self‑signed certificates:
openssl req -new -x509 -nodes -days 365 -keyout private.key -out certificate.crt -subj '/CN=local.dev.domain.com'cp private.key ./ssl/private_local.dev.domain.com.keycat ./ssl/certificate.crt ./ssl/private_local.dev.domain.com.key > ./ssl/combined.crt
Update the following lines in your nginx.conf file:
server {
listen 80; # HTTP traffic on port 80
listen [::]:80; # IPv6 HTTP traffic on port 80
listen 443 ssl http2; # SSL (HTTPS) traffic on port 443
listen [::]:443 ssl http2; # IPv6 SSL (HTTPS) traffic on port 443
server_name local.dev.domain.com;
root /var/www;
index index.php;
include snippets/ssl-selfsigned.conf;
}
Finally, update the following lines in your .env file:
APP_URL=https://local.dev.domain.com
APP_ENV=local
APP_DEBUG=true # if you want to enable debugging mode
Run sail up -d, open your browser, and navigate to https://localhost/. If all goes well, you should see Laravel's welcome screen. Be aware that self‑signed certificates are not secure for production environments as they can cause potential security risks and warnings in the browser.
Option 2: Let's Encrypt SSL Certificate with Docker
Using Let's Encrypt, you can obtain a free and trusted SSL certificate. To generate one, add the following code to your docker-compose.yml:
services:
webserver:
image: nginx:stable-alpine
ports:
- "80:80"
- "443:443" # Map port 443 on your localhost to the docker container's port 443
volumes:
- ./src:/var/www
- ./ssl:/etc/letsencrypt
- ./nginx.conf:/etc/nginx/conf.d/default.conf # Add this line if you have a custom nginx.conf file
links:
- php:8.1-fpm
- mysql:MySQLServer
Run the following commands to generate and setup Let's Encrypt certificates:
docker-compose run --service-ports webserver bashletsencrypt-auto --renew-by-default -a standalone -d local.dev.domain.com
To renew the certificates every 90 days, run docker-compose run --service-ports webserver bash && letsencrypt-auto renew. Now, update the relevant lines in your nginx.conf and .env files as described earlier.
Conclusion
By following these steps, you can successfully set up SSL for your Laravel 8 project using Laravel Sail with either a self‑signed certificate or a trusted Let's Encrypt SSL certificate. Remember to keep your security in mind and use the appropriate option depending on your needs. Always monitor your setup and adjust accordingly if any issues arise.