How to use Certbot with Laradock on your Laravel project
How to Install a Free SSL Certificate with Certbot on your Laravel Project in Laradock
If you are hosting your Laravel application with Laradock, the popular Docker development stack, you can easily set up a free SSL certificate with Certbot. This will allow you to have a secure HTTPS connection, which is essential for modern websites.
Follow the steps below:
Prerequisites
Docker and Docker Compose installed on your server Your Laravel project is set up.
If you don't have Laradock yet installed
Follow the following instructions if you don't have Laradock yet installed.
1. Clone the Laradock project
In your Laravel project, clone Laradock from the official GitHub repo:
git clone https://github.com/Laradock/laradock.git
2. Copy Laradock .env file
In the laradock folder, copy paste the .env.example file to .env:
cp .env.example .env
3. Setup Mysql in the .env file
In your Laravel folder, open the .env file and change the MYSQL... values with the ones in the Laradock .env file.
You should get something like this :
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=default
DB_USERNAME=default
DB_PASSWORD=secret
4. Start the docker services
In your Laradock folder, start the Docker services you need with docker-compose up -d --build nginx mysql redis mailhog
5. Install the laravel project's dependencies
Install your Laravel project's dependencies by running the following commands :
docker-compose exec --user=laradock workspace composer install
docker-compose exec --user=laradock workspace php artisan key:generate
docker-compose exec --user=laradock workspace npm install
docker-compose exec --user=laradock workspace php artisan migrate
Setup Certbot in Laradock
Now you have your Laravel project running with Laradock, follow the following steps to install your Certbot certificate and make your website run on HTTPS.
1. Set your domain in Nginx
Edit the nginx/sites/default.conf
file and set the default "localhost" server_name value by your domain name.
2. Edit Certbot values in docker-compose.yml
2.a Edit CN and Email values
In docker-compose.yml
, in the Certbot section, configure the CN and EMAIL values.
Replace the CN value with your domain name and the email value with your email address.
WARNING : Don't put the doubles quotes around the value.
Example :
2.b Change Certbot and Nginx volumes
In the Cerbot section, replace the values in volumes by the 3 following lines :
- ${DATA_PATH_HOST}/certbot/certs/:/var/certs
- ${DATA_PATH_HOST}/letsencrypt/:/etc/letsencrypt
- ./certbot/letsencrypt/:/var/www/letsencrypt
In the Nginx section, replace the volumes values by the 3 following lines :
- ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
- ${NGINX_HOST_LOG_PATH}:/var/log/nginx
- ${NGINX_SITES_PATH}:/etc/nginx/sites-available
- ${NGINX_SSL_PATH}:/etc/nginx/ssl
- ${DATA_PATH_HOST}/certbot/certs/:/var/certs
- ${DATA_PATH_HOST}/letsencrypt/:/etc/letsencrypt
- ./certbot/letsencrypt/:/var/www/letsencrypt
3. Reload Nginx conf file and launch Certbot service
Launch the command docker-compose exec nginx nginx -s reload; docker-compose up --build certbot
Once Certbot created the certificat, you can copy their path here :
Quit the container and then uncomment the 443 lines and copy the paths in the nginx file nginx/sites/default.conf
and reload the nginx conf file docker-compose exec nginx nginx -s reload
That's it