Deploying Laravel Projects
To deploy Laravel projects on remote servers, we follow the nine steps outlined below (usually the server runs Ubuntu OS).
All the steps are executed through SSH.
Preparing the Remote Server
- Create the repository directory. We usually name it repository or repo for short.
- Inside the repository directory, create another directory with the name of your project. It would look something like this: repository/yourProjectName.git/
- Inside your project directory, initialize the git repository by executing the following bash command: git bash init –bare. The –bare option is used to initialize the version control system without including the source project files in the directory.
- Create the post-receive file.
Inside repository/yourProjectName.git/hooks/, create a file and name it post-receive.
Now execute the bash commands in post-receive:
“git –work-tree=/var/www/yourDomain/ –git-dir=”/home/userName/repository/yourProjectName.git checkout -f“, “sh var/www/yourDomain/build/build.sh”. The first command will deploy files to the project directory, while the second command will run the bash script for implementation of our bash commands for the project (more about that later).
- Give proper permissions to post-receive. Run chmod +x post-receive inside of repository/yourProjectName.git/hooks/ to give permissions to the file post-receive to be executed.
- Install the LEMP stack (Linux Nginx MySQL PHP). For our projects, we typically use the LEMP stack with modern versions of its modules. You can find all the details on how to install and configure a LEMP stack at the following URL: there.
Prepare Local Project
- We now need to configure our project for deployment, check and add unwanted files or directory to “gitignore”, and create “build.sh”.
Example of build.sh:
Now add a remote branch by executing git remote add beta ssh://user@ip_or_domain/home/userName/repository/yourProjectName.git.You can now run the git push live master git command to start the deployment process.After the files have finished loading to the remote server, build.sh executes all the instructions.
This method of deploying Laravel projects is the most common. However, for some situations, the specific directory paths, package managers, or server configuration might change.