Tag Archives: aws

How-to: Setup Laravel 5 on Amazon Linux (w/ PHP5.4 and Apache2.4)

After playing around with node for a couple of years I’ve recently decided to switch back to PHP for a project and have chosen to learn Laravel. It looks sleek, is apparently pretty easy to learn and has lots of features.

It’s 2015 though and I couldn’t find a simple install guide for my scenario: AWS EC2 using Amazon Linux. I eventually found this guide and this guide, and this one which together with my limited knowledge and some troubleshooting became my setup guide below to building my standard Laravel base build. You’ll end up with Amazon Linux with Laravel 5, PHP 5.4 and Apache 2.4.

1. Prep the linux packages, start Apache and set it to autostart.

sudo yum -y update
//sudo ln -sf /usr/share/zoneinfo/Australia/Sydney/etc/localtime
sudo yum install -y gcc make gcc-c++
sudo yum install -y git
sudo yum install -y httpd24 php56 php56-devel php56-mysql php56-pdo php56-mbstring php56-mcrypt
sudo yum install -y php-pear
sudo pear install Log
sudo service httpd start
sudo yum install -y php56-pecl-apc
sudo chkconfig httpd on
sudo service httpd restart

2. Check that you have PHP 5.6 and Apache 2.4, check phpinfo() for loaded modules.

check versions:
  php -v
  httpd -v
sudo chown -R ec2-user /var/www
cd /var/www
vim html/info.php and enter:
<?php phpinfo(); ?>
save and exit, then:
  sudo service httpd restart
then browse to your website URL + /info.php, read loaded modules (use find) and you should see mod_rewrite in the list. If not, you need to enable it.

3. Install Laravel and add a route.

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

(in /var/www) Download and extract Laravel:
  wget https://github.com/laravel/laravel/archive/master.zip
  unzip master.zip && cd laravel-master/ && mv * ../ && cd ..
  rm -r laravel-master && rm master.zip
sudo vim /etc/httpd/conf/httpd.conf and:
  find "AllowOverride None" (its there two times with capitals) and change them all to AllowOverride All
  find "DocumentRoot /var/www/html" and "<Directory /var/www/html" and change both paths to /var/www/public
Save and exit, then:
  sudo service httpd restart
composer install
sudo chmod -R 777 storage
sudo service httpd restart
Browse to your URL, you should see a Laravel 5 welcome image hooray!
One simple change to prove its working, vim app/Http/routes.php and under "Route::get('home'..." enter:
Route::get('/mytest', function()
{
    return "Oh yeah, this really works !";
});  
save and exit, then:
  sudo service httpd restart
Then browse to your URL + /mytest and you should see "Oh yeah, this really works !"
Now you can go explore Laravel ;)

Installing Redis as a service for the session db, and adding AWS RDS (MySql) for the database. The latest ‘Stable’ Redis at time of writing is 3.0.1.

wget http://download.redis.io/releases/redis-3.0.1.tar.gz
tar xzf redis-3.0.1.tar.gz
cd redis-3.0.1
make

sudo mkdir /etc/redis /var/lib/redis
sudo cp src/redis-server src/redis-cli /usr/local/bin
sudo cp redis.conf /etc/redis

sudo vim /etc/redis/redis.conf
[..]
daemonize yes
[..]
[..]
bind 127.0.0.1
[..]
[..]
dir /var/lib/redis
[..]

wget https://raw.github.com/saxenap/install-redis-amazon-linux-centos/master/redis-server

sudo mv redis-server /etc/init.d
sudo chmod 755 /etc/init.d/redis-server
sudo vim /etc/init.d/redis-server

redis="/usr/local/bin/redis-server"

sudo chkconfig --add redis-server
sudo chkconfig --level 345 redis-server on

cd ..
rm -rf redis*

composer require predis/predis

vim config/cache.php

 'default' => env('CACHE_DRIVER', 'redis'),

vim config/session.php

  'default' => env('CACHE_DRIVER', 'redis'),

//first, add a user record to db and make a new page to login auth::attempt
As a test update our /mytest to this:
Route::get('/mytest', function()
{

    if (Auth::check())
    {
      // The user is logged in...
      return "Good golly, you're logged in !";
    }

    return "Dagnamit, you're not logged in buddy !";
});

sudo service restart

Everything should work... is now using Redis for cache and session.

This exact process works flawlessly for me as at 02/06/15 using the standard Amazon Linux PV image and a t1.micro instance, hopefully it does for you too.