In the previous laravel tutorial, we have seen how you can install laravel on your local machine to get started with your laravel project.
But what if your project is ready and you want to transfer it to a production server?
Is it feasible to transfer all the files from your development environment to the production server?
How to provide updates to the project after deploying it to the production server?
Here in this tutorial, we are going to tell you a step by step process by which you can transfer the whole laravel project with the help of GitHub. First of all, let’s have a brief introduction to GitHub before we get started.
What is GitHub?
GitHub is a web-based Git or version control repository and Internet hosting service. It is mostly used for code. It offers all of the distributed version control and source code management functionality of Git as well as adding its own features.
Source: Wikipedia.org
Why are we using GitHub?
Let’s say you have a project that will be modified every once in a while because you want to provide frequent updates and bug fixes from time to time.But does it mean that you have to re-upload your whole project every time you modify something?
But does it mean that you have to re-upload your whole project every time you modify something?
Well, technically yes. But what if after uploading you discover that there is some critical bug in the updated version. How can you roll back?
All of these problems can easily be addressed by using GitHub. You can find out more about it on the web because in this article we are not diving too much into GitHub.
Steps to Deploy Laravel to Production Server on Ubuntu with GitHub
There are several steps that need to be followed in order to successfully deploy your laravel project on the server.
Creating a GitHub Repository
- First of all, you will have to create a GitHub repository so that your project can be uploaded there.
- Login to your GitHub Account.
- Click on the +(Plus) icon and click New Repository.
- Now you will see a page like this. Fill out the repository name. (The one that you can remember easily)
- If you want to keep your project private you will need to buy GitHub premium plan.
- For Laravel Project select the .gitignore file and search for Laravel. This will add all the dependencies folders and other common folders to the .gitignore file.
- Now click Create Repository.
Uploading Project to The GitHub Repository
There are various softwares available for Windows, Linux, and Mac systems. But here we are considering that you are using a Linux Distribution for Laravel Development.
- Install Git by typing sudo apt-get install git.
- Now mount the project directory using the cd command.
- Type git init to initialize the git repository in that folder.
- Execute git remote add origin https://github.org/{yourusername}/{repositoryname}.git to link the GitHub repository to the current directory.
- Now for user-friendliness, we are going to set git to remember credentials everytime we have to push or pull to the repository. To do so type git config credential.helper store
- Fetch the .gitignore file from the repo using git pull origin master.
- Now you can add all the files using git add . command
- Commit all the file using git commit -m “Initializing the Repo”.
- Upload the modifications using git push origin master.
To verify that the push was successfull you can go to your repository URL and check that the files are uploaded or not.
Setting Up the Production Server
Well, this is a very easy task as we have already discussed how to set up a laravel development server on Ubuntu 16.04.
Here is what you have to do. Follow all the steps given in How to Install Laravel 5.5 on Ubuntu 16.04 but don’t create a new laravel app. Just create an empty folder and consider it your project directory.
Note: You can ignore dev packages and install the normal one like php instead of php-dev as it is not really necessary and required on a production server.
Uploading Laravel Project to The Production Server
Now it’s time to deploy your project to the production server. You will have to download all the files of the project that we previously uploaded to the GitHub repository.
- Install Git by typing sudo apt-get install git.
- Now mount the project directory using the cd command.
- Type git init to initialize the git repository in that folder.
- Execute git remote add origin https://github.org/{yourusername}/{repositoryname}.git to link the GitHub repository to the current directory.
- Now for user-friendliness, we are going to set git to remember credentials everytime we have to push or pull to the repository. To do so type git config credential.helper store
- Fetch the entire project from the repo using git pull origin master.
Configuring and Installing Laravel Project on Production Server
- Once you fetch all the files to the directory, now it’s time to create .env file for the project. Create the one that is appropriate and needed for your project but make sure to set the environment to production so that confidential data is not revealed when an error occurs.
- Now install all the dependencies from composer.json file using command composer update.
- Set the permission of the project folder to 755 and storage folder to 777 so that everything can work smoothly.
- Test out whether the laravel installation is accessible through the browser. If you have done everything correctly you will be good to go.
Enabling Caching of Routes and Configuration
In a production server, you need to make sure that everything is snappy. For this you can enable caching of configuration and routes but here are some things that you need to keep in mind before enabling them.
- If you are accessing any environment variables using the env() function then replace all of them to use config option. i.e. env(“APP_NAME”) is equivalent to config(“app.name”)
- For routes caching you cannot use any Closure Routes that means you will have to compulsorily use controllers for everything.
- Type php artisan config:cache to enable configuration cache and php artisan route:cache to enable routes caching.
You have successfully deployed your laravel project on your production server with Ubuntu.
Now, what if you need to patch an update from your development server. So here you go.
Updating the Laravel Project
- On your development server type git add . then type git commit -m “What you have done in the update that you can remember” and type git push origin master to upload the modifications.
- Now on your production server type git pull origin master to fetch all the modifications and you are done.
- If you have made changes to routes and config then you need to clear the cache for both.
- In case if you have added any dependencies to the project then install those dependencies on production server using composer update command.
So that was all on setting up the production server for your laravel project. It may be possible that you will encounter some problems during the process but feel free to leave that in the comment so that we can look into it.
Have a great day!
You are correct but envoyer as well as cloudways both costs money and it is quite higher than renting a normal server from Vultr & Digital Ocean
I found envoyer best for deploying app from git. It really makes the process faster. I use this on my laravel hosting platform, Cloudways and I can setup a server from scratch and deploy laravel on it under half a second.