search
HomePHP FrameworkLaravelUse Git to implement automated deployment of Laravel projects

Use Git to implement automated deployment of Laravel projects

Introduction

In our development process, we will inevitably use version control. Of course, this also gives you an understanding of Git and SVN. Both are excellent version control tools. I am personally more accustomed to using Git. Of course, this may be related to personal habits. I don’t know how you used git for development in the first place. Anyway, I personally submitted the code to the github repository, then logged in to the server using SSH, and then cloned or updated the version. It sounds very troublesome, and of course it is also very troublesome in practice. So is there any way to "fix it once and for all"? Please read below!

Easy to use

Git hook

What is a git hook? The official explanation is a bit long. Simply put, it is a script that is triggered in a specific environment. This explanation may not be accurate, but I think it is easier to understand. If you want to know more, you can go to the Git official website to check it out. Below we will use hooks to implement automated deployment.

Step one: Create a git user

Log in to our server. By default, you have installed git. Create a git user:

# 创建一个名叫jouzeyu的用户
adduser jouzeyu

Step 2: Add permissions to the git user

#在根目录下的 home 文件夹下创建一个git文件夹
mkdir /home/git 
#切换到创建好的git文件夹
cd /home/git
#创建 .ssh文件夹,里面主要用来放公钥
mkdir .ssh
#切换到.ssh文件夹并创建authorized_keys文件
cd .ssh
touch authorized_keys

Step 3: Configure git and obtain the public key

#在本地配置用户名和邮箱,我的用户名默认为jouzeyu
git config --global user.name "jouzeyu"
git config --global user.email "your email"

Note: If you use the --global option, all your future projects will use the user information configured here. If you want to use another name or email address in a specific project, just execute under the project:

git config user.name "xxx"
git config user.email "xxx"

OK, next we get the public key, please check the .ssh file under your user first If the folder contains public and private keys before, we need to look for a pair of files named id_dsa or id_rsa, one of which has a .pub extension. The .pub file is your public key and the other is your private key. If not, run ssh-keygen.

Use the cat ~/.ssh/id_rsa.pub command to obtain the public key, copy it, use the vi or vim command to paste it into the authorized_keys file we created previously, and use: wq to save it.

Step 4: Initialize the warehouse

Create a folder to store the git warehouse:

mkdir /www/wwwroot/git
cd /www/wwwroot/git

Initialize the warehouse:

#初始化一个裸仓库(强烈建议)
git init --bare website.git
#配置仓库的权限,让我们之前创建好的git用户jouzeyu能读写
chown -R git:git website.git

It must be noted here that if permission is not given, the subsequent git pull will report an error because there is no permission to write. The difference between a bare warehouse and an ordinary warehouse is simply that the bare warehouse cannot see the project files. The ordinary warehouse is the same as your project directory, except for an additional .git folder.

Step 5: Generate the project warehouse

This is also done on the server, and please note that /www/wwwroot/ is the root directory of my environment.

#创建我服务器上的项目目录test
mkdir /www/wwwroot/test
#克隆仓库
git clone /www/wwwroot/git/website.git
#设置权限
chown -R git website

Note: Be sure to pay attention to my path. The git repository is /www/wwwroot/git and the project repository is /www/wwwroot/test.

Step 6: Clone to local

# 通过ip地址从配置好的线上仓库拉取下来
git clone git@47.97.121.XXX:/www/wwwroot/git/website.git
# 如果有配置域名的话也可以通过域名拉取
git clone git@www.XXX.XXX:/www/wwwroot/git/website.git

Because of the public key, no password is required here. If successful, a website will appear on your computer. folder, if an error is reported, please check before proceeding with the following operations.

Step 7: Test upload (git pull)

# 打开刚才克隆下来的本地仓库
cd website
# 创建README.md文件
touch README.md
git add .
git commit -m"创建README.md文件"
git push

It has been uploaded normally. If an error is reported, please check the permissions. As mentioned above, if If it doesn’t work yet, you can comment below.

Step 8: Add the hook

Finally it’s time to get to the main point. The writing is relatively detailed, so it’s a little more troublesome. Back to our online server, the following is performed online:

#切换到这个目录
cd /www/wwwroot/git/website.git/hooks
# 生成post-receive文件
touch post-receive
# 使用vim编辑
vim post-receive

Paste in the post-receive file:

#!/bin/sh
# 打印输出
echo '======上传代码到服务器======'
# 打开线上项目文件夹
cd /www/wwwroot/test/website
# 这个很重要,如果不取消的话将不能在cd的路径上进行git操作
unset GIT_DIR
git pull origin master
# 自动编译vue项目,如有需要请去掉前面的#号
# npm run build
# 自动更新composer(我暂时没试过)
# composer update
echo $(date) >> hook.log
echo '======代码更新完成======'

After saving, add running permissions to the post-receive file:

chmod +x post-receive

The last step

Modify some content locally, and then submit and push git pull. You can see that we have implemented automated deployment.

Use Git to realize automated deployment of Laravel projects

Use Git to implement automated deployment of Laravel projects

For more Laravel-related technical articles, please visit the Laravel Framework Getting Started Tutorial column. study!

The above is the detailed content of Use Git to implement automated deployment of Laravel projects. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:learnku. If there is any infringement, please contact admin@php.cn delete
Laravel's Backend Capabilities: Databases, Logic, and MoreLaravel's Backend Capabilities: Databases, Logic, and MoreApr 14, 2025 am 12:04 AM

Laravel performs strongly in back-end development, simplifying database operations through EloquentORM, controllers and service classes handle business logic, and providing queues, events and other functions. 1) EloquentORM maps database tables through the model to simplify query. 2) Business logic is processed in controllers and service classes to improve modularity and maintainability. 3) Other functions such as queue systems help to handle complex needs.

Laravel's Versatility: From Simple Sites to Complex SystemsLaravel's Versatility: From Simple Sites to Complex SystemsApr 13, 2025 am 12:13 AM

The Laravel development project was chosen because of its flexibility and power to suit the needs of different sizes and complexities. Laravel provides routing system, EloquentORM, Artisan command line and other functions, supporting the development of from simple blogs to complex enterprise-level systems.

Laravel (PHP) vs. Python: Development Environments and EcosystemsLaravel (PHP) vs. Python: Development Environments and EcosystemsApr 12, 2025 am 12:10 AM

The comparison between Laravel and Python in the development environment and ecosystem is as follows: 1. The development environment of Laravel is simple, only PHP and Composer are required. It provides a rich range of extension packages such as LaravelForge, but the extension package maintenance may not be timely. 2. The development environment of Python is also simple, only Python and pip are required. The ecosystem is huge and covers multiple fields, but version and dependency management may be complex.

Laravel and the Backend: Powering Web Application LogicLaravel and the Backend: Powering Web Application LogicApr 11, 2025 am 11:29 AM

How does Laravel play a role in backend logic? It simplifies and enhances backend development through routing systems, EloquentORM, authentication and authorization, event and listeners, and performance optimization. 1. The routing system allows the definition of URL structure and request processing logic. 2.EloquentORM simplifies database interaction. 3. The authentication and authorization system is convenient for user management. 4. The event and listener implement loosely coupled code structure. 5. Performance optimization improves application efficiency through caching and queueing.

Why is Laravel so popular?Why is Laravel so popular?Apr 02, 2025 pm 02:16 PM

Laravel's popularity includes its simplified development process, providing a pleasant development environment, and rich features. 1) It absorbs the design philosophy of RubyonRails, combining the flexibility of PHP. 2) Provide tools such as EloquentORM, Blade template engine, etc. to improve development efficiency. 3) Its MVC architecture and dependency injection mechanism make the code more modular and testable. 4) Provides powerful debugging tools and performance optimization methods such as caching systems and best practices.

Which is better, Django or Laravel?Which is better, Django or Laravel?Mar 28, 2025 am 10:41 AM

Both Django and Laravel are full-stack frameworks. Django is suitable for Python developers and complex business logic, while Laravel is suitable for PHP developers and elegant syntax. 1.Django is based on Python and follows the "battery-complete" philosophy, suitable for rapid development and high concurrency. 2.Laravel is based on PHP, emphasizing the developer experience, and is suitable for small to medium-sized projects.

Which is better PHP or Laravel?Which is better PHP or Laravel?Mar 27, 2025 pm 05:31 PM

PHP and Laravel are not directly comparable, because Laravel is a PHP-based framework. 1.PHP is suitable for small projects or rapid prototyping because it is simple and direct. 2. Laravel is suitable for large projects or efficient development because it provides rich functions and tools, but has a steep learning curve and may not be as good as pure PHP.

Is Laravel a frontend or backend?Is Laravel a frontend or backend?Mar 27, 2025 pm 05:31 PM

LaravelisabackendframeworkbuiltonPHP,designedforwebapplicationdevelopment.Itfocusesonserver-sidelogic,databasemanagement,andapplicationstructure,andcanbeintegratedwithfrontendtechnologieslikeVue.jsorReactforfull-stackdevelopment.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment