Install Laravel on Amazon Linux 2

Joaquín Ruiz - 29th October 2019

Install Laravel on Amazon Linux 2
  • Date:   29th October 2019
  • Category:

      Software

  • Comments:

      0

Laravel is a free, open-source PHP web framework, intended for the development of web applications following the model–view–controller (MVC) architectural pattern and based on Symfony.

Some of the features of Laravel are a modular packaging system with a dedicated dependency manager, different ways for accessing relational databases, utilities that aid in application deployment and maintenance, and its orientation toward syntactic sugar.

Amazon Linux 2 is the next generation of Amazon Linux, a Linux server operating system from Amazon Web Services (AWS). It provides a secure, stable, and high performance execution environment to develop and run cloud and enterprise applications.

It is available as an Amazon Machine Image (AMI) for use on Amazon Elastic Compute Cloud (Amazon EC2). Amazon Linux 2 is also available as a Docker container image and as a virtual machine image. AWS provides ongoing security and maintenance updates for Amazon Linux 2.

Log-In to your EC2 instance

Te most common way to login to an EC2 instance is via ssh with a pem file. The username changes depending on your operative system:

  • Ubuntu: ubuntu
  • Amazon Linux: ec2-user
  • CentOS: root
ssh -i file.pem username@ip-address

Install Apache

Laravel needs a Web Server like Nginx or Apache, we choose Apache 2 for this example. To install Apache 2, we use YUM package management.

sudo yum install httpd -y

And then we launch the service to start Apache.

sudo systemctl restart httpd
sudo systemctl enable httpd

Install MySQL

For the database of Laravel, we choose MySQL. To install it, we get the RPM installer of the MySQL 5.7 to get latest version.

sudo yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

Then we install the rest of packages

sudo yum install mysql
sudo yum install mysql-server

To check that we have install the correct packages, we can type:

yum list installed | grep mysql

And then we launch the service to start MySQL daemon.

sudo systemctl start mysqld

Then, we need to provide a temporary password.

sudo grep 'temporary password' /var/log/mysqld.log

And finally, perform a Secure Installation to ensure the correct configuration and secure the MySQL service.

sudo mysql_secure_installation

Install PHP

And finally, we need PHP to interpret the Laravel code, to check the versions that Amazon Linux 2 brings by default we type on the console:

sudo amazon-linux-extras list | grep php

Then we enable PHP 7.3 typing

sudo amazon-linux-extras enable php7.3

And then we install the rest of libraries

sudo yum install php-cli php-xml php-json php-mbstring php-process php-common php-fpm php-zip php-mysqlnd git -y

Finally we can check if everything went right with

php --version

Install Composer

Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries. It is needed to install Laravel correctly.

To get composer, we need to type on the console:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/bin/composer
sudo chmod +x /usr/bin/composer

Install Laravel App

There are many ways to install the actual code of Laravel. I presume that you have the code on a git repository.

Then we need to go to the /var/www/ folder (apache or nginx folder) to download the code.

cd /var/www

And then, clone the code

git clone XXXX

And give the correct permissions to the folder. If you are not using Amazon Linux 2, you need to check your user:

  • Ubuntu: ubuntu
  • Amazon Linux: ec2-user
  • CentOS: root
sudo chown -R ec2-user:apache /var/www
sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;

Then we need to populate the environment file with our configuration

cp .env.example .env
nano .env

And finally, install the libraries with composer, and launch the migration to populate the database

composer install
php artisan migrate

Configure Vhost

And the last step (yes!) is to configure a virtual host to indicate Apache where is the code located. Here is an example with http://….

Edit the apache 2 configuration with a console editor (nano, vim…)

nano /etc/httpd/conf/httpd.conf

Or if you have multiple hosts, you can set the configuration on sites-available / sites-enabled.

This would be an example of valid configuration:

<VirtualHost *:80>
       ServerName subdomain.domain.com
       DocumentRoot /var/www/project/public
       <Directory /var/www/project>
              AllowOverride All
       </Directory>
</VirtualHost>

And then, restart Apache service and PHP-FPM service. FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features (mostly) useful for heavy-loaded sites.

sudo systemctl start httpd
sudo systemctl start php-fpm
sudo systemctl enable php-fpm

The Author

Joaquín Ruiz (aka Joki)
Joki is a Computer Engineer, Senior Full-Stack Developer and Lead Developer, specialized in web and e-commerce. Joki has also a wide international experience as freelance and working in agencies.