There are several ways to deploy your Laravel application to your server and put it online. An old-fashioned way is FTP, but that is no longer advisable because it is almost impossible to keep track of which files you have to upload. Instead you can deploy Laravel with Git Hooks to push your application to production with only a couple of commands!
Throughout this tutorial, we’ll assume you’re already using Git for version control. We also assume that you can access your server via SSH.
Step 1 – Setting up Git Hooks on the production server
All servers and Control Panels are using different folder structures, so adjust this where necessary! I am using the folder structures as used on DirectAdmin.
Go to your user home directory (not accessible from the public), for example /home/myuser
mkdir gitrepos cd gitrepos mkdir appname.git
Replace appname.git with the name of your application.
cd appname.git git init --bare
You have now created an empty git repository on your production server and the next step is to create the post-receive hook.
cd hooks touch post-receive chmod +x post-receive
In this post-receive hook put the following script:
#!/bin/sh git --work-tree=/home/user/public_html/ --git-dir=/home/user/gitrepos/appname.git checkout -f
The above script puts all project files in the –work-tree folder when you do a git push from your local repository. This is done through a git checkout. Be sure to change the following values:
- This needs to be the directory where you want to put your project files on the server. The domain root needs to point to the /public folder inside this folder.
- This needs to be the location of the git repo that you have just created on the production server.
Step 2 – Add Git remote to the local Git repo
Now that you’ve created the git repository on the production server, you can add it to your local repository with your project code.
git remote add live ssh://*USER*@*WEBSERVER_ADDRESS*/home/*USER*/gitrepos/appname.git
Change the following values to your own:
- This needs to be your production web SSH User
- Change this to your domain name or IP address
- Change this to the name of git git repository on the production server you have created in the first step.
Verify that you added the repository correctly as a remote:
git remote -v
This will output all the remote repositories you have added to your local repository.
When you’re ready to push your code to production, run the following command from your local git repository:
git push live master
You will be asked for your webserver user password (or if you used certificates it is instantly pushed) and now the code is pushed to production! Verify if the code is in the –work-tree you have specified earlier.
Now you have learned how to deploy Laravel with git hooks! The next steps you need to do is install your composer & NPM dependencies, configure your .env file, optionally the permissions on the /storage folder and configure the domain root to the /public folder. How to do this can be found in the official Laravel documentation.
After you have deployed your Laravel app you can learn how to improve performance on your live applications.