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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

Laravel provides a comprehensive Auth framework for implementing user login functions, including: Defining user models (Eloquent model), creating login forms (Blade template engine), writing login controllers (inheriting Auth\LoginController), verifying login requests (Auth::attempt) Redirecting after login is successful (redirect) considering security factors: hash passwords, anti-CSRF protection, rate limiting and security headers. In addition, the Auth framework also provides functions such as resetting passwords, registering and verifying emails. For details, please refer to the Laravel documentation: https://laravel.com/doc

In the Laravel framework version selection guide for beginners, this article dives into the version differences of Laravel, designed to assist beginners in making informed choices among many versions. We will focus on the key features of each release, compare their pros and cons, and provide useful advice to help beginners choose the most suitable version of Laravel based on their skill level and project requirements. For beginners, choosing a suitable version of Laravel is crucial because it can significantly impact their learning curve and overall development experience.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

To learn Laravel 6, you can get video tutorials from Laracasts (recommended), official documentation and YouTube. Recommended courses include Laracasts’ “Laravel 6 From Beginner to Mastery” and “Official Laravel 6 Tutorial” produced by the official team. When choosing a video course, consider skill level, teaching style, project experience and frequency of updates.

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

Want to learn the Laravel framework, but suffer from no resources or economic pressure? This article provides you with free learning of Laravel, teaching you how to use resources such as online platforms, documents and community forums to lay a solid foundation for your PHP development journey from getting started to master.
