Terraform modules and Ansible scripts to orchestrate setup, deployment (and redeployment) of a Laravel project for flat deployments because not every developer is a DevOps expert but clients still expect them to be. 🥲 Using these scripts, you can even deploy multiple Laravel projects on the same server, if desired.
This project contains Terraform modules for DigitalOcean and Google Cloud Platform. Depending on the cloud platform of your choice, you can use either of those available providers.
It configures and manages deployment of a Laravel project (with Nginx, PHP-FPM 7 or 8, MySQL, Redis, job queue backed by Supervisor, task scheduler etc.) on an Ubuntu VPS with one command.
Firstly, make sure you have Terraform and Ansible installed on your workstation.
If you plan to use DigitalOcean providers, go to this page and create a Personal Access Token. In addition, go to this page and create a Spaces Access Key as well.
To access the droplet via SSH for running Ansible scripts, go to this page and add your public SSH key.
To use Google Cloud Platform, select the relevant project and enable access to following APIs:
- cloudresourcemanager.googleapis.com
- compute.googleapis.com
- dns.googleapis.com
- redis.googleapis.com
- sqladmin.googleapis.com
Go to this page and create a Service Account with Basic > Owner
access.
Once created, also create a JSON key for newly created Service Account and save it in project folder.
To access the compute instance via SSH for running Ansible scripts, go to this page and add your public SSH key.
Begin with installing required Terraform providers:
# For DigitalOcean
terraform -chdir=terraform/digitalocean init
# For Google Cloud Platform
terraform -chdir=terraform/google-cloud init
Once downloaded, deploy the resources using below commands:
# For DigitalOcean
terraform -chdir=terraform/digitalocean apply \
-var="do_token=<digitalocean_pat>" \
-var="do_spaces_access_id=<digitalocean_spaces_key_id>" \
-var="do_spaces_secret_key=<digitalocean_spaces_secret_key>"
# For Google Cloud Platform
terraform -chdir=terraform/google-cloud apply \
-var="gcp_project=<gcp_project_id>" \
-var="gcp_credentials=$(pwd)/<service_account_key_file>.json"
You can now set up required server software e.g., PHP etc.
ansible-playbook -i inventory.ini -u root ansible/setup.yml
You may want to copy the public SSH key from the output of above command and add to your Git repository's Access Keys for read-only access.
Make a copy of sites/example.yml
and update your site specific values.
# copy example site config
cp sites/example.yml sites/yoursite.yml
# update values in sites/yoursite.yml
Before deploying, if you point your configured domain in sites/yoursite.yml
to your newly created server's IP address, the deployment scripts can also take care of installing a free Let's Encrypt SSL certificate automatically.
Finally, deploy your site to newly created server:
ansible-playbook -i inventory.ini -u root ansible/deploy.yml --extra-vars "@sites/yoursite.yml"
In the future, if you ever wish to update your site with latest changes, you can run below command to automatically pull latest changes from your Git repository:
ansible-playbook -i inventory.ini -u root ansible/redeploy.yml --extra-vars "@sites/yoursite.yml"
Send your love