Sunday, December 22, 2024

create a NAS program

Here’s how to create a NAS program using Debian 12, Nginx 1.27, PHP 8.4, MariaDB 11, and Linux Kernel 6.1, deploying it under /usr/share/nginx/html.

Objectives of the NAS Program
User Authentication and Role Management:

Admin and regular user roles.
User registration, login, and permissions.
File Management:

Upload, download, delete, and rename files.
Create directories.
File Sharing:

Generate shareable links.
Set expiration dates for shared links.
System Monitoring:

Display storage usage.
Provide data backup and restore options.
Steps to Develop and Deploy
1. Environment Setup
1.1 Install Necessary Packages
# Update the system

sudo apt update && sudo apt upgrade -y

# Install Nginx

sudo apt install -y nginx

# Install PHP 8.4 and required extensions

sudo apt install -y php8.4 php8.4-fpm php8.4-mysql php8.4-zip php8.4-mbstring php8.4-curl php8.4-xml php8.4-gd

# Install MariaDB 11

sudo apt install -y mariadb-server

# Install Git and Composer

sudo apt install -y git composer

1.2 Configure Nginx
Create a new Nginx configuration file at /etc/nginx/sites-available/nas:

server {
    listen 80;
    server_name nas.local;

    root /usr/share/nginx/html;
    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/php8.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.ht {
        deny all;
    }
}

Enable the site configuration and restart Nginx:

sudo ln -s /etc/nginx/sites-available/nas /etc/nginx/sites-enabled/
sudo systemctl reload nginx

2. Database Setup
2.1 Create the Database
Log in to MariaDB:

sudo mariadb

Run the following SQL commands:

CREATE DATABASE nas;
CREATE USER 'nas_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON nas.* TO 'nas_user'@'localhost';
FLUSH PRIVILEGES;

2.2 Database Schema
users Table:

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    role ENUM('admin', 'user') DEFAULT 'user',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

files Table:

CREATE TABLE files (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    path VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

shared_links Table:

CREATE TABLE shared_links (
    id INT AUTO_INCREMENT PRIMARY KEY,
    file_id INT NOT NULL,
    token VARCHAR(64) NOT NULL UNIQUE,
    expires_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (file_id) REFERENCES files(id) ON DELETE CASCADE
);

3. PHP Project Setup
3.1 Create a Laravel Project
Set up the Laravel project in /usr/share/nginx/html:

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

3.2 Configure .env File
Update the .env file to connect to the database:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=nas
DB_USERNAME=nas_user
DB_PASSWORD=strong_password

3.3 Set Permissions
Ensure proper file permissions for PHP and Nginx:

sudo chown -R www-data:www-data /usr/share/nginx/html
sudo chmod -R 775 /usr/share/nginx/html

4. Core Functionality Development
4.1 User Authentication
Use Laravel’s built-in authentication system:

cd /usr/share/nginx/html/nas
php artisan make:auth

4.2 File Management
Create a file management controller:

php artisan make:controller FileController
Example code for file upload:

public function upload(Request $request) {
    $request->validate([
        'file' => 'required|file|max:10240', // Max 10MB
    ]);

    $path = $request->file('file')->store('uploads', 'public');
    File::create([
        'user_id' => auth()->id(),
        'name' => $request->file('file')->getClientOriginalName(),
        'path' => $path,
    ]);

    return redirect()->back()->with('success', 'File uploaded successfully!');
}

4.3 File Sharing
Example code to generate a shareable link:

public function share($file_id) {
    $file = File::findOrFail($file_id);
    if ($file->user_id !== auth()->id()) {
        abort(403);
    }

    $token = Str::random(32);
    SharedLink::create([
        'file_id' => $file_id,
        'token' => $token,
        'expires_at' => now()->addDays(7),
    ]);

    return response()->json(['link' => route('files.shared', $token)]);
}

5. Deployment and Testing
5.1 Test the Application
Access http://nas.local and ensure everything works. If there are issues:

Check Nginx logs: /var/log/nginx/error.log
Check Laravel logs: /usr/share/nginx/html/nas/storage/logs/laravel.log
5.2 Optimize Performance
Enable PHP OPCache: Edit /etc/php/8.4/fpm/php.ini:

opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000

Use Redis for caching: Install Redis and configure Laravel caching.

This setup provides a fully functional NAS program deployed under /usr/share/nginx/html.
OVER!!!