It's never been easier to deploy a Laravel app. In this guide we will use AWS to host a fresh install of Laravel.
First you'll need to sign up for an AWS account at aws.amazon.com.
Some basic services are available under free tier while other services are charged. Remember to delete any resources you don’t need when you’re finished.
Step 1 - Launch an AWS EC2 instance
Open up the AWS console, click on the ‘Services’ menu and select ‘EC2'.
This will take you to the EC2 dashboard. Now click on ‘Instances’ on the left hand menu to see a list of your running EC2 instances.
Finally click ‘Launch Instances’ where we will configure our instance.
You will see a list of Amazon Machine Images which you can use - for this guide we’ll choose the 'Ubuntu Server 20.04 LTS (HVM), SSD Volume Type' which is available under free tier.
Next you can choose how powerful you want your instance to be. For this guide we can choose the t2.micro available under free tier.
Now we can configure our instance. You don’t need to change anything here but note the Network option where the default VPC has been selected for us.
The VPC is a Virtual Private Cloud which we can think of like a kind of network where we are running our resources.
We want our EC2 instance and RDS database to be running in the same VPC so that they can talk to each other.
Skip this step to use the default storage options.
Skip over this step. If you were running lots of apps then you could use the tags to label your instance.
Here we are going to create a new Security Group. A Security Group is simply a set of firewall rules which govern what kind of traffic can connect to our instance. So for example our EC2 instance will need to accept web traffic from the internet so users can visit our app.
The first line shows SSH which allows you to connect directly to the instance and run commands on it. To keep your instance secure it’s best to limit this to just your own IP address.
We also need to add a rule to allow HTTP traffic (on port 80) and another rule to allow HTTPS traffic (on port 443). These are the standard settings to expose your EC2 instance to web traffic from the internet.
Double check the settings and then click ‘Launch’. A popup window will ask you to create a security key file. Let’s make a new key and call it ‘my-aws-key’.
Click on 'Download Key Pair' and save the key file (my-aws-key.pem) somewhere safe. You will need to access this key file whenever you need to connect to your EC2 instance to make changes.
Finally we’re ready - click ‘Launch Instances’. After a few minutes you will see your instance is up and running.
Step 2 - Launch an RDS database instance
Now let’s head over to RDS and set up our database instance.
Now click on ‘Databases’ and then ‘Create database’.
Next select ‘Standard create’ and select the database type. For this guide we will choose PostgreSQL.
For the template select ‘Free tier’.
Next we need to name our database, and set up the username and password which our Laravel app will use to access the database. Make a note of these login details as you will need them later.
Now we need to choose our VPC, we want to select the same VPC we used for the EC2 instance. Then for Public access make sure to select ‘No' so your database so that your database is not accessible outside of the VPC.
Finally we need to set up a Security Group. Choose to create a new Security Group and give it a name. Also note the database port which is 5432, the default for PostgreSQL.
Check over your configuration and then click ‘Create database’.
In a couple of minutes your RDS database will be up and running.
Step 3 - Set up your EC2 instance
Next we need to connect to our EC2 instance via SSH so that we can set it up and install Laravel on it. On Mac we will use the terminal to connect via SSH and then execute commands on the EC2 instance.
Go back to the page for your EC2 instance and click on the Instance ID to view it.
Click on ‘Connect’ to view the instructions
In the SSH Client tab you will find the example command at the bottom of the page. This instructs your computer to make an SSH connection using your key file 'my-aws-key.pem' for authentication.
The long 'Public DNS' is the endpoint of your EC2 instance.
Now in your Mac terminal you need to navigate to the folder where you stored your 'my-aws-key.pem' key file
First you need to limit who can access the file on your computer. You need to remember to do this otherwise you will get an error when you try to connect.
chmod 400 my-aws-key.pem
Now let’s run the command to connect to your EC2 instance.
ssh -i “my-aws-key.pem” ubuntu@[Your EC2 endpoint]
If you get an error trying to connect, double check the Security Group rules for your EC2 instance. The IP address enabled for SSH connection needs to match your IP address.
Once we’re connected, we need to install some software which we will need to run Laravel.
Update our list of packages
sudo apt update
Install PHP 7.4
sudo apt install php7.4-cli
We also need to install some PHP extensions which Laravel uses:
sudo apt install php-xml php-mbstring
Install a PHP extension to support our database engine (PostgreSQL)
sudo apt install php-pgsql
And finally an extension to support our web server which we will install in the next section
sudo apt install libapache2-mod-php
Step 4 - Install Laravel and Apache2 web server
Next we need to install an HTTP server which will serve up our Laravel app to people visiting our site. We will install the Apache server.
sudo apt install apache2
Open your EC2 endpoint in a web browser - you should see the default Apache page.
http://[Your EC2 endpoint]
Now let’s install Laravel
Make a new folder for your app
sudo mkdir myapp
Download a fresh install of Laravel from Github
sudo apt install git
sudo git clone https://github.com/laravel/laravel
Allow write permissions for the Laravel folder, and the storage folder:
sudo chown -R $USER /var/www/myapp/laravel
sudo chmod -R 777 /var/www/myapp/laravel/storage
Make a copy of the example .env file, and open it for editing:
sudo cp .env.example .env
sudo nano .env
Fill out the Database block and save the file:
DB_HOST=[Your RDS endpoint]
DB_PASSWORD=[Your postgres password]
To check your RDS database endpoint head over to the RDS centre in AWS console and select your database:
Now download and install Composer, a dependency manager, from https://getcomposer.org/download/.
Install ZIP software used by Composer:
sudo apt install zip php-zip unzip
Install PHP curl which speeds up Composer:
sudo apt install curl php-curl
sudo service apache2 restart
Use composer to install our Laravel dependencies:
Now we can setup Laravel
php artisan key:generate
php artisan migrate:fresh
Step 5 - Setup Apache to serve our Laravel app
The best way to setup Apache is to use Virtual Hosts for our sites.
Let’s create the config file for our site:
sudo nano /etc/apache2/sites-available/myapp.conf
Update myapp.conf with the following code. ServerName should be your EC2 endpoint.
This code tells Apache to route traffic arriving at port 80 to your Laravel public folder, which is the entry point to your Laravel app.
The "AllowOverride All" option is needed for Laravel routing to work.
ServerName [Your EC2 endpoint]
Back in the terminal, enable the new myapp.conf file, and disable the default config file
sudo a2ensite myapp.conf
sudo a2dissite 000-default.conf
We also need to run a command to enable mod_rewrite feature for Apache. This lets Apache use a set of rules to handle incoming URLs, and we need it for our Laravel routes to work correctly.
sudo a2enmod rewrite
sudo service apache2 restart
Optional: install Laravel Breeze
Laravel Breeze is a Laravel package which provides a simple authentication system
composer require laravel/breeze --dev
sudo php artisan breeze:install
We need to use Node Package Manager (NPM) to manage our front end packages.
The easiest way to use NPM is to install Node Version Manager (NVM):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
Open a new terminal window and reconnect to your EC2 instance
nvm install node
Use NPM to install the front end packages
npm run dev
Now visit http://[Your EC2 endpoint] and your app should be up and running!
If you installed Laravel Breeze then you’ll be able to Register and Login to the app.
(Finally, remember to delete your EC2 instance and RDS instance once you no longer need them)