Setting Up AWS-EC2 For Laravel

Jad Jabbour
Life ‘n’ Tech
Published in
2 min readSep 2, 2016

7-Step guide to deploying your Laravel app on an EC2 instance.

In this post, I’ll go over the steps to prepare an EC2 instance for your Laravel app. I am assuming you had already set up the EC2 instance and launched it. You must also generate a key-pair and connect via SSH to your new EC2 instance. (https://aws.amazon.com/ec2/getting-started/)

Use your SSH tool of choice and connect to your new EC2 instance.

1- Installing apache, php, mysql and mods

  • sudo apt-get install apache2
  • sudo apt-get install mysql-server php5-mysql (remember the mysql password set here)
  • sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt
  • sudo apt-get install php5 php-pear
  • sudo apt-get install curl php5-curl php5-cli git
  • sudo a2enmod rewrite
  • sudo php5enmod mcrypt
  • sudo service apache2 restart

2- Getting the project from your repository

  • cd /var/www/html/
  • sudo git clone [PROJECT REPO LINK]
  • cd [PROJECT FOLDER]

3- Install & run composer

4- Create .env file & generating app key

  • sudo cp .env.example .env
  • sudo nano .env [Ctrl+O to save, Ctrl+x to exit](set config values)
  • sudo php artisan key:generate

5- Configuring apache

  • cd /etc/apache2/
  • sudo nano apache2.conf
  • Add to file[Ctrl+O to save, Ctrl+x to exit]:
    <Directory /var/www/html/[PROJECT FOLDER]/public>
    Options Indexes FollowSymLinks
    AllowOverride all
    Require all granted
    </Directory>
  • cd sites-enabled
  • sudo nano 000-default.conf
  • Add to file under <VirtualHost *:80>[Ctrl+O to save, Ctrl+x to exit]:
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/[PROJECT FOLDER]/public/
    ServerName [DOMAIN].[EXT]
    ServerAlias *.[DOMAIN].[EXT]
  • sudo service apache2 restart

6- Creating a database

  • cd
  • mysql -u root -p (enter mysql password when prompted) (NB: for the sake of this example, i am using root user for mysql, however it is much safer to create a mysql user specific to your app.)
  • create database [DATABASE NAME];[Ctrl+z when done]

7- Setting folder rights & running migrations

  • cd /var/www/html/[PROJECT FOLDER]
  • sudo chgrp -R www-data storage bootstrap/cache
  • sudo chmod -R ug+rwx storage bootstrap/cache
  • php artisan migrate
  • php artisan db:seed

By completing the above steps, you would have deployed your Laravel app on your new EC2 server.

In future posts I’ll go more in-depth regarding complex Elastic Beanstalk architectures for your Laravel solutions including setting up load balancers, connecting to an AWS-RDS server and other cool stuff.

--

--