Home >Backend Development >PHP Tutorial >Deploy Your Website Using Laravel and Git
Core points
.gitignore
to ignore certain files and check if they exist before running the deployment route. Other methods include password protection or IP address restriction. Successful web development is inseparable from effective website deployment workflows. Whether your workflow is good or bad or not, if the website is not available, customers will not be able to pay for the fruits of your hard work.
There are many ways to deploy a website to a production server, each with its advantages and disadvantages, but the starting and ending methods are the same. The key to a good website deployment workflow is the intermediate steps.
The other day, I used FileZilla to upload files to my production server. I always felt that FileZilla’s interface wasn’t very friendly, but for a long time I’ve thought it was standard practice.
For the same project, I also use Git to track file changes. When I started typing git push origin
, I paused and thought for a moment. If the website deployment is as simple as pushing a Git repository, wouldn’t it be much more convenient?
Think about it carefully, Git is the perfect tool for handling website deployment files. It tracks all file changes and pushes or pulls what you want with just one command. I decided to start searching Google for a way to use Git when deploying my own website.
One popular way I found to deploy websites using Git is to make the most of Git Hooks (Tom Oram likes to use similar methods). Imagine that Git Hooks will start a predefined script by simply typing git push origin
into the terminal console. This predefined script accesses your production server and pulls the latest file changes. You can also update your website using the same commands as push changes. I thought this approach was great until I realized that just because I pushed the local repository didn't mean I was ready to go live.
I want a way to push a repository as simple as using Git. More importantly, I want to have full control when bringing the content online. I found a similar workflow that handles file transfers using Git. Most importantly, I also found that it is possible to use the PHP framework Laravel to automate more repetitive tasks.
The following is my website deployment workflow:
Structural Settings
In this section, we will set up a --bare repository as our central repository and create two repositories by cloning. One is our local website where we will perform the deployment process; the other is our online website. We will also install Laravel.
Before you start, you need a local server and production server with Git installed.
SSH Connect to your production server and find the directory where you want the central repository to be located.
<code>ssh username@domain.com cd repos</code>
Now initialize your repository.
<code>git init --bare --shared mywebsite.git</code>
This folder is usually recommended to place it outside the public folder structure. This way, others won't accidentally stumble upon your private repository.
Determine where to host the website files on your public_html
.
<code>cd ~/public_html/</code>
Now clone the center --bare repository you just created and log out of this SSH session.
<code>git clone username@domain.com:~/repos/mywebsite.git mywebsite exit</code>
If you are using something like Homestead, you may need to use the Vagrant command to SSH to connect to your local server to access your files.
<code>vagrant ssh</code>
Follow the steps you just created an online website.
<code>cd websites git clone username@domain.com:~/repos/mywebsite.git mywebsite</code>
Before setting up Laravel, you need to install it on your local website.
Add your remote server settings to the configuration file by opening /app/config/remote.php
.
<code>'connections' => array( 'production' => array( 'host' => 'domain.com', 'username' => 'username', 'password' => '********************', 'key' => '', 'keyphrase' => '', 'root' => '/var/www', ), ),</code>
Remember the "production" key, as we need to reference it later.
Add your files to your local website repository so we can track any changes to them.
<code>git add .</code>
Execute your initial commit.
<code>git commit -m 'initial commit with laravel'</code>
Finally, push to the central repository on the production server.
<code>git push origin master</code>
When accessing your localhost, you should see Laravel's "You have arrived." screen.
Good job! You've all set up and configured, and now you should be ready to dig into interesting content.
Core workflow using Git
After everything is set up, it will be easy to deploy your website using Git. Let's look at the code and try to understand its core content.
It is important to understand the workflow, as we will rewrite it later in PHP using Laravel. It also helps us debug any issues that may arise.
<code>ssh username@domain.com cd repos</code>
<code>git init --bare --shared mywebsite.git</code>
If you've all been correct so far, you should see Laravel's "You have arrived." screen when visiting your online site.
If you want to stop here, I won't have any opinions about you. This is a pretty reliable deployment workflow in itself. But we can make it more efficient by automating it with Laravel.
Automatic deployment with Laravel
OK, now that we know how to deploy a website using Git, let's use Laravel to automate this process. This part may not be necessary, but if you are already using Laravel, I would ask, “Why not?” Using Laravel here makes this website deployment workflow simple, efficient, controllable, and customizable.
Open the /app
page in the routes.php
folder and add the following PHP code line to the file.
<code>cd ~/public_html/</code>
Whenever we access http://localhost/deploy
, the common function of the Server
controller is executed. deploy
class that extends BaseController
. Server
<code>git clone username@domain.com:~/repos/mywebsite.git mywebsite exit</code>Now insert the common function
into the controller. deploy
<code>vagrant ssh</code>Save it in your
folder and name it /app/controllers
. Server.php
3. Insert Laravel's SSH facade into the
deploy
<code>cd websites git clone username@domain.com:~/repos/mywebsite.git mywebsite</code>Now the
function will accept two parameters we need to provide. The first and most important parameter is the array of terminal commands we want to run when executing the run()
function. deploy
<code>'connections' => array( 'production' => array( 'host' => 'domain.com', 'username' => 'username', 'password' => '********************', 'key' => '', 'keyphrase' => '', 'root' => '/var/www', ), ),</code>The second is the function we want to call to handle the feedback we receive from the server.
<code>git add .</code>Now, whenever we want to deploy our website, we just need to visit
. Simple enough? Yes, not exactly. http://localhost/deploy
and deploy our website. What we need to do is set up something to prevent this. http://domain.com/deploy
There are many ways to do this, and we can argue which method is safest until we are exhausted. You can protect the route's password, you can block access through an IP address, and so on.
In this example, we will use .gitignore
and check if the file exists, then run the route we just created.
controllers
file in .gitignore
to ignore Server.php
. Create a new file and save it in /app/controllers
and name it .gitignore
.
Add the following text line to the file and save it.
<code>ssh username@domain.com cd repos</code>
Server.php
file exists before running the route that deploys our website. Remember the route we created before to deploy our website? We need to wrap it with this conditional statement, and then we can go online.
<code>git init --bare --shared mywebsite.git</code>
When it is finished, it should look like this.
<code>cd ~/public_html/</code>
Get creative
That's it! Just save all updated files, commits, and pushes and you can start deploying with Git-friendly workflows.
You can go further if you want. Just add http://localhost/deploy
to your bookmark for quick one-click deployment. You can even create a simple HTML form to post to the page, allowing you to pull specific branches. The possibilities are endless.
Deployment with Git and Laravel makes my boring website deployment tasks fun. If you know Git, it's easy to set up, but it's easier to use.
Please share with me the creative methods you used to simplify website deployment!
FAQs (FAQs) for Deploying Websites with Laravel and Git
Clashes can occur when deploying Laravel websites using Git, especially when multiple developers work on the same project. To resolve these conflicts, you need to use Git's conflict resolution tool. When conflict occurs, Git will pause the change base and give you a chance to fix the conflict. You can then use git add
to add the resolved files and use git rebase --continue
to continue to rebase. Before you start working, be sure to always pull the latest changes from the repository to minimize conflicts.
Git branch is a powerful tool for managing different versions of Laravel projects. You can create a new branch for each feature or bug fix, which allows you to handle multiple tasks simultaneously without affecting the main code base. After the task is completed, you can merge the branches back to the main code base. This method ensures that the main code base remains stable and error-free.
Automated deployment process can save a lot of time and effort. You can use Git hooks, which are scripts that Git executes before or after events such as commit, push, and receive. You can write a script that automatically deploys your website whenever you push to the main branch. This way, you can make sure your website is always consistent with the latest changes.
Git provides several ways to roll back changes. You can use git revert
to create a new commit to undo the changes made in a specific commit. Alternatively, you can use git reset
to move the HEAD pointer to the previous commit, effectively "forgot" the commit that appears afterwards. Be careful when using git reset
as it may permanently delete your changes.
Security is an important aspect of any web development project. When deploying Laravel projects with Git, make sure to add sensitive files (such as .env
) to your .gitignore
files to prevent them from being committed to the repository. Additionally, secure protocols such as SSH or HTTPS are always used when pushing and pulling from the repository.
Laravel's migration system is a powerful tool for managing database schemas. When deploying a project, you can use php artisan migrate
to apply any pending migrations. However, be careful when working with multiple developers, as conflicting migrations can cause problems. Always pull the latest changes from the repository to avoid conflicts before creating a new migration.
Git is a powerful collaboration tool. You can use branches to handle different features simultaneously and use pull requests to review and merge changes into the main code base. Additionally, Git's conflict resolution tool can help you resolve any conflicts that occur when you merge changes.
Git provides a complete history of all changes in the project. You can view the commit history using git log
and use git diff
to view changes made in a specific commit. This is very useful for debugging and understanding the evolution of the project.
Performance optimization is a key aspect of web development. When deploying Laravel websites with Git, you can use Laravel’s built-in caching to improve performance. Also, consider using CDN to provide static resources and optimize your images and other resources to reduce loading time.
Correct handling of errors and exceptions is essential for a smooth user experience. Laravel provides a powerful exception handling system that you can use to handle different types of errors. When an error occurs, you can use Git to roll back to the previous stable state while fixing the error.
The above is the detailed content of Deploy Your Website Using Laravel and Git. For more information, please follow other related articles on the PHP Chinese website!