Back to Course |
[NEW] Laravel Project: From Start to Finish

Deploy to Staging Server: AWS and Laravel Forge

The final step in this section is to actually show something to the client, so we need to deploy our project somewhere online.

At this point, we don't need a domain name, the goal is to allow the client to see the progress and click around.


Where/How We Will Deploy

We will deploy our project on a new AWS EC2 server instance, created via Laravel Forge.

Why AWS? For this specific course, I chose AWS over Digital Ocean or other providers, because it's the most useful in practice: AWS is a required skill in many Laravel job descriptions, so it would be beneficial for you to learn how to use it.

Why Laravel Forge? This one is debatable, but Forge is one of the fastest way to get from zero to a working server, without ever touching Terminal. If you want to save some money and have more time, you can build a server manually and configure it for Laravel projects, we have a separate course Deploy Laravel Project to AWS EC2: Step-By-Step

Notice: at the time of writing this, Laravel Cloud isn't officially released yet. Maybe will need to rewrite this in 2025.


Deployment: Create Server

To start, you need to create an account on AWS and on Laravel Forge, both will require credit cards. Unfortunately, there's no free option here.

On Laravel Forge, you need to add your AWS credentials, and then press Create Server. Select AWS in the list:

In here, we need to configure our server information: the most important things are:

  • Name: so you would recognize it later in the list
  • Server Size: t3.small is fine, at the moment price is $0.0208/hour, which is around $15/month. You may try a smaller size but with risk that it may not have enough resources.

Press Create Server. Then a Pop-Up will open up with Server Credentials. Save them somewhere safe!

After that, your server should be preparing:

Wait until this process is completed. You'll know as soon as you will see this page:


Creating a (Web)Site from GitHub on New Server

From here, for our testing purposes - we will click on default site and install a Git Repository: we need to fill in the LaravelDaily/linksletter in the Repository input field (beforehand you have to set up GitHub credentials in Laravel Forge) and select dev as a branch.

Once we fill our Repository and select a branch (in our case dev) - we should see it setting up:

Wait until it installs and shows a page like this:

Great, the server is ready and with our code pulled down from GitHub! Every server gets a public IP address automatically, so that will be the URL we will give to client. Without any domains, for now. It's just for testing, at the moment.

Now, we should be able to visit our public IP address and see the Laravel website:

But as if we visit Log In page, we see the error:

So, our project was deployed with errors. We need to modify the default Laravel Forge deployment script.


Forge Deployment Script

Open up Deployments in the sidebar and look for Deployment Script:

Default script looks like this:

cd /home/forge/default
git pull origin $FORGE_SITE_BRANCH
 
$FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader
 
( flock -w 10 9 || exit 1
echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock
 
if [ -f artisan ]; then
$FORGE_PHP artisan migrate --force
fi

But it's lacking a lot of things. For example, npm commands. So let's fix that, also adding php artisan down and php artisan up to put the application in maintenance mode while deploying.

cd /home/forge/default
 
if [ -f artisan ]; then
$FORGE_PHP artisan down
fi
 
git pull origin $FORGE_SITE_BRANCH
$FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader
 
npm install
npm run build
 
rm -rf /node_modules
 
if [ -f artisan ]; then
$FORGE_PHP artisan cache:clear
$FORGE_PHP artisan view:clear
$FORGE_PHP artisan route:cache
$FORGE_PHP artisan config:cache
$FORGE_PHP artisan view:cache
fi
 
( flock -w 10 9 || exit 1
echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock
 
if [ -f artisan ]; then
$FORGE_PHP artisan migrate --force
$FORGE_PHP artisan up
fi

Once we are done, hit Update and then Deploy Now. This should make another deployment with our new script.

Once it is done - we can try to load the page:

And it works! Because we compiled our assets, there is no more 500 | Server error on our screen.

From here, every time you want to deploy the latest changes from dev branch, you can just go to the Forge page and click "Deploy now". Or, you can even specify that deployment would happen automatically, as soon as you push changes to dev directly or via merged Pull Request.


Queues

The final thing: setting up Queues. Remember, we had a queued Job in our application. This is another thing where Forge can help. So let's setup a Queue worker:

Visit Queue in the sidebar:

Change connection to database since for now we are using the database driver.

Everything else remains the same for our default page. Press Create.

This should load a page that looks like this:

That's our active queue worker!


At this stage, we are done and the application is ready for our development needs. We can send the IP to the client and wait for their feedback!

That's it for the current section of this course so far. We're currently working on the next sections, hoping to release them by mid-December. Come back soon!