Install Laravel Site on AWS EC2 Ubuntu instance – 16.04/17.10/18.04 With Apache2 and Php7.2

Laravel is a very robust and widely used php open source framework. This framework comes with huge set of libraries for almost every use-case. It is a rapid application development framework to create quality business applications. It is a tough competition to other cms like code ignitor, fuel php, yii etc. Though it is great tool but it is bit heavy and learning curve is bit long than code ignitor. In this article we will be going to see – how to install laravel site on AWS EC2 Ubuntu instance – 16.04/17.10/18.04 With Apache2 and Php7.2.

STEP 1: Install Apache2

Run below commands to update system packages and install apache2:

sudo apt update
sudo apt install apache2

Stop and start apache service:

sudo systemctl stop apache2.service
sudo systemctl start apache2.service

STEP 2: Install PHP7.2 and dependencies

Install PHP7.2 with other required php extensions:

sudo apt install php7.2 libapache2-mod-php7.2 php7.2-mbstring php7.2-xmlrpc php7.2-soap php7.2-gd php7.2-xml php7.2-cli php7.2-zip

STEP 3. Edit PHP Settings as per Laravel recommendations

Open php ini file:

sudo nano /etc/php/7.2/apache2/php.ini

Make following edits to php.in file:

memory_limit = 256M
upload_max_filesize = 64M
cgi.fix_pathinfo=0

STEP 4: Composer Install

Install composer for laravel installation:

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Move to project root directory and install laravel through composer:

cd /var/www/html
sudo composer create-project laravel/laravel DemoProject --prefer-dist

Update Ownership of directory:

sudo chown -R www-data:www-data /var/www/html/DemoProject/

Update file and directory permissions:

find /var/www/html/DemoProject -type d -exec chmod 2775 {} \;
find /var/www/html/DemoProject -type f -exec chmod 0664 {} \;

STEP 5: Configure Virtualhost

Create virtualhost config file named as laravel.conf:

sudo nano /etc/apache2/sites-available/laravel.conf

Add below lines to the file:

<VirtualHost *:80>
ServerAdmin admin@webnlinux.com
DocumentRoot /var/www/html/DemoProject/public
ServerName webnlinux.com

<Directory /var/www/html/DemoProject/public>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable rewrite module for htaccess rewrites and redirects:

sudo a2enmod rewrite

STEP 6: Enable site and Restart Apache2

Enable laravel site on apache and then restart apache service:

sudo a2ensite laravel.conf
sudo systemctl restart apache2.service

That’s it you are done, you can now browse laravel site on DNS or ip of aws ec2 instance. You will be able to see default laravel page.

Add a Comment

Your email address will not be published. Required fields are marked *