DEV Community

Cover image for Laravel Hosting: Deploy Laravel Easily on AWS Cloud
anmollohana
anmollohana

Posted on

Laravel Hosting: Deploy Laravel Easily on AWS Cloud

Laravel is a well-known PHP framework increasingly used for PHP projects, due to its extensive features and tools, which provide a streamlined development experience, developers frequently like Laravel.

For Laravel-powered apps, the Laravel hosting solution, regarded as the project's performance's lynchpin, presents a substantial impediment. For instance, developers' stunning performance-focused combo of Laravel on AWS is very well-liked for its features and excellent benchmarks.

Developers enjoy the battle since the combination of Laravel with AWS is so well-liked because it results in reliability and high-quality hosting alternatives. Laravel operates faultlessly and gives applications a flawless online experience on AWS. However, deploying Laravel on an Amazon EC2 hosting server is not recommended for everyone due to the difficulties involved.

This post will walk you through each step of hosting Laravel 8.0 on an Amazon Cloud server.

Laravel Applications on AWS Cloud

The leading cloud technologies and solutions are provided by AWS, including RDS (Relational Database Service), EC2 (Elastic Compute Cloud), and S3 (Simple Storage Service, Cloudfront, AutoScaling, Classic, and Application Load Balancers).

We will require the following resources to deploy Laravel on AWS:
● VPC
● RDS
● EC2 Instance

Once we have these resources, we can install our Laravel stack on our server.
Stack:
● Ubuntu 16.04
● Nginx 1.14
● PHP-7.2
● PHP7.2-FP

Step by Step Deployment of Laravel on AWS Cloud

*1. Login to access your EC2 instance. *
If you're unsure how to accomplish it, look at this blog for instructions!

*2. Refresh your library. *
Let's update our libraries with the most recent packages.

Warning: If your server is brand new, updating libraries will not cause any issues; if it isn't, make sure you don't break anything. You could be updating a package that is incompatible with your existing stack.

$ sudo apt-get update
$ sudo apt-get upgrade

*3. Install the newest version of Nginx. *
Let's start with superuser permissions.

$ sudo su

Let's create a variable for the most recent version of nginx.

$ nginx=stable

Using the previous variable, add the repository.

$ add-apt-repository ppa:nginx/$nginx

To confirm this addition, press enter.
Let's now upgrade our libraries to the most recent Nginx version.

$ apt-get update

Finally, Nginx must be installed.
Use the following command to check Nginx's version:

$ nginx -v

*4. PHP 7.2 and php7.2-fpm must be installed. *
Let's start by installing the next package.

$ apt-get install python-software-properties

Add the repository now.

$ add-apt-repository ppa:ondrej/php

To confirm this addition, press enter.
Let's now update our libraries to version php7.2.

$ apt-get update

Finally, install php7.2 along with php7.2-fpm.

$ apt-get install php7.2 php7.2-msql php7.2-mysql php7.2-fpm php7.2-xml php7.2-gd php7.2-opcache php7.2-mbstring

Install these libraries, as well.

$ apt install zip unzip php7.2-zip

Check the PHP version to ensure your installation is correct.

$ php -V
*5. Install Laravel and deploy your application. *
In this phase, you can upload your software to your server over SCP, FTP, or any other protocol you desire. Laravel will be installed from scratch in this tutorial.

$ curl -sS https://getcomposer.org/installer | php

Make composer executable by moving it.

$ mv composer.phar /usr/local/bin/composer

Let's get to the location where we'll install or move Laravel.

$ cd /var/www

Let's get Laravel up and running.
If you already have your code, you can skip this step.

$ composer create-project laravel/laravel test --prefer-dist

Set good directory ownership.

$ chown -R www-data:www-data test/

Set correct permissions as well.

$ chmod -R 775 test/

*6. Configure Nginx's Virtual Host. *
On Nginx, go to the sites-available folder.

$ cd /etc/nginx/sites-available

*7. Make changes to the virtual host. *
Use vim editor to alter the default virtual host file, which should look like this:

$ vim laravelapp.conf

The following code should be copied and pasted.

`server {
listen 80 default_server;
listen [::]:80 default_server;

root /var/www/test/public;

index index.php index.html index.htm index.nginx-debian.html;

server_name _;

location / {
try_files $uri $uri/ =404;
}

location ~ .php$ {
include snippets/fastcgi-php.conf;

With php-fpm (or other unix sockets);
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
With php-fpm (or other unix sockets);
}
}
`
You can adjust your virtual host to meet your specific needs, such as adding an SSL certificate to your website (see Why is an SSL certificate (HTTPS) vital for a website?). or implementing a redirect).

Save and exit now.

$ :wq

Create a soft link from /etc/nginx/sites/available/laravelapp.conf to /etc/nginx/sites-enabled to the virtual host.

$ ln -s /etc/nginx/sites/available/laravelapp.conf /etc/nginx/sites-enabled

Before restarting Nginx, make sure the syntax is correct. If the syntax is correct, it is safe to restart.

$ nginx -t

To load our changes, restart Nginx.

$ service nginx restart

Now, paste your public IP into your web browser, and you should be able to see your Laravel app!

You can use this command to find out if you don't know your public IP address. You can also look for your public IP on the AWS dashboard.

$ curl icanhazip.com

Note: If you've deployed your custom app, modify the RDS endpoint in your.env file to enable database access.

Keep in mind:

Alter the vendor directory. Permissions for this folder should be set to 777. Look at the following command to modify the permissions of this folder:

chmod -R 777 vendor/

If you're having trouble accessing your virtual host, try replacing the location/ block (this patch will not work if you're using Apache2):

`location / {
try_files $uri $uri/ /index.php?$query_string;

}`

Wrap Up

Laravel deployment on AWS EC2 is simpler thanks to Devrims Managed Cloud Hosting, and every server and application launched with Devrims has improved security and speed.

In the comments box below, feel free to ask any questions you may have concerning the installation process, and we will try our best to respond.

Top comments (0)