Deploy PHP web applications with Laravel Forge

Admin   Laravel   60  2020-07-16 10:53:46

Developers love to automate everything - for every process between development and production, they want a script that makes their workflow easier. This is also the case for deployment.

The process of pushing the final build and application deployment must be as easy as pressing the Deploy button now, but that's not what happens most of the time. We end up investing our time and resources in server configuration, environment settings, migration of files that we deem unrelated to production builds, etc.

Some of us prefer to send files to the server manually using FTP or have the code pushed into a GitHub repo, while others prefer a deployment tool to make the process easier. . One tool that makes PHP deployment easy is Laravel Forge.

Don't let the Laravel brand name fool you. In addition to Laravel, you can use this service to host WordPress, Symphony, Statamic, or any other web project as long as PHP. I like Laravel Forge because of its simplicity and ease of getting used to it.

In this tutorial, I will walk you through the steps to hook Laravel Forge with AWS and explore what it has to offer.

Overview

Laravel Forge allows you to rotate cloud servers and handle deployment processes with Git and many popular server providers available. The process is explained below:

First, you will need to connect AWS or any other cloud provider to your Forge account. Next, link your source control like GitHub to Forge. You can now create the server. Install your source control repository on the server. Finally, press the deploy button. Easy enough, right?

Servers licensed with Laravel Forge are included with the following stack:

    Ubuntu 16.06
    Nginx
    PHP 7,2 / 7.1 / 7.0 / 5.6
    MySQL / MariaDB / Postgres
    Redis
    Memcached

Once the server has been created, you can configure everything else.

When you sign up, you can choose between the different packages they offer. I chose the basic plan $ 12 / month; however, you get a free trial with access to everything on the list for five days.

Once you are logged in, you will see something like this below.

Deploy PHP web applications with Laravel Forge

You can choose between Digital Ocean, AWS, Linode, and Vultr for service providers. Alternatively, you can also use Forge with custom VPC. For source control, Forge supports GitHub, GitLab, and Bitbucket. In this tutorial, I will discuss the basics of configuring AWS to work with Forge and GitHub for source control. Once you're done, you should be able to create and provision any number of servers.

If you are using another provider in the list, you can skip this step and catch up with us later, after we have configured AWS and Laravel Forge.

Set up Laravel Forge and AWS

To set up Forge and AWS, here are the steps you need to take

1. Log in to Laravel Forge

Log into Laravel Forge and select AWS as the service provider. You will be asked to enter your key ID (key) and secret access key (secret). You will need to create a specific IAM user with a policy that provides sufficient access to Laravel Forge. IAM is Amazon's mapping of each user so you can revoke access if something goes wrong.

2. Create new IAM users

Log in to AWS Console and create new IAM users.

Deploy PHP web applications with Laravel Forge

Create an IAM user on AWS with programmatic access

Give users a meaningful name and check the box that says Programmable Access.

3. Select the appropriate policy

Set appropriate permissions for fake IAM Laravel users. Create new user groups because ideal user groups to manage permissions. Now the natural question is,

Create a user group with the right permissions

Deploy PHP web applications with Laravel Forge

If you need Forge to create and provision a server on your behalf, you will need to add two policies:

  1.     AmazonEC2FullAccess
  2.     AmazonVPCFullAccess

4. Save authentication information and confirm

Verify your IAM account and, on the next page, you will find the Access Key and Secret Code.

Deploy PHP web applications with Laravel Forge

The newly generated Access key and secret

Go to the Laravel Forge page and paste them there. That's it.

5. Link your GitHub account with Forge

Connect your GitHub / Bitbucket account to Forge if you have not already done so. Forge will add the public key to your account when you create the server. If you need to add new service providers and / or update source control, you have those options on your profile.

Create a new server

Visit the Create server page to add a new server.

Laravel Forges interface for adding a new server

Deploy PHP web applications with Laravel Forge

Choose t2.micro with 1 GB RAM if you're at the free AWS level. For the other settings, I will go with the default values. This includes MySQL for databases and PHP version 7.2. You can customize the database name later. To keep things simple, I decided not to use load balancing. If you're wondering about post-production formulas, I mentioned the end of this tutorial.

It may take up to five minutes for the server to be created. You will receive login credentials for Sudo access. Store them in a safe place so you can use them in the future. To see that everything is working as expected, go to the server's IP address and you will see the output of phpinfo() on your screen.

Server management interface

The interface you see after creating the server is the server management console.

Deploy PHP web applications with Laravel Forge

Server management dashboard

You can do a lot of things here, such as:

  1.     website management
  2.     add SSH keys
  3.     database configuration
  4.     Update PHP settings
  5.     schedule a task
  6.     Start a daemon
  7.     Network management and firewall configuration
  8.     Monitoring application using Blackfire or Papertail
  9.     Configure meta settings

That are a lot of features that come in there. I have covered the important things in this tutorial. Let's start with website management. According to Forge documentation:

The website represents each site "Default" website is included in each new licensed server; however, you should delete it and create a new page with a valid domain name when you are ready to launch your production site.

As you can see, Forge has set a default site for us. You can create any number of websites and route them to your subdomain. For the purposes of this tutorial, I will stick to the default page. The web directory is set to / public by default. This is how it will be configured for Laravel and most other web applications.

If you click on a specific website, you will see the site management interface. You can manage, deploy, and configure individual sites from here.

Website management interface

This is the original interface of the interface.

Deploy PHP web applications with Laravel Forge

You can add the Git repo from this screen

You can install it from the Git repository or install WordPress. For the purpose of this tutorial, I created a Contact Us form application that you can fork into your account. You can specify the name of the project and branch. When you're done, you will have controls to deploy your application.

Deploy PHP web applications with Laravel Forge

Site management dashboard with deployment controls

I will give you a quick tour of the options available.

Deploy now and deploy quickly

For deployment, you can deploy manually using the Deploy now button. Alternatively, you can enable the Quick Deploy option, which will automatically deploy the project when you push the code into the main branch of the selected GitHub repo.

Website management interface

This is the original interface of the interface.

You can add the Git repo from this screen

You can install it from the Git repository or install WordPress. For the purpose of this tutorial, I created a Contact Us form application that you can fork into your account. You can specify the name of the project and branch. When you're done, you will have controls to deploy your application.

Site management dashboard with deployment controls

I will give you a quick tour of the options available.

Deploy now and deploy quickly

For deployment, you can deploy manually using the Deploy now button. Alternatively, you can enable the Quick Deploy option, which will automatically deploy the project when you push the code into the main branch of the selected GitHub repo.

Deployment script

The default deployment script pulls code from the repository, installs dependencies, starts the server, and runs migration every time the application is deployed. This is the actual deployment scenario.

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
fi

If you need to tweak it and add something extra, you can.

Activation activation URL

You can use this to integrate your application into third-party services or create custom deployment scripts. When the URL receives a request, the deployment script is activated.

Updated Repo and Branch

If you need to update a branch or install a newer version of the same project on another repository, you can use these options. If you are updating the branch, you may have to update the branch name in the deployment scenario.

Environment

Forge automatically creates an environment file for the application. Some details such as database logins are automatically added to the environment. However, if the application uses the API, you can securely 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.

Workers line up

Starting a worker queuing up in Forge is like running a queue: Artisan work command. Forge manages queue workers by using a process monitor called Monitoring so that the process continues to run permanently. You can create multiple queues based on queue priority or any other classification you find useful.

SSL

SSL security for a website is anything but easy and free in the past. Forge allows you to install an existing certificate or you can get a free certificate from LetsEncrypt. It is quick and easy. If you need SSL for wildcard subdomains, you can add a free Cloudflare certificate to Forge.

Back at the server management interface, we have the SSH keys.

Add SSH keys

Although most of the configuration options are available on the control panel, if you need to connect to the server, you should do it using SSH. SSH is a safer way to log in to a VPS and provide more password protection.

To access the server via SSH, you need to generate a key pair if you don't already have one. The public key will be accessed on the server and the private key will be in your host. You can then use the settings to connect to the server instance.

Note: The SSH key added from the server management dashboard will be specific to that server. If you need to automatically add keys for all servers from here, you can add them from your Profile settings.

To generate the key pair, run the following command.

  1. ssh-keygen -t rsa

You will be asked a few questions like the file you want to store the key and the passphrase for additional security. Next, add the SSH key to ssh-agent.

  1. ssh-add ~/.ssh/id_rsa

Copy the public key and add it to the list of Forge SSH keys.

  1. cat ~/.ssh/id_rsa.pub  # Copy the output of this command

Configure PHP and MySQL

You can use the interface to configure PHP and MySQL. For databases, the available options include:

  1.     Create a new database.
  2.     Add a new user.
  3.     Update user access to the database.
  4.     Update Forge's knowledge about passwords.

Make sure you fill in the updated data with your .env file.

You can configure the following PHP settings:

  1.     Upgrade to the latest PHP version.
  2.     Resize uploaded files.
  3.     Optimize OPCache to produce so that the compiled PHP code will be stored in memory.

    
Other important settings

Here I have listed some other settings available.

Plan a task

You can use Forge's scheduler to schedule recurring tasks or run cron jobs. If you need to periodically send emails, clean up something, or run scripts, you can use the task scheduler. Tasks are created by default, run automatically compose on a nightly basis. You can try scheduling a new one as often as you choose.

Start a Daemon

A daemon is a computer program that runs in a background process. Laravel Forge allows you to start a daemon and use the Supervisor to ensure that the daemon is still running. If the daemon hangs for some reason, Monitoring will automatically restart the script.

Application monitoring

Laravel Forge has built-in support for your application monitoring tools to perform measurements by collecting data about resources such as memory, CPU time, and I / O activities. Available are Blackfire.io and Papertrail. To start profiling your application, you simply need to retrieve the appropriate credentials from the third party website and that's it.

Configure network server and firewall

If you need to update your firewall settings, you don't need to go to the AWS console to do that. You can create new firewall rules from the dashboard. If you have other servers that are licensed to use the same provider and region, you can set up a server network so they can communicate painlessly.

Summary

Laravel Forge is an incredible tool for deploying a piece of cake. It has a lot of features and an easy-to-use user interface that allows you to create and provision servers and deploy applications without any hassle. Once you have configured the service provider, chances are you will not need to access the AWS console to manage the server again.

In this tutorial, I covered the basics of configuring AWS with Laravel Forge and the steps to provision a server and deploy an application. I also discussed nearly all the features available in the Forge interface.

For those of you who are either just getting started with Laravel or looking to expand your knowledge, website, or application with extensions, we have a lot of things you can learn at. Envato Market.