Table of Content
- Introduction
- Getting Started with Laravel Hosting
- Deploy Laravel on the AWS server
- Steps to deploy Laravel on the AWS server
- Benefits of Laravel Hosting
- Conclusion
Introduction
Laravel is a great PHP framework with several frameworks, and it has excellent scalability and performance. Every application development requires a hosting environment for running it, both for testing and deployment. This blog will learn how to run a Laravel application on an Amazon Virtual Private Cloud (Amazon VPC).
Getting Started with Laravel Hosting
Laravel hosting is vital to the success of your eCommerce website. It is not something that you want to get wrong the first time. Often, people just go for the cheapest hosting with the highest speed. But this can often lead to a bad experience.
If your hosting does not offer the right level of speed, you will experience lag, and it will be challenging to navigate the site. You will have to keep your fingers crossed that your website does not go down or have any outages. It is where Laravel hosting comes in. Luckily for you, there are a bunch of excellent Laravel hosting options.
Deploy Laravel on the AWS server
Laravel is a robust PHP-based web framework that is used by a lot of developers. And now, thanks to the advent of the Cloud, it can be deployed on the AWS server for an affordable price. In this blog, I will show you how you can set up a Laravel Environment on the AWS cloud and how you can optimize it for a better user experience.
RDS (Relational Database Service), EC2 (Elastic Compute Cloud), and S3 (Storage Service) are some of the most incredible cloud solutions and technologies provided by AWS (Simple Storage Service, Cloudfront, AutoScaling, Classic, and Application Load Balancers).
Steps to deploy Laravel on the AWS server
- Login to access your EC2 instance.
Look at this blog for directions if you are unsure how to do it!
- Refresh your library.
Let’s make sure we have the most up-to-date packages in our libraries.
Warning: Updating libraries should not create any problems if your server is spanking new; if it is not, make careful you do not break anything. You may be updating a package that’s incompatible with your current stack.
$ sudo apt-get update
$ sudo apt-get upgrade
- Install the newest version of Nginx.
Let’s begin with superuser privileges.
$ sudo su
Let’s make a variable for Nginx’s most recent version.
$ nginx=stable
Add the repository using the primary variable.
$ add-apt-repository ppa:nginx/$nginx
Press enter to confirm this addition.
Let’s update our Nginx libraries to the most recent version.
$ apt-get update
Finally, Nginx needs to be set up.
To verify Nginx’s version, run the following command:
$ nginx -v
- PHP 7.2 and php7.2-fpm must be installed.
Let’s get started with the next bundle.
$ apt-get install python-software-properties
Now is the time to add the repository.
$ add-apt-repository ppa:ondrej/php
Press enter to confirm this addition.
Now we will upgrade our libraries to php7.2.
$ apt-get update
Finally, php7.2 and php7.2-fpm must be installed.
$ 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
These libraries also need to be installed.
$ apt install zip unzip php7.2-zip
To check that your installation is correct, look at the PHP version.
$ php -V
- Install Laravel and deploy your application.
You can upload your software to your server using SCP, FTP, or any other protocol you want at this point. In this tutorial, Laravel will be installed from the ground up.
$ curl -sS https://getcomposer.org/installer | php
By relocating the composer, you may make it executable.
$ mv composer.phar /usr/local/bin/composer
Let’s get to where we will install or relocate Laravel.
$ cd /var/www
Now it is time to get Laravel up and running.
You can skip this step if you already have your code.
$ composer create-project laravel/laravel test –prefer-dist
Make sure you have good directory ownership.
$ chown -R www-data:www-data test/
As well, make sure the permissions are accurate.
$ chmod -R 775 test/
- Configure Nginx’s Virtual Host.
Navigate to the sites-available folder in Nginx.
$ cd /etc/nginx/sites-available
- Make changes to the virtual host.
Alter the default virtual host file with vim editor, which should look like this:
$ vim laravelapp.conf
You should copy and paste the code below.
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 customize your virtual host to match your unique requirements, such as adding an SSL certificate to your website (see Why is an SSL certificate (HTTPS) necessary for a website?). or setting up a redirect).
Now save and exit.
$ :wq
Create a soft link from the virtual host’s /etc/nginx/sites/available/laravelapp.conf to /etc/nginx/sites-enabled.
$ ln -s /etc/nginx/sites/available/laravelapp.conf /etc/nginx/sites-enabled
Make sure the syntax is right before restarting Nginx. It is safe to restart if the syntax is accurate.
$ nginx -t
Nginx must be restarted for our changes to take effect.
$ service nginx restart
Now, paste your public IP into your web browser, and your Laravel app should appear!
If you do not know it, you can use this command to find out your public IP address. You can also use the AWS dashboard to find your public IP.
$ curl icanhazip.com
Note: To enable database access after deploying your custom app, change the RDS endpoint in your.env file.
Keep in mind:
Change the vendor list. This folder’s permissions should be set to 777. To change the permissions of this folder, run the following command:
chmod -R 777 vendor/
Replace the location/ block if you are having difficulties accessing your virtual host (this patch will not work if you are using Apache2):
location / {
try_files $uri $uri/ /index.php?$query_string;
}
Benefits of Laravel Hosting
There are many reasons to use dedicated hosting. The days of setting up a dedicated server for a single app, or worse, installing something on your server, are officially gone.
If you are going to host multiple applications on a single server, it is good to have each application running on a different port. It can be achieved by using a reverse proxy, such as Nginx, in front of your Laravel application.
Conclusion
This blog tried to cover all the essential aspects of hosting Laravel applications on the AWS cloud (Amazon Web Services).