Friday, December 27, 2024

Deploying a Private ChatGPT on Debian 12 with Nginx, PHP, and MariaDB

Below is a step-by-step guide to deploy a private ChatGPT web application using OpenAI’s API or similar LLMs.

1. System Requirements
Ensure your server has the following installed:

Debian 12 operating system.
Nginx 1.27 for the web server.
PHP 7.4 or 8.4 for backend processing.
MariaDB 11 for database management.
2. Install Required Software
Update the system and install necessary packages:
sudo apt update && sudo apt upgrade -y

sudo apt install nginx mariadb-server php php-mysql php-fpm git unzip curl -y

Start and enable services:

sudo systemctl enable nginx mariadb php7.4-fpm
sudo systemctl start nginx mariadb php7.4-fpm

3. Configure MariaDB
1.Secure MariaDB installation:

sudo mysql_secure_installation

2.Create a database and user:

sudo mysql -u root -p

Run the following commands in the MariaDB console:

CREATE DATABASE chatgpt_db;
CREATE USER 'chatgpt_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON chatgpt_db.* TO 'chatgpt_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

4. Set Up ChatGPT Application
1.Download and set up Laravel:

cd /usr/share/nginx/html
composer create-project --prefer-dist laravel/laravel chatgpt
cd chatgpt

2.Install dependencies:

composer install

3.Configure environment variables: Copy the .env file:

cp .env.example .env

Edit the .env file:

nano .env

Update database and OpenAI API settings:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=chatgpt_db
DB_USERNAME=chatgpt_user
DB_PASSWORD=your_password

OPENAI_API_KEY=your_openai_api_key

4.Set permissions:

sudo chown -R www-data:www-data /usr/share/nginx/html/chatgpt
sudo chmod -R 775 /usr/share/nginx/html/chatgpt/storage /usr/share/nginx/html/chatgpt/bootstrap/cache

5.Generate an application key:

php artisan key:generate

5.Configure Nginx
Edit the Nginx configuration file:

sudo nano /etc/nginx/sites-available/chatgpt

Add the following configuration:

server {
    listen 80;
    server_name your_domain.com;
    root /usr/share/nginx/html/chatgpt/public;

    index index.php index.html;

    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;
    }
}

Enable the configuration and restart Nginx:

sudo ln -s /etc/nginx/sites-available/chatgpt /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

6. Build ChatGPT Features
1.Create a controller:

php artisan make:controller ChatGPTController

2.Implement the API logic: Edit app/Http/Controllers/ChatGPTController.php:

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesHttp;

class ChatGPTController extends Controller
{
    public function chat(Request $request)
    {
        $response = Http::withHeaders([
            'Authorization' => 'Bearer ' . env('OPENAI_API_KEY'),
        ])->post('https://api.openai.com/v1/chat/completions', [
            'model' => 'gpt-3.5-turbo',
            'messages' => $request->input('messages'),
        ]);

        return response()->json($response->json());
    }
}

3.Add routes: Edit routes/web.php:

use AppHttpControllersChatGPTController;

Route::post('/chat', [ChatGPTController::class, 'chat']);

7. Test the Application
1.Access the application in your browser: http://your_domain.com.
2.Use a frontend form or API client (like Postman) to send a POST request to the /chat route.
8. Optional Enhancements
Add HTTPS
Install a free Let’s Encrypt SSL certificate:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain.com

Optimize Performance
Configure caching for API responses.
Use a job queue (e.g., Redis) for asynchronous tasks.
Enable PHP opcache for faster script execution.
Example Interaction
Here’s how the flow works:

A user sends a chat message via a POST request to /chat.
The Laravel backend relays the message to the OpenAI API.
The OpenAI API processes the input and returns a response.
The Laravel backend sends the response back to the user.
This setup creates a scalable private ChatGPT web app.
OVER!!!

Add comment

Fill out the form below to add your own comments