A Laravel app is a web app framework with an elegant, expressive syntax. It furthermore attempts in removing the difficulty out of development through easing common tasks in the majority of web projects, which include authentication, routing, authentication, sessions, and caching. The demand for Laravel app development services continues growing.
Laravel aims to make the process of development pleasing for a developer without sacrificing the functionality of an app. A happy Laravel developer makes the best code. Laravel is accessible, but powerful and provides powerful tools for robust, huge apps.
Deploying a Laravel App with Docker and Alibaba Cloud
This guide helps you deploy a Laravel App using Docker and Alibaba Cloud. However, before you start you need the following:
- Docker installed on the local machine
- The composer that’s installed on your computer
First of all, you must have a Laravel app you could Dockerize. You can add a few files to the root of the app. Then, you must build an environment configuration file for a production environment, which we will call .env.prod. You may copy your current. env file, but don’t forget modifying values, such as set APP ENV to production. We would also require a configuration file for the webserver, create a vhost.conf file for the virtual host.
We would need a Dockerfile to build a container, and we’ll use a multi-stage build:
Furthermore, we also have to include several files and folders from the container, thus you must create a .dockerignore file:
Building a Repository in Alibaba Cloud Container Registry
On Alibaba’s Cloud Console, proceed to Products > Elastic Computing > Container Registry.
You must first set the password of the registry login.
We need to build a namespace, then we could create an app repository.
Make certain to set to Private the repository type, otherwise the repository would be accessible even without the password. Moreover, you could select your repository’s region.
The Container Registry supports as a code source GitLab, GitHub, and Bitbucket, which is extremely useful. If you’re going to use one of them, you could choose that, but to keep it simple, we’ll use the Local repository option in this tutorial.
On your local computer, you have to build the container and push it towards the registry. In the root of your Laravel app, you could run the following command to build the container.
When you manage on at the right side of the repository in the Container Registry, you could find the address of the repository and a guide about logging in the registry and pushing the image to the repository.
You should therefore run the commands below, but with your region, repository, and namespace.
After successfully pushing the image, you will see it under the Tags tab.
Building the VPC
On the Alibaba Cloud Console, proceed to Products > Networking > Virtual Private Cloud and then activate VPC. Choose the region from the top menu, and build a VPC and a VSwitch as well.
You should first enable RAM (Products > Monitor and Management > Resource Access Management) and then proceed to Products > Elastic Computing > Container Service. The Container Service supports Swarm and Kubernetes. We will now use Swarm; thus you should choose Swarm from the left menu.
Click the Cluster button on and then configure it.
When you’re done with the configuration, you can click the Create button and a confirm dialog would show up with the pricing information. When you’ve finished doing the cluster creation and you see it in the cluster click, you could then click the Manage on.
You have to log in to your private repository for access to images, thus click on the Log on to Hut button.
Deployment of the App
On the left menu on the Container Service control panel, click Applications on, and then click Create Application on. Set the name and the version, check the Pull Docker Image, and click on Create with an Image button.
Choose the image and a version. Under the Web Routing in the Network section, click on the blue plus symbol. Type 80 for the container port. What you want the Domain type to be called, you could set the number of instances as well. Select the image and the version.
Click on Create and then the View Application List. You should see the new container Under Applications. Click it on and in the services tab click on the name of the container once again. Now, you’ll see the information regarding the container. Click the Access Endpoint URL on and you’ll see the homepage of your Laravel application.
Connecting App to the MySQL and Redis
We’ll require an SQL database as well as a session database, thus we should set up the services and configure connections. Proceed to Products > ApsaraDB > or Redis. Choose the region where your VPC is from the top menu. Build an instance that fits your requirements.
Click manage on after making the instance and you see it in the instance list. From the left menu, choose Whitelist settings then modify to the 0.00.0/0 default value to enable connections from all VPC machines, or you could add the private IP/IP range of the ECS instances.
On the left menu, choose Instance information and then copy the Connection Address. Open the .env.prod file on the local machine and set the Redis host and password.
Similar to this:
Laravel requires a package named predis for interacting with Redis.
On the local machine, run the command below, which installs predis and adds to the list of dependency.
The Redis connection is now configured, but before we deploy a new container, we should also configure the MySQL connection.
Proceed to Products > ApsraraDB > ApsaraDB for RDS. From the top menu, choose the region where the VPC is located. Make an instance that fits your requirements. After building your instance and you see it in the Instance list, click manage on. Choose Security from the left menu and modify the whitelist in the same way we did with Redis.
You need to build a database account as well as a database and grant the user read-write access. Select Connection Options from the left menu, and copy the intranet address. You should, on the local machine edit the database connection settings in the .env.prod file. The same was as this:
Deploying the New App Version
Create the container and push to the registry:
You must run commands the same as these:
You should see the application in the list when you go to Container Service and click Update on. Change the value of the version field and change the yaml file image.
You can now connect to MySQL and Redis. If you wish to run a database migration, you could click the app on, in the services tab click the name of the app on-again, choose a container and then click on Web Terminal. You need to run these commands:
Add Domain Name to a Cluster
On the container service panel, go to Applications and click on the update on the right side of it. Add the domain name in the yaml file to the aliyun.routing1.port.80. To separate values, you may use semicolons. Aliyun,routinglport_80:laravel 1;yourdomain.com for instance.
Go to the cluster list in the container service control panel list and then click on manage at the right. When you click Load Balancer Settings on, you can now see the load balance ID. Proceed to Products > Networking > Server Load Balance, then copy the load balancer’s IP address and then add to the DNS records.
If you want to do so, you may add a TLS certificate to the load balancer.
Using the Object Storage Service
You likely would want to store files, and thus an Alibaba Cloud Object Storage Service is needed. You can make a bucket and connect the app to the service with the official libraries:
You’ve containerized and deployed a Laravel app successfully to the Container Service and configured a few Alibaba Cloud services to work together for a reliable and scalable app infrastructure.