Laravel Hosting: Deploy Laravel Easily on AWS Cloud
Table of Content
- Introduction
- Laravel Applications on AWS Cloud
- Deploy Laravel on AWS Cloud Step by Step
- Conclusion
Introduction
Laravel is a prominent PHP framework that has become the go-to framework for many PHP projects. Developers often prefer Laravel due to its vast features and tools, which offer a streamlined development experience.
The Laravel hosting solution, considered the cornerstone of the project’s performance, is a significant barrier for Laravel-powered applications. For example, Laravel on AWS is a beautiful performance-focused combination that is extremely popular among developers due to its features and high benchmarks.
Because the Laravel and AWS combination is so popular, developers enjoy the struggle because it pays off in terms of stability and high-quality hosting options. On AWS, Laravel runs smoothly and provides a bug-free online experience for applications. However, installing Laravel on Amazon’s EC2 hosting server is not for everyone since it comes with obstacles.
This article will show you how to host Laravel 8.0 on Amazon Cloud hosting services step by step.
Laravel Applications on AWS Cloud
AWS provides us with the best cloud solutions and technologies, such as RDS (Relational Database Service), EC2 (Elastic Compute Cloud), and S3 (Simple Storage Service, Cloudfront, AutoScaling, Classic, and Application Load Balancers).
To deploy Laravel on AWS, we’ll need the following resources:
- VPC
- RDS
- EC2 Instance
We can now proceed to install our Laravel stack on our server after we have these resources.
Stack:
- Ubuntu 16.04
- Nginx 1.14
- PHP-7.2
- PHP7.2-FP
Deploy Laravel on AWS Cloud Step by Step
- Login to access your EC2 instance.
If you’re unsure how to accomplish it, look at this blog for instructions!
- 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
- 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
- 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
- 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/
- Configure Nginx’s Virtual Host.
On Nginx, go to the sites-available folder.
$ cd /etc/nginx/sites-available
- 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!
If you don’t know your public IP address, you can use this command to find out. 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;
}
Conclusion
Devrims Managed Cloud Hosting has made it easier to deploy Laravel on AWS EC2, and every server and application launched with Devrims comes with added security and performance.
Please feel free to ask any questions about the installation procedure in the comments section below, and we will do our best to respond.