Tutorial

How to Deploy Laravel on AWS EC2: A Step-by-Step Guide

Are you tired of developing your Laravel application on your local host? Do you want to see your application in the cloud? This might sound scary initially, but this tutorial will cover every setup you need to follow up to deploy Laravel on AWS EC2.

Learn how to scale to million users with AWS Laravel applications.

Now, let’s start deploying Laravel on AWS!

Laravel applications on the Cloud

Nowadays, Laravel is one of the most popular PHP frameworks in the world. Developers prefer to use Laravel rather than CodeIgniter, CakePHP or Yii framework because of its easy database integration, high customization, ease of development and templating, libraries and excellent documentation.

You can read our Laravel 5.6 vs Symfony 4: The Best PHP Framework Battle.

AWS offers us the best Cloud solutions and technologies to put out applications on the cloud, such as RDS (Relational Database Service), EC2 (Elastic Compute Cloud), S3 (Simple Storage Service, Cloudfront, AutoScaling, Classic & Application Load Balancers.

To deploy Laravel on AWS, we must have these resources ready to use:

Once we have these resources available, we can proceed to install our Laravel stack on our server.

Stack

  • Ubuntu 16.04
  • Nginx 1.14
  • PHP-7.2
  • PHP7.2-FPM

Steps to deploy Laravel on AWS

1. Login to your EC2 instance.
If you don’t know how to do it, check this blog to get help!

2. Update your libraries.
Let’s update our libraries to the latest packages available

Warning: if your server is new there won’t be any problem by updating libraries, if not, make sure you won’t break anything. You might update a package that is not compatible with your current stack.

$ sudo apt-get update
$ sudo apt-get upgrade

3. Install Nginx latest version.
Before that, let’s get superuser permissions.

$ sudo su

Let’s establish a variable for nginx latest version

$ nginx=stable

Now add the repository using the previous variable.

$ add-apt-repository ppa:nginx/$nginx

Press enter to confirm this addition.

Now let’s update our libraries again with latest Nginx version.

$ apt-get update

And last but not least, install Nginx.

And last but not least, install Nginx.

Tip:

Check Nginx version with the next command:

$ nginx -v

4. Install php 7.2 and php7.2-fpm
First of all, let’s install the next package.

$ apt-get install python-software-properties

Now add the repository.

$ add-apt-repository ppa:ondrej/php

Press enter to confirm this addition.

Now let’s update our libraries again with php7.2 version.

$ apt-get update

And finally, install php7.2 with php7.2-fpm as well.

$ apt-get install php7.2 php7.2-msql php7.2-mysql php7.2-fpm php7.2-xml php7.2-gd php7.2-opcache php7.2-mbstring

Additionally, install these libraries.

$ apt install zip unzip php7.2-zip

To verify your installation, check PHP version.

$ php -V

5. Install Laravel/Deploy your code.
In this step, you can send your app to your server using SCP, FTP or any protocol you want to deploy your code. In this tutorial, Laravel will be installed from zero.

Download composer

$ curl -sS https://getcomposer.org/installer | php

Move composer to make it executable.

$ mv composer.phar /usr/local/bin/composer

Let’s move to the directory where we want to install/move our Laravel.

$ cd /var/www

Now let’s install Laravel.

Note:

Skip this step if you already have your code.

$ composer create-project laravel/laravel test --prefer-dist

Set proper ownership for our directory.

$ chown -R www-data:www-data test/

And also set proper permissions

$ chmod -R 775 test/

6. Configure Virtual Host on Nginx.
Move to the sites-available folder on Nginx.

$ cd /etc/nginx/sites-available

7. Modify the virtual host.
Modify the default virtual host file with vim editor; the file should look like this:

$ vim laravelapp.conf

Copy and paste the next code.

server {
listen 80 default_server;
listen [::]:80 default_server;
 
root /var/www/test/public;

index index.php index.html index.htm index.nginx-debian.html;
 
server_name _;
 
location / {
try_files $uri $uri/ =404;
}
 
location ~ \.php$ {
include snippets/fastcgi-php.conf;

# With php-fpm (or other unix sockets);
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# With php-fpm (or other unix sockets);
}
}

Suggestion:

You can customize your virtual host as it fits your necessities, for example adding an SSL certificate to your site (checkout Why SSL certificate (HTTPS) is important for a website? or adding a redirect).

Now save and quit.

$ :wq

Let’s create a soft link to the virtual host from: file /etc/nginx/sites/available/laravelapp.conf to /etc/nginx/sites-enabled

$ ln -s  /etc/nginx/sites/available/laravelapp.conf /etc/nginx/sites-enabled

Tip:

Before doing any restart on Nginx, first check Nginx syntax. If syntax is ok, then is safe to perform a restart

$ nginx -t

Now restart Nginx, to load our changes.

$ service nginx restart

Now, go to your web browser and paste your public IP and you should be able to see your Laravel App!

Tip: If you don’t know your public IP, you can use this command to see the IP address quickly. Also, you can go to AWS dashboard and look for your public IP.

$ curl icanhazip.com

Note: If you deployed your custom app, make sure to replace on your .env file, RDS endpoint to enable database connection.

You might also like: How to upload files to Amazon s3 using Laravel

Do not forget:

Change your vendor directory. This folder should have 777 permissions. Take a look on the command to change this folder permissions:

chmod -R 777 vendor/

Tip:

If you have an issue with your virtual host by accessing the site, try to replace the location/ block (if you’re using apache2, this fix will not work for you):

location / {
try_files $uri $uri/ /index.php?$query_string;

}

What’s next?

Now that you’ve deployed/installed your Laravel app on AWS, you might be concerned about performance. If you want to scale up your app, you can use the Auto Scaling feature to enable high availability on your application. For example, if your app reaches a certain use CPU by having many visitors, another exact copy of the server will be launched. To deliver the traffic between the two (or as much as needed), we can also use the AWS service Load Balancer. So both servers will be registered on the Load balancer and traffic will be split in both servers.

Now that you’ve deployed/installed your Laravel app on AWS, you might be concerned about performance. If you want to scale up your app, you can use Auto Scaling feature to enable high availability on your application. For example, if your app reaches a certain use CPU by having many visitors, another exact copy of the server will be launched. To deliver the traffic between two (or as much as needed), we can also use the AWS service Load Balancer. So both servers will be registered on the Load balancer and traffic will be split in both servers.

You might also like: What is AWS ECS? – Run Docker in Production

Deploy Laravel on AWS FAQs

Why should I choose AWS EC2 for deploying my Laravel application?

AWS EC2 provides scalable and reliable virtual servers in the cloud, offering flexibility in terms of server configurations. With AWS, you can easily scale your infrastructure as your Laravel application grows. Additionally, AWS offers services that complement EC2, such as RDS for databases and S3 for storage, making it a comprehensive solution for hosting Laravel applications.

How to deploy Laravel project on AWS EC2?

Firstly, set up an EC2 instance with the desired specifications. Then, configure the server by installing necessary dependencies, such as PHP, Composer, and a web server like Nginx or Apache. Next, clone your Laravel project from version control and configure the environment variables. Lastly, ensure proper security measures, such as configuring firewalls and setting up SSL certificates for secure communication.

How can I automate the deployment process of my Laravel application on AWS EC2?

Automating the deployment process streamlines updates and reduces the risk of errors. One effective approach is to use tools like AWS Elastic Beanstalk or AWS CodeDeploy. These services allow you to define deployment configurations, manage rolling updates, and automate the deployment pipeline. Additionally, consider integrating a continuous integration/continuous deployment (CI/CD) tool like Jenkins or GitLab CI to automate testing and deployment stages, ensuring a smoother and more efficient deployment workflow.

Published by
DevOps Guy

Recent Posts

How to Implement AI Data Management In Your Business

Have you ever wondered how businesses easily process enormous volumes of data, derive valuable insights,…

6 days ago

Developing Cloud Applications: The Complete Guide for 2025

Discover the steps for developing cloud applications, from costs to cloud app deployment

2 weeks ago

Why Build an MVP in 2025 | Video

Imagine launching your product with just the core features, getting honest user feedback, and then…

4 weeks ago

How CDN Reduced AWS Costs For MEXICANA Airlines Web App

When a tight deadline is non-negotiable, every second counts! Here’s how we developed and launched…

4 weeks ago

Nearshore Software Development Company | Complete Guide

You may have considered hiring a nearshore software development company or services, but you still have doubts…

1 month ago

End to End Project Management: Complete Guide

End-to-end project management goes as far back as you can remember. Every project in history, even…

1 month ago