Deploying Laravel Projects
Elizabeth, Project Manager
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.
- Install a package manager. Laravel requires package managers in order to install dependencies. Example package managers include Composer for PHP projects or NPM for JavaScript.
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:
ROOT_DIR=”/home/userName/repository/yourProjectName.git”
COMPOSER_DIR=”/home/userName/.composer”
if [ ! -f ${COMPOSER_DIR}/composer.phar ]; then
php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=${ROOT_DIR}
fi
PHP_PATH="/usr/bin/php-7.2"
${ROOT_DIR} ${COMPOSER_DIR}/composer.phar install -d ${ROOT_DIR}
${ROOT_DIR} ${COMPOSER_DIR}/composer.phar dumpautoload
chmod -R 777 ${ROOT_DIR}/storage/
chmod -R 777 ${ROOT_DIR}/bootstrap/cache/
${PHP_PATH} ${ROOT_DIR}/artisan migrate --force
${PHP_PATH} ${ROOT_DIR}/artisan view:clear
${PHP_PATH} ${ROOT_DIR}/artisan cache:clear
${PHP_PATH} ${ROOT_DIR}/artisan clear-compiled
${PHP_PATH} ${ROOT_DIR}/artisan optimize
cd ${ROOT_DIR}
npm install
npm run prod
echo "Done..!"
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.
Tags: Laravel, Web Development
Updated
4th December 2023
We are A2 Design
Since 2006, we develop custom online marketplaces of any complexity and augment software development teams.
Read more