Hosting Laravel in Amazon Lightsail
Laravel is a PHP framework that is designed for rapid development. It is based on the MVC software architecture pattern.
While Amazon Lightsail is a new offering from AWS to create a VPS (Virtual Private Server) on the cloud. It also offer a cheap price for its VPS.
See my previous post for more detail regarding how to create a new instance and connecting to it.
There are quite a few options when choosing a web server for PHP in Linux. But the most popular ones are Nginx and Apache. And continuing from the last post, we’re going to use Nginx to host the PHP.
Preparing the Laravel application
When doing deployment for Laravel application, you can deploy all files from development and it will work as in development. This is because unlike other programming languages such as C# or Java that needs to be compiled, PHP is a dynamic scripting language that will be interpreted dynamically.
So you will need a Laravel application working at your local development machine before we can start to use a Lightsail service for hosting the app. As Laravel is using PHP, it is a cross platform application, so you can start development from any platform of your choice. Some of the guides for Linux, Windows, or Mac are already available.
For this article, we are going to proceed with a Linux environment machine in Lightsail.
Creating the Lightsail Instance
You will need to have an AWS account and already set up your payment information. If you don’t have your payment information set up, you will not be able to create a new Lightsail instance.
From the AWS Management Console, now exists a new navigation to the Lightsail service which has that icon that shows that it is an external link / product.
If you navigate from the Lightsail link, you will then be greeted with the Lightsail dashboard.
Creating a new instance is pretty straightforward, from choosing the base image that you want to use for your instance (Windows / Linux).
Although Amazon Lightsail already provided us with a LAMP stack or an Nginx template for us, we’re going to stick to “OS Only” and use Ubuntu 16.04. You then need to setup your SSH Key for connecting to the instance later on. You can use the default that is provided by Amazon, or create a new one. I’m just going to use the default one for now.
You can then choose the instance size which come as low as $3.50
Connecting to the new instance
After creating the new instance, you can then use any SSH tools to connect to it. If you’re coming from Windows, like me, you can use Putty to connect.
You will then need to input the IP address.
And then use the SSH key that you setup/downloaded earlier.
The user for login will be “ubuntu”.
If you use the default SSH key, it can be downloaded from your account page.
Setting up the instance
After connecting to the instance, we can setup the instance. For our purposes, we use nginx to serve the HTTP request.
sudo apt-get update
sudo apt-get install nginx
You can test if the nginx is installed successfully by browsing through the IP address that is provided from the Lightsail instance management.
Installing PHP
Before the server can serve laravel application, we need to install PHP 7. For the purpose of this article, we are going to install PHP 7.3
We will need to first update the Ubuntu server
sudo apt-get update && sudo apt-get upgrade
And then make sure you have the following package installed so you can add repositories
sudo apt-get install software-properties-common
You will need the repository from Ondrej
sudo add-apt-repository ppa:ondrej/php
Update your package list
sudo apt-get update
Then you can install the PHP 7.3 and several other packages
sudo apt-get install php7.3 php7.3-xml php7.3-gd php7.3-opcache php 7.3-mbstring
You can check your installed PHP version
php -v
Removing Apache2
Installing PHP using above commands will also install Apache2 as default. And if you don’t want to use Apache2, you can uninstall it using this command
sudo apt-get purge apache2 apache2-utils apache2-bin apache2.2-common
Installing MySQL
Currently Laravel supports MySQL, Postgres, SQLite, and SQL Server. If you need MySQL for your application, you can install MySQL using Ubuntu package manager, you will also need to provide the password for the “root” user later.
sudo apt-get install mysql-server
You can check whether the MySQL service is running
sudo service mysql status
Setting up Laravel
Next thing is you need to copy and paste your Laravel application from development to the server. You can do this using any FTP tool that is out there. Connect using SFTP protocol on port 22 and provide the SSH key that is created earlier when creating the instance.
The default location for nginx files is in /var/www/html and we might put our files in the /var/www folder as well. These folders are owned by the root user and so we might need to create the folder using sudo in the terminal. After that you will need to change the folder owner to ubuntu as this user is what we are using on the FTP connection.
sudo mkdir /var/www/laravel
sudo chown ubuntu:ubuntu /var/www/laravel
When you have setup the target folder, you can just copy using the FTP tool of your choice.
Configuring Nginx for Laravel
You will need to setup Nginx to use your newly created Laravel folder and use the public folder as the root folder.
sudo nano /etc/nginx/sites-available/default
Use this configuration below as a guideline
- Set the server to listen at port 80
- Set the root to point to the public folder of your Laravel application
- Add index.php to the list of index files that might be called
- Set the server_name to any server name that you might use for your website
- Update the location / to use index.php with its query string
- Add the location .php to use fastcgi and also run using php-fpm
You will then need to restart Nginx
sudo service nginx restart
Running your website is not working now as it comes up with some error.
Looking at the error message shows exactly what it is. You will need to grant permission to the www-data user for the storage folder inside your Laravel application. www-data is the default user for Nginx to run your application.
sudo chown www-data:www-data /var/www/laravel/storage -R
Refreshing your browser after this, you will get the welcome page by Laravel.
Editing your application
Let’s try to change our welcome page to show “My Laravel” label instead.
And then you will need to transfer the modified file to the server. The changes will automatically reflected after you refresh your browser.
Summary
We have successfully created a new instance using Amazon Lightsail. We have also setup fresh Laravel application to be run using Nginx. Updating your Laravel application is as simple as copy paste all modified files from your development machine to the instance.