Table of Contents
Are you struggling to find an easy tutorial on how to deploy Laravel application on AWS EC2? Then here is a blog for you to lessen your hustle! Follow this step-by-step guide to deploy your application on Amazon EC2.
You’d be learning about the following topics for this tutorial.
Make sure you have the following resources to use.
To host our Laravel app on Amazon EC2 we will be using the following technical stack.
Use the following command for logging in to your EC2 instance.
$ ssh -i “pemfilename” username@ipaddress
Unleash the power of AWS and elevate your Laravel app to new heights!
Hire AWS Developers who can craft robust infrastructure that fuels your success and witness your digital ambitions come to life.
First of all, run the command to update the libraries with the latest packages.
$ sudo apt-get update
Get superuser permissions.
$ sudo su
Using the previous variable, add the repository. Then, press enter to confirm.
$ add-apt-repository ppa:nginx/$nginx $ sudo apt install nginx
Once done with the installation, check NGINX version.
$ nginx -V
There are two ways to install a MySQL database.
Install MySQL in Ubuntu by running the below command.
sudo apt-get update sudo apt install mysql-server sudo mysql_secure_installation
Now, open the MySQL prompt.
sudo mysql
Once we are done with the installation process use the below command to configure the root account with an authentication password.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_your_password';
Flush the privileges (changes)
FLUSH PRIVILEGES;
For creating a MySQL database instance, go to the link below, there is a step-by-step guide: Create a MySQL DB instance and connect to a database using AWS RDS.
Since we are done with configuring our database let’s move towards the Laravel project setup. If you are familiar with the initial setup of laravel then feel free to skip the next section and jump to the deployment section.
Develop and Deploy with Bacancy!
Looking for top-notch developers with extraordinary problem-solving skills to accomplish your requirements? Contact us now and hire DevOps developer.
sudo apt update sudo apt install php-cli unzip cd ~
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php HASH=`curl -sS https://composer.github.io/installer.sig`
echo $HASH
Verify the installation script for the composer.
php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH')
{
echo 'Installer verified';
}
else
{
echo 'Installer corrupt'; unlink('composer-setup.php');
}
echo PHP_EOL;"
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer -v
Create the Laravel project
composer create-project laravel/laravel demo cd demo
Install composer in the preferable directory
composer install
Copy .env file from .env.example and add the following changes. In case you’re using RDS then you need to configure it with RDS creds.
cp .env.example .env DB_DATABASE=dabasename DB_USERNAME=root DB_PASSWORD=your_password DB_HOST=localhost DB_PORT=3306
Migrate the database
php artisan migrate
Run the server
php artisan serve
Now move your Laravel demo code to /var/www/html.
cd /demo /var/www/html
Moving towards the final step- change the Nginx configuration file as per your requirements to deploy the application.
sudo nano /etc/nginx/sites-available/default
After changing the default virtual host file, your file would look like something like this.
server {
listen 80;
server_name localhost;
root /var/www/html/demo/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Save the file and run the following commands to verify the Nginx syntax and restart Nginx for loading your changes.
sudo nginx -t sudo service nginx restart
Visit http://EC2_PUBLIC_IPADDRESS
So, this was about how to deploy laravel application on AWS EC2. What are you waiting for? Go try these steps and host your application as well! For more such cloud tutorials, feel free to visit the Cloud tutorials page and start learning! Write us back your suggestions and feedback; we’d love to hear from you!
Your Success Is Guaranteed !
We accelerate the release of digital product and guaranteed their success
We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.