Making a horizontally scalable server infrastructure with Laravel Forge and AWS EC2 instances, that can deliver high performance traffic and execution.
I Need to make a server infrastructure that should be extensible and easy to manage, also that should give high performance on computing and requests.
If your application backend is php then you can easily consider forge.laravel.com to manage your hosting with different server providers like amazon, digital ocean, etc.
And if your application backend is with Laravel framework then it’s very easy to manage the servers and infrastructure with Laravel Forge. Laravel is a robust and scalable PHP framework, I am already working with it since last 2 years and very satisfied with it until now.
In this topic I will consider Laravel Forge and Amazon EC2 instances to make an extensible and flexible infrastructure that we can scale if your application requires. The benefit of making an extensible/scalable infrastructure is that we can scale the structure later on when we need, based on our application requirements.
How our infrastructure will look in a overview -
So from the diagram you can see, we have one load-balancer to redirect the traffic into two(2) application servers. The good thing of using a load-balancer is that we can add more application servers like app-1 and app-2 based on our application load. So in future we can add app-3, app-4 etc. This is how we can scale the application load. We have one database server, so all the app servers can access that. We also added a cache server where we will save the shared things for the app server, for example sessions, cache etc. Basically we will use Redis database for that into this d-cache server.
We also have one worker server named d-worker1, so as you can see I named it worker1 and if our application needs more execution power then we can add more worker servers like worker2, worker3 etc. basically we will dispatch JOBs into the worker servers. Worker server can also communicate with cache server and database server.
So in total we will need 6 servers and based on out future need we can add more servers into the application servers and worker servers, and that makes our infrastructure scalable horizontally.
Steps:
- Create the servers and make the connections required between the servers.
- Pointing the site domain to the load-balancer.
- Configure the Load-balancer.
- Setup the Application servers and install the app to the application servers.
- Configure the cache and worker servers.
6. Fine tune the php-fpm processes for better performance and remove unnecessary services from different servers.
Done :)
Lets start discussing the steps…
Step 1: Creating the servers
As we have already discussed above, that for this example, we will create couple of servers. Lists are below-
- 1 load-balancer
- 2 app server
- 1 cache server
- 1 worker server
- 1 database server
# Create load-balancer server:
We will create the servers from Forge. Login to forge and start creating the servers from the top. I will first create load-balancer. I will choose an aws instance that is very low, as this does not need lot of memory / cpu. I would suggest t3.small. In my situation I will choose c5.large as I wanted to give some Ram too, as my application needs to handle lot of requests.
All the servers have to be into the same region and same VPC, I am based on Europe so I have chosen Frankfurt and I already had a VPC so I chose that. Most importantly choose Provision As a Load Balancer.
Create 2 App servers:
Now we will create 2 app servers, I will choose t3.medium as App server will not do much except handling some web requests. Most of our heavy lifting work processes will be JOB and we will handle those by our worker servers. So I will start with t3.medium. If we see that we might need more requests to handle we can just add more application servers and that is how we are going to scale.
We have chosen the same region and VPC as like load-balancer. But here we need to choose what PHP version, and as forge now supports php 7.4 default so I will keep that. We don’t need to install any database into our application servers, so I will keep it none. Make the name app-2 instead of the screen shoot shows app-1 for the next server :) Make two servers like that app-1, app-2.
Creating the Database server:
Now for database I will give the most powerful server, as my database is very large and may need more memory and CPU. You can choose based on your requirements. Now I will choose database MySQL 8.0 and a database name default as forge.
Creating Worker server:
For worker server I will also give a powerful server. Worker server need more CPU and Ram. It depends on your own application. In my application most of the heavy lifting work is done by the worker server so I give a good server resource for that. This server does not need to have database as it will connect to our database server, name the worker server as d-worker1.
Create cache server:
This server only will have the Redis database, and only will use the memory, so I will only consider memory(RAM). I would prefer using more memory here than CPU or anything else.
Now we are done creating all the necessary servers. We just need to wait couple of minutes, so forge provisions them and then they are ready.
Do the connection:
Now our servers are individual servers and they can not connect to each other. We have to make the connection. Before doing so I will do one extra step here, that is giving some Elastic IP to the servers. Elastic IP is not that costly but very helpful later on, as they will not change if we stop the EC2 instances or change the instance later on for some reason.
Create some Elastic IPs and associate them to Load balancer and database server. Other servers are not that important if the public IP changed. We need the static public IP for database as we may need to connect to this database using 3rd party software like sequel Ace / pro. You have to do this step from the Aws console login, you can not do it from forge, but this is pretty easy. When you are done with association of Elastic IP, you have to also let the forge know about the IP’s. Just go to the Server’s and on the meta, change the new IP. For confirmation please check if forge can connect to the server by clicking on the button refresh status. If forge can connect then you are good to go.
STEP 2: Pointing the site domain to the load-balancer
Pointing the domain to the Load Balancer, Now we should setup the domain IP to the load balancer server IP address. I have setup an Elastic IP for my load balancer, and I will use that here. Once you have done that, your domain example.com will ping to the load balancer IP that you have just setup. In this moment, now it’s not doing anything and probably will show you a 404 page, which is alright as we didn’t setup anything yet.
Summary:
So up to now we have created all the necessary servers and made the required connections. We have also pointed our domain to the load-balancer. Now we have to install the applications and configure it. We will take that into the next article. If you are still interested please follow the next article.
Thanks for reading.