How To Deploy Laravel Application On AWS EC2 ?

Link Copied To Clipboard !

deploy_laravel_project_on_aws_ec2_ubuntu_apache2 Frameworks

Deploying a Laravel Application on a web-server is fairly an easy task. However, it can be a pain in the head just because you are missing some basic configurations. Laravel can be deployed in any type of server configurations which can be via cpanel or SSH Shell.

In this article, I am going to explain about the core laravel deployment concept and how you deploy laravel on Amazon EC2 instance.

How Does Laravel Deployment Work ?

Laravel uses its index.php file from public directory as root file. You can find that on your_project/public/ directory. The project directory and public directory also contain .htaccess file for rewrite rules and configurations. So, ideally, if you put your project directory in your web browser, provided the web server is running, it should automatically look for public/index.php and if that file is loaded successfully, your application gets deployed successfully. But, sometimes, you may have to add come configurations to your server and even modify some lines of project files.

How To Deploy Laravel Application On AWS EC2 ?

The steps for deploying laravel application on AWS EC2 Ubuntu Server are as follows ?

  1. Login To Your Ubuntu Server Via SSH
    ssh -i yourkey.pem ubuntu@yourip
  2. Install Apache And MySQL
    sudo apt update && sudo apt upgrade -y
    sudo apt install apache2
    sudo apt install mysql-server
    Configure MySQL.
    Complete the setup and create user and database.
    sudo mysql
    CREATE USER 'your_username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR_PASSWORD';
    mysql -u your_username -p
    CREATE DATABASE your_database_name;
    Import database if needed as per your requirement.
    Now, we have a running apache2 and ready to use database.
  3. Install PHP
    sudo apt install php
    run php- if you need any specific.
  4. Upload project using SSH
    scp -i your_key.pem ubuntu@your_ip -v
    Unzip using
    sudo unzip -d /var/www/html/
    if unzip is not available, you can install zip using
    sudo apt install zip
    Change the ownership to www-data(Laravel recommended) using
    sudo chown www-data:www-data -R /var/www/html/your_project
  5. Create Virtual Host Configuration
    sudo vim /etc/apache2/sites-available/your_project.conf
    Paste the following and make necessary changes
    <VirtualHost *.80>
    DocumentRoot /var/www/html/your_project
    <Directory /var/www/html/your_project/ >
    AllowOverride All
    Require all granted
    Allow from all
  6. Enable Virtual Host Your Just Made
    sudo a2ensite your_project.conf
    Restart Apache2
    sudo service apache2 restart
  7. Now, create .htaccess file on root project directory and paste the following:
    RewriteEngine on
    #Redirect trailing slashes if not a folder
    RewriteCondn %{REQUEST_FILENAME} ! -d
    RewriteCondn %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L, R=301]
    RewriteCondn %{Request_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
  8. Edit .env File and Make Necessary Changes

Now, you are good to go. Hope it worked for you.

Here are common problems that arise while deploying laravel application.

Why does the Virtual Host display directory tree instead of actual website even though it is configured and enabled ?

The probable cause to this problem is due to mode_rewrite . You have to write AllowOverride All in your virtual host configuration and execute sudo a2enmod rewrite . After doing these two, the problem should go away you should be able to see the actual website rather than just directory tree.

You May Also Like