Overview
In this first part of the series, we will guide you through setting up a Laravel API server hosted on an AWS EC2 instance (Free Tier) with a MySQL database on Amazon RDS. We will cover the EC2 server setup, Laravel installation, Sanctum configuration for token-based authentication, Apache vhosts
configuration, using Route 53 for domain management, and securing your server with SSL via Let’s Encrypt.
Prerequisites
- AWS (Free Tier account).
- EC2 instance running Ubuntu 22.04.
- RDS MySQL database.
- Basic knowledge of Laravel, Ubuntu, and command-line tools.
1. Setting up the Server
Launching an AWS EC2 Instance
- Start by creating an Ubuntu 22.04 EC2 instance eligible for the AWS Free Tier.
- Assign a security group with HTTPS (port 443) and SSH (port 22) access to the instance.
Installing the LAMP Stack
Once your instance is running, SSH into it and install the LAMP stack:
sudo apt update
sudo apt install apache2 mysql-server php php-mysql libapache2-mod-php php-cli php-xml php-mbstring unzip curl
Configuring Apache for Laravel
Create a directory for your Laravel application and adjust permissions:
sudo mkdir -p /var/www/api.example.com
sudo chown -R www-data:www-data /var/www/api.example.com
sudo chmod -R 755 /var/www/api.example.com
Cloning Your Laravel Repository
Clone your Laravel project repository into the created directory:
cd /var/www/api.example.com
sudo git clone <your-laravel-repo-url> .
2. Configure Apache vhosts
To set up a virtual host for Laravel, follow these steps:
- Create a new
vhost
configuration file for your domain:
sudo nano /etc/apache2/sites-available/api.example.com.conf
- Add the following configuration, replacing
api.example.com
with your domain and ensuring the document root points to your Laravel public directory:
<VirtualHost *:80>
ServerAdmin webmaster@api.example.com
ServerName api.example.com
DocumentRoot /var/www/api.example.com/public<Directory /var/www/api.example.com>
AllowOverride All
Require all granted
</Directory>ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- Enable the site and rewrite module:
sudo a2ensite api.example.com.conf
sudo a2enmod rewrite
sudo systemctl restart apache2
This will configure your server to respond to api.example.com
and ensure that the .htaccess
file in Laravel works as expected.
3. Pointing the Domain to the EC2 Instance with Route 53
To make your domain accessible, you need to configure AWS Route 53 to point your domain to the EC2 instance:
- Create a Hosted Zone in Route 53
In the AWS Management Console, navigate to Route 53 and create a hosted zone for your domain (api.example.com
). - Add an A Record
Once the hosted zone is created, add an A record that points to the public IP address of your EC2 instance:- Select Create Record.
- For Record type, select A – IPv4 address.
- In the Value field, enter the public IP of your EC2 instance.
- Set the TTL (Time to Live) as preferred, then click Create records.
- Update Your Domain Registrar
If your domain is registered outside AWS, go to your domain registrar and update the nameservers (NS records) to point to the ones provided by AWS Route 53 for your hosted zone.
This will ensure that when you visit api.example.com
, it directs to your EC2 instance.
4. Set up MySQL
Log in to MySQL from the RDS instance
Log in to your MySQL database on Amazon RDS:
sudo mysql -u root -p
Create Database and User
Run the following SQL commands to create a database and a user with proper privileges:
CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'%' IDENTIFIED BY 'securepassword';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'%';
FLUSH PRIVILEGES;
EXIT;
5. Update the .env
File in Laravel
Navigate to the .env
file in your Laravel project and update the database configuration to match the details for your RDS instance:
6. Installing and Configuring Laravel
Install Laravel Dependencies
Run the following commands to install Laravel’s PHP dependencies:
composer install
php artisan migrate
php artisan key:generate
Configure Sanctum for Token-Based Authentication
In config/auth.php
, configure Sanctum to use token-based authentication by updating the 'driver'
field:
Next, comment out unnecessary middleware in app/Http/Kernel.php
:
7. Setting Up SSL with Let’s Encrypt
To secure your server with SSL, we will use Let’s Encrypt, a free SSL certificate provider.
Install Certbot
- First, add the Certbot repository and install Certbot:
sudo apt install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt update
sudo apt install certbot python3-certbot-apache
Obtain an SSL Certificate
- To request an SSL certificate for your domain, run the following command:
- Follow the prompts to complete the process. Certbot will automatically configure Apache for SSL and redirect all HTTP traffic to HTTPS.
Automatic SSL Renewal
- To ensure your SSL certificate is renewed automatically, Certbot adds a cron job for renewal. You can test it by running:
8. Testing the API
To ensure everything is working, use tools like Postman or Insomnia to test your Laravel API endpoints. Make sure that your Laravel routes, token authentication, and SSL are functioning as expected.