使用Laravel和Git部署您的网站

Use Git and Laravel to deploy your websites with one click using a bookmark.

You can’t be a successful web developer without using some sort of deployment workflow for your websites. It doesn’t matter how good or bad your workflow might be. If you can’t get your website up to production then your client will never pay you for your hard work.

如果不为网站使用某种部署工作流程,就无法成为成功的Web开发人员。 不管您的工作流程有多好或多坏。 如果您无法使您的网站投入生产,那么您的客户将永远不会为您的辛勤工作付费。

There are many different ways to deploy your website to your production server. Each one of them has their own list of pros and cons, and each one starts and finishes the same way. What makes a good website deployment workflow is what happens in the between.

有许多种方法可以将网站部署到生产服务器。 每个人都有自己的优缺点列表,并且每个人的开始和结束方式都相同。 良好的网站部署工作流程是在两者之间发生的。

The other day I was working on a project where I was using FileZilla to FTP my files up to my live production server. I’ve never considered FileZilla to be the easiest interface to use, but for the longest time I thought it to be standard practice.

前几天,我在一个项目中工作,在该项目中,我使用FileZilla将文件通过FTP传输到实时生产服务器。 我从来没有考虑过FileZilla是最容易使用的界面,但是从最长的时间来看,我认为这是标准做法。

For the same project, I was also using Git to track my file changes. I started to type out git push origin when I stopped to think to myself. Wouldn’t it be convenient if deploying websites was as easy as pushing Git repositories?

对于同一项目,我还使用Git跟踪文件更改。 当我停止思考时,我开始输入git push origin 。 如果部署网站和推送Git存储库一样容易,那会不会很方便?

If you think about it, Git is the perfect tool to handle website files in deployment. It keeps track of all your file changes, and it pushes or pulls only what it needs with one command. I decided to set out on a Google journey to search for a way to use Git when deploying my own websites.

如果您考虑一下,Git是处理部署中网站文件的理想工具。 它跟踪所有文件更改,并且仅通过一个命令即可推送或提取所需文件。 我决定踏上Google之路,寻找在部署自己的网站时使用Git的方法。

One popular method I found using Git to deploy your websites is by making good use of Git Hooks (Tom Oram likes to use a similar approach). Imagine as soon as you type git push origin into your terminal console, Git Hooks launch a predefined script. That predefined script accesses your production server and pulls your most recent file changes. With the same command you used to push your changes, you were also able to update your website. I thought this method was awesome until I realized that just because I push my local repository doesn’t mean I am ready to go live with it.

我发现使用Git部署网站的一种流行方法是善用Git Hooks ( Tom Oram喜欢使用类似的方法 )。 想象一下,一旦在终端控制台中输入git push origin ,Git Hooks就会启动一个预定义的脚本。 该预定义脚本将访问您的生产服务器并提取您最近的文件更改。 使用用于推送更改的相同命令,您还可以更新网站。 我认为这种方法很棒,直到我意识到仅仅因为我推送本地存储库并不意味着我准备好使用它。

I wanted something that was as easy as pushing a repository with Git. More important, I wanted to be in full control when pushing content live. I was able to find a similar workflow that used Git to handle the file transferring. On top of that I found out I could also use the PHP framework Laravel to automate the more repetitive tasks.

我想要的东西就像使用Git推送存储库一样容易。 更重要的是,我希望在直播内容时能够完全控制自己。 我能够找到使用Git来处理文件传输的类似工作流程。 最重要的是,我发现我还可以使用PHP框架Laravel自动执行重复性更高的任务。

Here is the website deployment workflow I came up with:

这是我想到的网站部署工作流程:

结构入门 (Getting Started with Structure)

In this section, we will set up a --bare repository as our central repository and create two repositories by cloning. One is for our local website where we will be executing our deployment process, and the other for our live website. We will also be installing Laravel.

在本节中,我们将建立一个--bare存储库作为我们的中央存储库,并通过克隆创建两个存储库。 一个用于我们将在其中执行部署过程的本地网站,另一个用于我们的实时网站。 我们还将安装Laravel。

Before getting started, you will need a local server and production server with Git installed on both.

在开始之前,您将需要同时安装了Git的本地服务器和生产服务器。

有用的资源: (Helpful Resources:)
1.初始化--bare存储库 (1. Initialize your --bare repository)

SSH into your production server and locate the directory you would like your central repository to be.

SSH进入生产服务器,然后找到您想要中央存储库所在的目录。

ssh username@domain.com
cd repos

Now initialize your repository.

现在初始化您的存储库。

git init --bare --shared mywebsite.git

It’s usually considered a good practice to keep this folder outside of your public folder structure. This way, someone can’t stumble upon your private repository on accident.

通常,将此文件夹保留在公用文件夹结构之外是一个好习惯。 这样,就不会有人偶然发现您的私有存储库。

2.克隆创建实时网站 (2. Clone to create live website)

Determine where in your public_html you would like to host your live website files.

确定您要在public_html中托管实时网站文件的位置。

cd ~/public_html/

Now you clone the central --bare repository you just created and logout of this SSH session.

现在,您克隆刚刚创建的中央--bare存储库,并注销此SSH会话。

git clone username@domain.com:~/repos/mywebsite.git mywebsite
exit
3.克隆以创建本地网站 (3. Clone to create local website)

If you’re using something like Homestead, you may need to use the Vagrant command to SSH into your local server to access your files.

如果您使用的是Homestead之类的工具 ,则可能需要使用Vagrant命令将SSH SSH到本地服务器以访问文件。

vagrant ssh

Follow the steps you just went through to create your live website.

按照您刚创建网站的步骤进行操作。

cd websites
git clone username@domain.com:~/repos/mywebsite.git mywebsite
4.设置Laravel (4. Setting up Laravel)

Before setting Laravel up, you need to have it installed on your local website.

在设置Laravel之前,您需要在本地网站上安装它。

有用的资源: (Helpful Resources:)

Add your remote server settings to the configuration file by opening /app/config/remote.php.

通过打开/app/config/remote.php将远程服务器设置添加到配置文件中。

'connections' => array(
        'production' => array( 
            'host'      => 'domain.com',
            'username'  => 'username',
            'password'  => '********************',
            'key'       => '',
            'keyphrase' => '',
            'root'      => '/var/www',
        ),
    ),

Remember the “production” key because we will need to reference it later.

记住“生产”键,因为我们以后需要参考它。

Add your files to your local website repository so we can track any changes to them.

将您的文件添加到本地网站存储库,以便我们可以跟踪对它们的任何更改。

git add .

Execute your initial commit.

执行您的初始提交。

git commit -m 'initial commit with laravel'

Finally, push to your central repository on your production server.

最后,推送到生产服务器上的中央存储库。

git push origin master

When you visit your localhost you should see Laravel’s, “You have arrived.” screen.

当您访问本地主机时,您应该会看到Laravel的“您已经到达”。 屏幕。

Great job! you’re all set up and configured, so you should now be ready to dive into the fun stuff.

很好! 您已经完成所有设置和配置,因此您现在应该准备好学习有趣的东西。

使用Git的核心工作流程 (The Core Workflow Using Git)

Once everything is set up, deploying your websites with Git is a piece of cake. Let’s look at the code to try and understand what’s going on at its core.

一切设置完成后,使用Git部署网站就轻松了。 让我们看一下代码,尝试了解其核心内容。

It’s important to understand the workflow because we will rewrite it later in PHP with Laravel. It’ll also help us debug any problems that may come up.

理解工作流程很重要,因为稍后我们将使用Laravel在PHP中重写它。 它还将帮助我们调试可能出现的任何问题。

1.继续并通过SSH进入您的实时服务器,然后找到您的生产存储库。 (1. Go ahead and SSH into your live server, and then find your production repository.)
ssh username@domain.com
cd public_html/mywebsite
2.现在下拉中央存储库以合并新文件更改。 (2. Now pull your central repository down to merge the new file changes.)
git pull origin master

If you have done everything correct up to this point, you should see Laravel’s, “You have arrived.” screen when you visit your live site.

如果到目前为止您已完成所有正确的操作,则应该看到Laravel的“您已经到达”。 当您访问实时网站时屏幕。

If you wanted to stop here, I wouldn’t think any less of you. That in itself is a pretty solid deployment workflow. But we can make it even more efficient by automating it using Laravel.

如果您想在这里停留,我不会再想你们了。 这本身就是一个相当可靠的部署工作流。 但是我们可以通过使用Laravel使其自动化来提高效率。

使用Laravel自动化部署 (Automating Deployment with Laravel)

Ok, so now that we know how to deploy a website using Git let’s use Laravel to automate the process. This part may not be necessary, but if you’re already using Laravel I ask, “Why not?” Using Laravel here makes this website deployment workflow easy, efficient, controllable, and customizable.

好的,现在我们知道了如何使用Git部署网站,让我们使用Laravel来自动化该过程。 这部分可能不是必需的,但是如果您已经在使用Laravel,我会问:“为什么不呢?” 在此处使用Laravel可使此网站部署工作流变得简单,高效,可控制和可自定义。

1.让我们开始创建一个引用控制器的简单路由。 (1. Let’s begin by creating a simple route that references a controller.)

Open up your routes.php page in your /app folder, and append the following line of PHP to the file.

/app文件夹中打开routes.php页面,然后将以下PHP行添加到该文件中。

Route::get('/deploy', 'Server@deploy');

Whenever we visit http://localhost/deploy the public function deploy in the Server controller will execute.

每当我们访问http://localhost/deploydeployServer控制器中http://localhost/deploy公共功能。

2.现在,我们创建要引用的控制器,并保存它。 (2. Now let’s create the controller we’re referencing, and save it.)

Start off with an empty Server class that extends the BaseController.

从扩展BaseController的空ServerBaseController

class Server extends BaseController {

    }

Now insert a public function deploy into the controller.

现在,将公共功能deploy插入控制器。

class Server extends BaseController {
    
        public function deploy() {
        
        }
    }

Save it in your /app/controllers folder, and name it Server.php.

将其保存在/app/controllers文件夹中,并将其命名为Server.php

Here’s where it gets fun!

这里很有趣!

3.将Laravel的SSH外观插入部署功能,并重复Git部署工作流程。 (3. Insert Laravel’s SSH facade into the deploy function and repeat the Git deployment workflow.)

Insert the SSH facade. We want to access the production remote configurations we set up earlier.

插入SSH门面 。 我们要访问我们之前设置的production远程配置。

SSH::into('production')->run();

Now the run() function will accept two arguments that we need to provide. The first, and most important one is an array of terminal commands we want to run when we execute our deploy function.

现在, run()函数将接受我们需要提供的两个参数。 第一个也是最重要的一个是我们在执行deploy函数时要运行的一系列终端命令。

SSH::into('production')->run(array(
	    'cd ~/public_html/mywebsite',
	    'git pull origin master'
	));

The second is a function we want called to handle the feedback we’re receiving from the server.

第二个函数是我们要调用的函数,用于处理从服务器接收到的反馈。

SSH::into('production')->run(array(
	    'cd ~/public_html/mywebsite',
	    'git pull origin master'
	), function($line){
	
	    echo $line.PHP_EOL; // outputs server feedback
	});

Now whenever we want to deploy our website, all we have to do is visit http://localhost/deploy and we’re done. Easy enough right? Yes, and no.

现在,只要我们要部署我们的网站,我们要做的就是访问http://localhost/deploy ,我们就完成了。 很容易吧? 是的,没有。

There are a couple of security breaches we need to handle before we can call this a night. Anyone and their mother could stumble upon http://domain.com/deploy and deploy our website as is. What we need to do is set something in place to prevent this.

在我们称之为“夜晚”之前,我们需要处理一些安全漏洞。 任何人及其母亲都可能偶然发现http://domain.com/deploy并按原样部署我们的网站。 我们需要做的是设置一些预防措施。

There are many ways of going about doing this, and we could debate which method is the most secure until we’re blue in the face. You can password protect the route, you could prevent access by IP address, etc.

有很多方法可以做到这一点,我们可以辩论哪种方法最安全,直到我们脸色发青。 您可以使用密码保护路由,也可以阻止通过IP地址访问等。

For this example we’re going to use .gitignore, and check to make sure the file exists before we run the route we just created.

在此示例中,我们将使用.gitignore ,并在运行刚才创建的route之前检查文件是否存在。

4.在控制器中创建一个.gitignore文件,以忽略Server.php(4. Create a .gitignore file in controllers to ignore Server.php.)

Create a new file and save it in /app/controllers as .gitignore.

创建一个新文件,并将其另存为.gitignore/app/controllers中。

Add the following line of text to the file and save it.

将以下文本行添加到文件中并保存。

Server.php
5.在运行部署我们网站的route之前,请确保Server.php文件存在。 (5. Make sure Server.php file exists before running the route that deploys our website.)

Remember the route we created earlier to deploy our website? We need to wrap it with this conditional statement, and then we’ll be ready to go live with it.

还记得我们之前创建的用于部署网站的route吗? 我们需要将此条件语句包装起来,然后就可以开始使用它了。

if (file_exists(__DIR__.'/controllers/Server.php')) {
        // route goes here
    }

It should look like this when we’re all done.

当我们完成后,它应该看起来像这样。

if (file_exists(__DIR__.'/controllers/Server.php')) {
	    Route::get('/deploy', 'Server@deploy');
    }

通过创新来完成 (Finish up by Getting Creative)

So there you have it! Just stage all your updated files, commit, push, and you’re ready to start deploying with a Git friendly workflow.

所以你有它! 只需暂存所有更新的文件,提交,推送,即可开始使用友好的Git工作流进行部署。

If you want to you can take this tutorial a step further. Just add http://localhost/deploy to your bookmarks for quick one-click deployment. You can even create a simple HTML form that posts to the page that allows you to pull specific branches. The possibilities are endless.

如果您愿意,可以进一步学习本教程。 只需将http://localhost/deploy添加到您的书签即可快速进行一键式部署。 您甚至可以创建一个简单HTML表单,将其发布到页面上,以允许您拉出特定的分支。 可能性是无止境。

Deploying with Git and Laravel has taken a boring and daunting task of deploying my websites, and made it fun again. It’s easy to set up if you understand Git, but it’s even easier to use.

使用Git和Laravel进行部署已经完成了部署我的网站的艰巨而艰巨的任务,并再次使其变得有趣。 如果您了解Git,就很容易进行设置,但是使用起来更容易。

Please share with me the creative ways you’re using to make website deployment easier!

请与我分享您用来简化网站部署的创造性方法!

翻译自: https://www.sitepoint.com/deploy-website-using-laravel-git/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值