Developers love automation - for every process between development and production, they crave a script to streamline their workflow. The same goes for deployment.
Push The process of finally building and deploying an application should be as simple as pressing the Deploy Now button, but that's not what happens most of the time. We ended up investing time and resources into configuring the server, setting up the environment, moving files that we didn't think were relevant to the production build, etc.
Some of us prefer to use FTP to manually send files to a server or push code to a GitHub repository, while others prefer to use a deployment tool to simplify the process. Laravel Forge is one such tool that makes PHP deployment a breeze.
Don’t let the Laravel brand name mislead you. In addition to Laravel, you can also use this service to host WordPress, Symphony, Statamic, or any other web project as long as it is PHP. Personally, I like Laravel Forge because it's simple and easy to get started with.
In this tutorial, I'll walk you through the steps of connecting Laravel Forge with AWS and explore the features it has to offer.
Laravel Forge allows you to spin up cloud servers and handle the deployment process using Git and some of the popular server providers available. The process is explained below:
First, you need to connect AWS or any other cloud provider to your Forge account. Next, link your source control (such as GitHub) to Forge. You will now be able to create the server. Install the source control repository on the server. Finally, press the deploy button. Easy, right?
Servers configured using Laravel Forge come with the following stack:
After creating the server, you can further configure it.
When you sign up, you can choose between the different plans they offer. I chose the $12/month Basic plan; however, you get a free trial that gives you access to everything on the list for five days.
After logging in, you will see something like the following.
You can choose from Digital Ocean, AWS, Linode and Vultr as service providers. Alternatively, you can use Forge with a custom VPC. As for source control, Forge supports GitHub, GitLab, and Bitbucket. In this tutorial, I'll discuss the basics of configuring AWS to work with Forge and GitHub for source control. Once completed, you will be able to create and configure any number of servers.
If you use another service provider on the list, you can skip this step and contact us later after we have configured AWS and Laravel Forge.
To set up Forge and AWS, you need to follow the following steps.
Log in to Laravel Forge and select AWS as the service provider. You will be asked for your access key ID (key) and secret access key (secret). You need to create a specific IAM user with a policy that provides sufficient access to Laravel Forge. IAM is Amazon's way of mapping permissions on each user so that you can revoke access if any issues arise.
Log in to the AWS console and create a new IAM user.
Give the user a meaningful name and check the Programmatic Access checkbox.
Set the correct permissions for the laravel-forge IAM user. Create new user groups as user groups are great for managing permissions. Now the natural question is, "What policies should the forge user have access to?" Although you can give it AdministratorAccess, you shouldn't.
If you want Forge to create and configure servers on your behalf, you need to add two policies:
Confirm the IAM account, on the next page you will find the access key and password.
Go to the Laravel Forge page and paste them there. That's it.
If you haven't already, please do so by connecting your GitHub/Bitbucket account to Forge. When you create a server, Forge adds the public key to your account. If you need to add a new service provider and/or update source control, you can find these options in your profile.
Go to the Create Server page to add a new server.
If you are using the AWS Free Tier, choose the t2.micro with 1GB RAM. As for the other settings, I will use the default values. These include databases MySQL and PHP version 7.2. You can customize the database name later. For the sake of simplicity, I decided not to use a load balancer. If you want to know the secrets of post-production, I've covered that at the end of this tutorial.
Creating the server may take up to five minutes. You will be given credentials for sudo access. Store them in a safe place so you can use them in the future. To see if things are working as expected, go to your server's IP address and you should see the output of phpinfo()
on your screen.
The interface you see after creating the server is the server management dashboard.
You can do a lot of things here, for example:
A lot of features are bundled in it. I've covered the important stuff in this tutorial. Let's start with site management. According to the Forge documentation:
Sites represent each "domain" on the server. Every newly configured server contains a "default" site; however, when you are ready to launch a production site, you should delete it and create a new site with a valid domain name. 块引用>As you can see, Forge has set up a default site for us. You can create any number of sites and route them to your subdomain. For the purposes of this tutorial, I'll stick with the default site. By default, the web directory is set to
/public
. This is how Laravel and most other web applications are configured.If you click on a specific site, you will see the site management interface. From here you can manage, deploy, and configure individual sites.
Site Management Interface
This is what the interface initially looks like.
You can install or install WordPress from a Git repository. For the purpose of this tutorial, I created a sample Contact Us application that you can fork into your account. You can specify project and branch names. Once completed, you should have control over deploying your application.
I'll take you through a quick tour of the options available.
Deploy now and deploy quickly
To deploy, you can deploy manually using the Deploy Now button. Alternatively, you can enable the Quick Deployment option, which automatically deploys your project when you push your code to the master branch of the selected GitHub repository.
Deployment script
The default deployment script pulls code from the repository, installs dependencies, starts the server, and runs migrations every time the application is deployed. This is the actual deployment script.
cd /home/forge/redmonark.com git pull origin laravelapi composer install --no-interaction --prefer-dist --optimize-autoloader echo "" | sudo -S service php7.2-fpm reload if [ -f artisan ] then php artisan migrate --force fiCopy after loginIf you need to tweak it and add extra stuff, you can.
Deployment Trigger URL
You can use it to integrate your application into third-party services or create custom deployment scripts. When a request is received for the URL, the deployment script is triggered.
Update repositories and branches
You can use these options if you need to update a branch or install a newer version of the same project on a different repository. If you are updating a branch, you may also need to update the branch name in the deployment script.
environment
Forge automatically generates environment files for applications. Some details, such as database credentials, are automatically added to the environment. However, if your application uses an API, you can safely place the API key in the environment. Even if you are running a generic PHP web application, you can access ENV variables using the
getenv()
method.Queue worker
Starting a queue worker in Forge is the same as running the
queue:work
Artisan command. Forge uses a process monitor called a Supervisor to manage queue workers so that processes remain running forever. You can create multiple queues based on queue priority or any other classification you find useful.SSL
过去,为网站提供 SSL 保护绝非易事且免费。 Forge 允许您安装现有证书,或者您可以从 LetsEncrypt 获取免费证书。它既快速又简单。如果您需要通配符子域的 SSL,您可以将免费的 Cloudflare 证书添加到 Forge。
回到服务器管理界面,我们有SSH密钥。
添加 SSH 密钥
虽然仪表板上提供了大多数可配置选项,但如果您需要连接到服务器,则应该使用 SSH 来完成。 SSH 是登录 VPS 的更安全的方式,并且提供比密码更多的保护。
要通过 SSH 访问服务器,您需要生成密钥对(如果尚未生成)。服务器将可以访问公钥,而私钥将驻留在您的主机中。然后,您可以使用该设置连接到服务器实例。
注意:从服务器管理仪表板添加的 SSH 密钥将特定于该服务器。如果您需要从现在开始自动将密钥添加到所有服务器,您可以从您的个人资料设置中添加它们。
要生成密钥对,请运行以下命令。
ssh-keygen -t rsaCopy after login系统会询问您几个问题,例如您想要存储密钥的文件以及用于提高安全性的密码。接下来,将 SSH 密钥添加到 ssh-agent。
ssh-add ~/.ssh/id_rsaCopy after login复制公钥并将其添加到 Forge 的 SSH 密钥列表中。
cat ~/.ssh/id_rsa.pub # Copy the output of this commandCopy after login配置 PHP 和 MySQL
您可以使用该界面来配置 PHP 和 MySQL。对于数据库,可用的选项包括:
- 创建新数据库。
- 添加新用户。
- 更新用户对数据库的访问权限。
- 更新 Forge 有关密码的知识。
确保在 .env 文件中填写更新的数据。
您可以配置以下 PHP 设置:
- 升级到最新版本的 PHP。
- 更改上传文件大小。
- 针对生产环境优化 OPCache,以便将编译后的 PHP 代码存储在内存中。
其他重要设置
这里我列出了一些其他可用的设置。
安排任务
您可以使用 Forge 的调度程序来安排重复任务或运行 cron 作业。如果您需要定期发送电子邮件、清理某些内容或运行脚本,则可以使用任务计划程序。默认情况下会创建一个任务,该任务每晚运行
composer self-update
。您可以尝试以您选择的频率安排新的会议。启动守护进程
守护进程是在后台进程中运行的计算机程序。 Laravel Forge 允许您启动守护进程并使用 Supervisor 来确保守护进程保持运行。如果守护进程由于某种原因崩溃,Supervisor 将自动重新启动脚本。
监控应用程序
Laravel Forge 内置了对工具的支持,这些工具可以通过收集有关资源(例如内存、CPU 时间和 I/O 操作)的数据来监控应用程序的性能指标。可用的工具有 Blackfire.io 和 Papertrail。要开始分析您的应用程序,您只需从第三方网站检索正确的凭据即可。
配置服务器网络和防火墙
如果您需要更新防火墙设置,则无需前往 AWS 控制台即可完成此操作。您可以从仪表板创建新的防火墙规则。如果您有使用相同提供商和区域配置的其他服务器,则可以设置服务器网络,以便它们可以轻松通信。
摘要
Laravel Forge 是一个令人难以置信的工具,它使部署变得轻而易举。它具有大量功能和易于使用的 UI,可让您轻松创建和配置服务器并部署应用程序。配置服务提供商后,您很可能不需要再次访问 AWS 控制台来管理服务器。
在本教程中,我介绍了使用 Laravel Forge 配置 AWS 的基础知识以及配置服务器和部署应用程序的步骤。我还讨论了 Forge 界面中几乎所有可用的功能。
对于那些刚刚开始使用 Laravel 或希望通过扩展来扩展您的知识、网站或应用程序的人,我们在 Envato Market 中提供了您可以学习的各种内容。
您是否有使用 Laravel Forge 或任何其他流行部署工具部署 PHP 应用程序的经验可以分享?让我们在评论中知道。
The above is the detailed content of Deploy PHP web applications using Laravel Forge. For more information, please follow other related articles on the PHP Chinese website!