Download the PHP package webmavens/laravel-remote-http-database without Composer

On this page you can find all versions of the php package webmavens/laravel-remote-http-database. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-remote-http-database

Laravel Remote HTTP Database Adapter

A Laravel package that provides a custom database adapter for communicating with a remote MySQL server via HTTP API. This allows you to run a Laravel application on a server without MySQL by connecting to a remote MySQL server through an encrypted HTTP endpoint.

Quick Start

  1. Install on both servers: composer require webmavens/laravel-remote-http-database
  2. Generate keys (run once, use on both servers):

  3. Server 1 (has MySQL): Set REMOTE_DB_API_KEY and REMOTE_DB_ENCRYPTION_KEY in .env (optionally add REMOTE_DB_ALLOWED_IPS for IP whitelisting)
  4. Server 2 (client):
    • ⚠️ REQUIRED: Manually add the remote-http connection to config/database.php (see Step 4 below for the exact code)
    • Set DB_CONNECTION=remote-http, DB_REMOTE_ENDPOINT, DB_REMOTE_API_KEY, and DB_REMOTE_ENCRYPTION_KEY in .env

See the Installation section for detailed steps.

Features

Architecture

Requirements

Installation

Step 1: Install the Package

Install the package on both servers via Composer:

The service provider will be auto-discovered by Laravel, so no manual registration is needed.

Step 2: Generate Security Keys

Generate a secure API key and encryption key. These must be identical on both servers:

Save these values - you'll need them for both servers.

Step 3: Configure Server 1 (Has MySQL)

Server 1 hosts the MySQL database and serves the remote endpoint.

  1. Configure your .env file with your MySQL connection and security keys:

Important Notes:

  1. The endpoint route is automatically registered at /remote-db-endpoint when REMOTE_DB_API_KEY is set.

  2. Test the endpoint (optional):

You should see a JSON response with endpoint information.

Step 4: Configure Server 2 (No MySQL - Client)

Server 2 connects to Server 1's endpoint to access the database.

⚠️ IMPORTANT: Manual code edit required! You must manually add the remote-http connection configuration to your config/database.php file. This cannot be done automatically.

  1. Add the remote-http connection to config/database.php:

Open config/database.php and add the following connection to the connections array. Important: This configuration safely handles cases where DB_REMOTE_ENDPOINT is not set (e.g., on Server 1), preventing PHP deprecation warnings:

Note:

  1. Configure your .env file:

Critical: The DB_REMOTE_API_KEY and DB_REMOTE_ENCRYPTION_KEY must exactly match the values on Server 1.

  1. For local development, you can disable SSL verification:

⚠️ Never disable SSL verification in production!

Step 5: Test the Connection

Test the connection from Server 2:

Or create a test script:

That's it! Your Laravel application will now use the remote MySQL database via HTTP.

Usage

Once configured, use your Laravel application exactly as you would with a normal MySQL connection:

Security Considerations

  1. HTTPS: Always use HTTPS for the remote endpoint URL. Never use HTTP in production.

  2. Strong Keys:

    • Use a strong, random API key (at least 32 characters)
    • Use a strong, random 32-byte encryption key
    • Never commit these keys to version control
  3. IP Whitelisting: Use the REMOTE_DB_ALLOWED_IPS environment variable to restrict access to specific IP addresses. This provides an additional layer of security by rejecting requests from unauthorized IPs early in the request lifecycle. See the IP Whitelisting section for detailed instructions.

  4. Firewall: Consider restricting access to the endpoint via firewall rules (IP whitelist).

  5. Rate Limiting: Consider implementing rate limiting on the remote endpoint to prevent abuse.

  6. Monitoring: Monitor endpoint logs for suspicious activity.

Configuration Options

Client Configuration (Server 2)

Option Environment Variable Description Default
endpoint DB_REMOTE_ENDPOINT The URL of the remote endpoint Required
api_key DB_REMOTE_API_KEY API key for authentication (must match Server 1) Required
encryption_key DB_REMOTE_ENCRYPTION_KEY 32-byte encryption key (can be base64 encoded) Required
database DB_DATABASE Database name laravel
timeout DB_REMOTE_TIMEOUT HTTP request timeout in seconds 30
verify_ssl DB_REMOTE_VERIFY_SSL Whether to verify SSL certificates true
retry_attempts DB_REMOTE_RETRY_ATTEMPTS Number of retry attempts on failure 3
enable_batching DB_REMOTE_ENABLE_BATCHING Enable query batching to reduce HTTP requests true
enable_caching DB_REMOTE_ENABLE_CACHING Enable query result caching true
cache_ttl DB_REMOTE_CACHE_TTL Cache TTL in seconds for SELECT queries 60

Server Configuration (Server 1)

Option Environment Variable Description
endpoint_api_key REMOTE_DB_API_KEY API key for endpoint authentication
endpoint_encryption_key REMOTE_DB_ENCRYPTION_KEY 32-byte encryption key (can be base64 encoded)
endpoint_path REMOTE_DB_ENDPOINT_PATH Custom endpoint path (optional) /remote-db-endpoint
endpoint_allowed_ips REMOTE_DB_ALLOWED_IPS Comma-separated list of allowed IP addresses (optional) null

How It Works

  1. Query Execution: When Laravel executes a database query, the RemoteHttpConnection intercepts it.

  2. Caching Check: If caching is enabled, the connection first checks if the query result is cached. If found and not expired, it returns the cached result immediately without making an HTTP request.

  3. Query Batching: If batching is enabled, multiple queries can be queued and sent together in a single HTTP request, reducing network overhead.

  4. Encryption: The query and bindings are encrypted using AES-256-GCM with a unique IV for each request.

  5. HTTP Request: The encrypted payload is sent to the remote endpoint via HTTPS POST with the API key in headers.

  6. Remote Processing: The endpoint on Server 1:

    • Validates IP address (if whitelisting is enabled)
    • Validates the API key
    • Decrypts the payload
    • Executes the query/queries on MySQL
    • Maintains transaction state per session
    • Encrypts the response
  7. Response: The encrypted response is sent back, decrypted, and returned to Laravel as if it came from a local database.

  8. Cache Storage: For SELECT queries, the result is cached (if caching is enabled) to avoid redundant requests for the same query.

Performance Optimization

This package includes built-in performance optimizations to reduce HTTP requests and improve page load times:

Query Caching

Query result caching stores SELECT query results in memory to avoid redundant HTTP requests. This is especially useful for:

Configuration:

The cache is automatically invalidated when write operations (INSERT, UPDATE, DELETE) are performed.

Query Batching

Query batching allows multiple queries to be sent in a single HTTP request, reducing network round-trips. This is particularly beneficial when:

Configuration:

Note: Batching is automatically disabled during transactions to ensure data consistency.

Performance Tips

  1. Enable Caching: For read-heavy applications, enable caching with an appropriate TTL:

  2. Use Eager Loading: When using Eloquent, use eager loading to reduce the number of queries:

  3. Optimize Queries: Review your application's queries and identify:

    • Duplicate queries that can benefit from caching
    • Queries that can be combined
    • Unnecessary queries that can be removed
  4. Monitor Cache Hit Rate: Consider adding logging to monitor cache effectiveness in production.

IP Whitelisting

The package supports optional IP address whitelisting to restrict access to the remote database endpoint. When enabled, only requests from specified IP addresses will be allowed.

How to Enable IP Whitelisting

  1. Add IP addresses to your .env file on Server 1:

  2. Clear the configuration cache:

  3. Restart your application to apply the changes.

How It Works

Examples

Single IP address:

Multiple IP addresses:

Behind a proxy/load balancer: The package automatically detects the real client IP from X-Forwarded-For or X-Real-IP headers. Just whitelist the actual client IPs:

Important Notes

Transaction Handling

Transactions are handled using session-based state management:

Troubleshooting

"401 Unauthorized" Error

This means the API key doesn't match between servers:

  1. Verify the API keys match exactly on both Server 1 and Server 2:

    • Server 1: REMOTE_DB_API_KEY in .env
    • Server 2: DB_REMOTE_API_KEY in .env
  2. Clear config cache on both servers:

  3. Restart your application after changing environment variables

"Encryption key must be exactly 32 bytes"

The encryption key must be exactly 32 bytes when decoded. You can:

  1. Use a base64-encoded key (recommended - 44 characters):

    Store this in your .env file as-is.

  2. Use a raw 32-byte key (32 characters):

    This generates 32 hex characters = 32 bytes.

  3. Verify the key matches on both servers exactly (including any base64 encoding)

"HTTP request failed"

"Decryption failed"

"QueryException: Argument #3 ($bindings) must be of type array"

This error indicates you're using Laravel 12. Make sure you have the latest version of this package that supports Laravel 12's QueryException constructor signature.

Transactions not working

Endpoint not found (404)

"403 Forbidden: IP address not allowed"

This error occurs when IP whitelisting is enabled and the request is coming from an unauthorized IP address.

  1. Check the client IP address:

    • Verify the IP address of Server 2 (the client making requests)
    • If behind a proxy/load balancer, check the X-Forwarded-For or X-Real-IP headers
  2. Add the IP to the whitelist on Server 1:

    Add the client's IP address to the comma-separated list.

  3. Clear config cache on Server 1:

  4. Temporarily disable IP whitelisting (for testing):

    • Remove or comment out REMOTE_DB_ALLOWED_IPS in Server 1's .env file
    • Clear config cache and restart the application
  5. Verify proxy configuration:
    • If Server 1 is behind a proxy/load balancer, ensure it's properly forwarding client IP headers
    • The package checks X-Forwarded-For and X-Real-IP headers automatically

Testing

Local Development Setup

For local development, you can run both servers on different ports:

Terminal 1 - Server 1 (MySQL Server):

Terminal 2 - Server 2 (Client):

Make sure:

Running Tests

The package includes tests that can be run with:

Or using PHPUnit directly:

License

This package is open-sourced software licensed under the MIT license.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For issues and questions, please open an issue on GitHub.


All versions of laravel-remote-http-database with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
illuminate/cache Version ^10.0|^11.0|^12.0
illuminate/database Version ^10.0|^11.0|^12.0
illuminate/support Version ^10.0|^11.0|^12.0
guzzlehttp/guzzle Version ^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package webmavens/laravel-remote-http-database contains the following files

Loading the files please wait ...