How To Deploy Laravel Application On AWS EC2 ?
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 ?
- Login To Your Ubuntu Server Via SSH
ssh -i yourkey.pem ubuntu@yourip
- Install Apache And MySQL
sudo apt update && sudo apt upgrade -y
sudo apt install apache2
sudo apt install mysql-server
Configure MySQL.mysql_secure_installation
Complete the setup and create user and database.sudo mysql
CREATE USER 'your_username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR_PASSWORD';
FLUSH PRIVILEGES;
quit
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. - Install PHP
sudo apt install php
runphp-
if you need any specific. - Upload project using SSH
scp -i your_key.pem your_project.zip ubuntu@your_ip -v
Unzip usingsudo unzip your_project.zip -d /var/www/html/
if unzip is not available, you can install zip usingsudo apt install zip
Change the ownership towww-data
(Laravel recommended) usingsudo chown www-data:www-data -R /var/www/html/your_project
- Create Virtual Host Configuration
sudo vim /etc/apache2/sites-available/your_project.conf
Paste the following and make necessary changes
<VirtualHost *.80>
ServerName YOUR_DOMAIN_ADDRESS
DocumentRoot /var/www/html/your_project
<Directory /var/www/html/your_project/ >
AllowOverride All
Require all granted
Allow from all
</Directory>
</VirtualHost> - Enable Virtual Host Your Just Made
sudo a2ensite your_project.conf
Restart Apache2sudo service apache2 restart
- 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] - 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.