Title: Database Setup
Content:
-
Install MySQL:
- Download and install MySQL from the official website or use a package manager.
- Start the MySQL server.
-
Create a Database:
CREATE DATABASE user_management;
-
Create a User and Grant Privileges:
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON user_management.* TO 'username'@'localhost'; FLUSH PRIVILEGES;
Title: Configure Laravel for the Database
Content:
-
Install Laravel:
composer create-project --prefer-dist laravel/laravel user-management-api
-
Configure the .env File:
- Open the
.env
file in the root of your Laravel project. - Set up the database configuration:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=user_management DB_USERNAME=username DB_PASSWORD=password
- Open the
-
Run Migrations:
php artisan migrate
Title: User Registration Endpoint
Content:
-
Create User Model:
php artisan make:model User -m
-
Define User Schema:
- Open the migration file in
database/migrations
. - Define the schema:
Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->string('phone'); $table->string('password'); $table->timestamps(); });
- Open the migration file in
-
Create Auth Controller:
php artisan make:controller AuthController
-
Add Register Method:
public function register(Request $request) { $validatedData = $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'phone' => 'required|string|max:20', 'password' => 'required|string|min:8', ]); $validatedData['password'] = bcrypt($validatedData['password']); $user = User::create($validatedData); return response()->json(['message' => 'User registered successfully', 'user' => $user], 201); }
Title: Login and Fetch User Details Endpoints
Content:
-
Login Method in AuthController:
public function login(Request $request) { $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { $user = Auth::user(); $token = $user->createToken('authToken')->accessToken; return response()->json(['message' => 'Login successful', 'token' => $token], 200); } else { return response()->json(['message' => 'Invalid email or password'], 401); } }
-
Fetch User Details Method:
public function userDetails() { $user = Auth::user(); return response()->json(['user' => $user], 200); }
-
Configure Routes:
- Add routes in
routes/api.php
:Route::post('register', [AuthController::class, 'register']); Route::post('login', [AuthController::class, 'login']); Route::middleware('auth:api')->get('user', [AuthController::class, 'userDetails']);
- Add routes in
Title: Preparing for Deployment
Content:
-
Set Up Server:
- Choose a hosting provider (e.g., AWS, DigitalOcean).
- Set up a virtual server with a LEMP stack (Linux, Nginx, MySQL, PHP).
-
Clone the Repository:
git clone <repository_url> cd user-management-api
-
Configure Environment:
- Copy
.env.example
to.env
. - Set production database credentials in the
.env
file.
- Copy
Title: Deploying the Application with Nginx
Content:
-
Install Dependencies:
composer install
-
Run Migrations:
php artisan migrate --force
-
Set Up Nginx Configuration:
- Create a new Nginx configuration file at
/etc/nginx/sites-available/user-management-api
:server { listen 1234; server_name example.com; root /var/www/html/user-management-api/public; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
- Create a new Nginx configuration file at
-
Enable the Site and Restart Nginx:
ln -s /etc/nginx/sites-available/user-management-api /etc/nginx/sites-enabled/ nginx -t systemctl restart nginx