A RESTful API for a blog platform built with Laravel, featuring user authentication (via Sanctum), CRUD operations for blog posts, and deployment on AWS EC2.
- Registration, login, and token-based authentication using Laravel Sanctum.
- Protected API endpoints using middleware.
- CRUD operations (Create, Read, Update, Delete) for blog posts.
- Posts are associated with authenticated users.
- Input validation for user data.
- Deployed on AWS EC2 with Nginx, MySQL, and PHP.
- Backend: Laravel 10
- Authentication: Laravel Sanctum
- Database: MySQL
- Web Server: Nginx
- Deployment: AWS EC2
- Testing: Postman
- PHP ≥ 8.1
- Composer
- MySQL
-
Clone the repository:
git clone https://github.com/yourusername/laravel-blog-api.git cd laravel-blog-api -
Install dependencies:
composer install
-
Configure the environment:
cp .env.example .env php artisan key:generate
-
Update
.envwith your database credentials:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Blog_db DB_USERNAME=*** DB_PASSWORD=***
-
Migrate the database:
php artisan migrate
-
Start the server:
php artisan serve
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/register | Register a new user. |
| POST | /api/login | Log in and get a token. |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/blogs | Get all blog posts. |
| POST | /api/blogs | Create a new blog post. |
| GET | /api/blogs/{id} | Get a specific blog post. |
| PUT | /api/blogs/{id} | Update a blog post. |
| DELETE | /api/blogs/{id} | Delete a blog post. |
- Ubuntu 22.04 LTS (t2.micro).
- Security group allowing SSH (port 22), HTTP (port 80), and HTTPS (port 443).
ssh -i "laravel-key.pem" ubuntu@13.61.9.245sudo apt update && sudo apt install php php-mysql nginx mysql-server composer -y- Clone your repository or upload files to
/home/ubuntu/blog-api.
- Create a config file in
/etc/nginx/sites-available/laravel(refer to Nginx Setup section below).
sudo chown -R www-data:www-data /home/ubuntu/blog-api
sudo chmod -R 775 storage bootstrap/cachesudo systemctl restart nginx php8.1-fpmCreate an Nginx configuration file (/etc/nginx/sites-available/laravel) with the following content:
server {
listen 80;
server_name 13.61.9.245;
root /home/ubuntu/blog-api/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}-
Live API URL:
http://13.61.9.245:8000/api/blogs