DEV Community

Cover image for Laravel Deployment on Elastic Beanstalk
Youssef Selem
Youssef Selem

Posted on

Laravel Deployment on Elastic Beanstalk

so i was trying to deploy Laravel web application on AWS elastic beanstalk, although it appeared to be easy it wasn't. some obstacles were hidden or unanswered whether i should upload all project files ,how to connect to this instance, where to begin and many more questions. I've found answers for most of these questions,before i get started special thanks to the youtube channel ProCoder it helped me a lot.

Note: this tutorial is for Linux users especially Ubuntu

Requirements:

  • stable project on your localhost
  • AWS account , you can use the free tier

Create a new empty project

on your AWS console choose elastic beanstalk and create new project without adding anything from your project

Alt Text

select web server environment, then you will be in Application page

write your application info

Alt Text

write your environment name , domin and descrption

Note: by default AWS will choose them for you

Alt Text

from platform drop down menu choose PHP and the rest will be chosen automatically

Note: feel free to change both the platform branch and platform version

Alt Text

choose sample application and click on configure , scroll down till you find Database click on edit

Alt Text
Alt Text

in this section the most important part is the username and password, save your settings and create your instance

Alt Text

Note: if your AWS account is free stick to db.t2.micro instance

In your laravel project

Edit database configuration

open your database config file /config/database.php now edit and make three global variables

define('RDS_HOSTNAME', $_SERVER['RDS_HOSTNAME']);
define('RDS_USERNAME', $_SERVER['RDS_USERNAME']);
define('RDS_PASSWORD', $_SERVER['RDS_PASSWORD']);
define('RDS_DB_NAME', $_SERVER['RDS_DB_NAME']);

now scroll down to connections and edit it to be something like that

'mysql' => [
            'driver' => 'mysql',
            'host' => RDS_HOSTNAME,
            'port' => env('DB_PORT', '3306'),
            'database' => RDS_DB_NAME,
            'username' => RDS_USERNAME,
            'password' => RDS_PASSWORD,
        ]

Zip your files

this part is a little bit tricky cause if you didn't notice the files you're zipping it might affect your server,also make sure you're not adding any hidden folders. your files should be something like that:

  • app
  • bootstrap
  • config
  • database
  • hooks
  • public
  • resources
  • routes
  • storage
  • tests
  • .editorconfig
  • .env
  • .env.example
  • .gitattributes
  • .styleci.yml
  • artisan
  • composer.json
  • composer.lock
  • package.json
  • package-lock.json
  • phpunit.xml
  • server.php
  • webpack.mix.js
Note: i've added the .env file to the file tree to make it easier but feel free to remove it

Upload zipped file

now open your elastic beanstalk tab ,choose the environment that you'd like to deploy to , then click on the upload and deploy button,choose your file give it a label and press deploy.

Web app route

on the left-side of your environment page click on configuration,then edit your software section,now change document root to be /public and apply the changes

Note: if your application need to migrate data and/or other installation don't worry continue to the SSH section

Add key pair to your elastic beanstalk

this step is very important because it will help you connect with your EC2 instance from anywhere of course by using your key pair file(.pem)

If you haven't made a key pair yet, make one by clicking Key Pairs below Security Group in the EC2 tab
  • In the AWS console, open the Elastic Beanstalk tab
  • Select Environment
  • Select Configurations in left panel
  • Select Security
  • Under "EC2 key pair:", select the name of your key pair in the Existing Key Pair field

open your env using SSH

open EC2 tab then choose running instance and click on connect ,follow the instruction page about accessing your instance and you'd be online

Install composer in your instance

sudo curl -sS https://getcomposer.org/installer | sudo php
sudo mv composer.phar /user/local/bin/composer
sudo ln /user/local/bin/composer /user/bin/composer

Open current version

first give the current user permission to write

sudo chown -R ec2-user /var/app/current/
Note: ec2-user is the name of your ec2 by default
cd /var/app/current/
composer dump-autoload

now all your artisan commands will work perfectly fine

php artisan migrate

change your storage file permission so that the server can access it

sudo chmod -R 0777 storage
sudo chmod 777 storage/logs/laravel.log
sudo chmod 777 storage/framework/cache

now everything is working perfectly fine you can run your own artisan commands

Top comments (15)

Collapse
 
robotys profile image
Robotys

If you are using git for the laravel code, you might want to use elastic beanstalk cli tools. Then you can deploy the changes immediately with eb deploy command.

Collapse
 
yousseffarag96 profile image
Youssef Selem

Actually i did that after deployment 😅

Collapse
 
brnathanlima profile image
Nathanael Lima

I have the same problem

Collapse
 
safventure11000 profile image
Josafe Balili • Edited

I was able to fix it. This is what I did.

cd /etc/nginx/conf.d/elasticbeanstalk - Navigate to the php.conf file
sudo nano php.conf - open the file

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

Insert the following after index and before location, then save and restart.

Alt Text

There has been a change in Elastic beanstalk. It is now configured to nginx instead of apache so the .htaccess file no longer works. All laravel routes are getting 404 Not Found Nginx error except for the landing page.

Thread Thread
 
brnathanlima profile image
Nathanael Lima

Thank you for your answer but it didn't work...

Thread Thread
 
safventure11000 profile image
Josafe Balili • Edited
Thread Thread
 
brnathanlima profile image
Nathanael Lima

Tried both but didn't work :/

Anyway, I created a brand new Laravel app and pulled a repo on it called rennokki/laravel-aws-eb and I was able to deploy without any problem. There might be something wrong in my app, not in the deployment process itself. Since it's still a small app I think that I'll recreate it...

Thanks for trying to help!

Thread Thread
 
safventure11000 profile image
Josafe Balili

nice :)

Collapse
 
jmpps profile image
JM PPS

Sorry but this is so wrong.
You should never "log in" and install anything on an ec2 instance that is built by Elastic Beanstalk.

The configuration of your elastic beanstalk environment should be done using the .ebextensions and .platform directories. Look it up.

Otherwise, the moment you do an update or scale out and EB build a new instance, you have to log in and do it all over again.

You can also set all your settings as environment variables in the elastic beanstalk config.

Collapse
 
safventure11000 profile image
Josafe Balili

Please let me know if you are able to fix this. I also have the same problem. thanks :)

Collapse
 
usamahusnain profile image
usama-husnain

i am facing this issue when running php artisan migrate:

SQLSTATE[HY000] [2002] Connection timed out (SQL: select * from information_schema.tables where table_schema = ebdb and
table_name = migrations and table_type = 'BASE TABLE')

please help anyone

Collapse
 
mo2menelzeiny profile image
Mo'men El-Zeiny

Awesome guide man!
though, whats so special about Elastic Beanstalk anyways?

Collapse
 
yousseffarag96 profile image
Youssef Selem

It connects all the services in one place..think about it as the boiler plate

Collapse
 
williamabbott profile image
williamabbott

If you have more than one instance you will need a centralized cache, and not running separately on each instance as they will become out of sync === different content on different instances.

Collapse
 
jayroy777 profile image
Jay Roy

Thanks, man your explanation solved my problem.