Skip to main content

Multiple databases with LAMP app

Overview

Custom PHP apps can be hosted on Cloudron using the LAMP app. This app provides PHP code access to a single MySQL database. Cloudron's isolation and sandboxing practices prevent the LAMP app from creating additional databases.

This guide explains how to create a custom LAMP app that can use multiple MySQL databases.

Prerequisites

Install the Cloudron CLI on your PC/Mac with sudo npm install -g cloudron, then login with cloudron login my.example.com.

Install app

Clone the LAMP 7.4 multidb app package:

~$ git clone https://git.cloudron.io/cloudron/lamp7.4-multidb-app.git
Cloning into 'lamp7.4-multidb-app'...
Receiving objects: 100% (841/841), 225.28 KiB | 1.06 MiB/s, done.
Resolving deltas: 100% (552/552), done.

The main difference between this app and the LAMP 7.4 app package in the App Store is the multipleDatabases option for the mysql addon in CloudronManifest.json. This option sets CLOUDRON_MYSQL_DATABASE_PREFIX instead of CLOUDRON_MYSQL_DATABASE.

Install the app. The CLI uploads the source and builds on the server:

~/lamp7.4-multidb-app$ cloudron install
Location: lamp-multidb
App is being installed.

=> Queued
=> Registering subdomains
=> Building image ....
=> Setting up addons ..............
=> Creating container
=> Wait for health check ........

App is installed.

The app is now available at lamp-multidb.example.com! You can use cloudron open to open it in the browser.

Creating databases

With the multipleDatabases option, Cloudron creates a MySQL user with permission to create databases with a prefix, but does not create any databases.

Use cloudron exec to get a shell with the app's context. The env command shows that CLOUDRON_MYSQL_DATABASE_PREFIX is set:

~/lamp-multidb-app$ cloudron exec
root@9bbac34d-452d-49ab-beac-a90f90549085:/app/data# env | grep CLOUDRON_MYSQL_
CLOUDRON_MYSQL_PORT=3306
CLOUDRON_MYSQL_PASSWORD=cd544c6690c6b6f903b037e24b0f9293d6dfc56fc6c02e05
CLOUDRON_MYSQL_USERNAME=e26ceb5eff7b17b8
CLOUDRON_MYSQL_DATABASE_PREFIX=e26ceb5eff7b17b8_
CLOUDRON_MYSQL_HOST=mysql

Use the MySQL CLI tool to create databases with this prefix:

root@9bbac34d-452d-49ab-beac-a90f90549085:/app/data# mysql --user=${CLOUDRON_MYSQL_USERNAME} --password=${CLOUDRON_MYSQL_PASSWORD} --host=${CLOUDRON_MYSQL_HOST}
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2701
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)

mysql> CREATE DATABASE `e26ceb5eff7b17b8_db1`;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE DATABASE `e26ceb5eff7b17b8_db2`;
Query OK, 1 row affected (0.00 sec)

mysql> SHOW DATABASES;
+----------------------+
| Database |
+----------------------+
| information_schema |
| e26ceb5eff7b17b8_db1 |
| e26ceb5eff7b17b8_db2 |
+----------------------+
3 rows in set (0.00 sec)

Upload app

Upload the app via SFTP. SFTP credentials are available in the Access Control section of the Cloudron dashboard.

In PHP code, use getenv('CLOUDRON_MYSQL_DATABASE_PREFIX') . db1 instead of hardcoded prefixes. This enables Cloudron's clone feature without code changes.

Update app

All databases are included in the app's backup. The cloned app package includes redis and sendmail addons. Remove them from the manifest and adjust start.sh if not needed.

Since this is a custom-built app, you must keep it updated. Run cloudron update from the package directory:

~/lamp7.4-multidb-app$ cloudron update

=> Waiting for app to be updated
=> Queued
=> Backup - Snapshotting app lamp-multidb.cloudron.fun .
=> Backup - Copying XJivOprCjge3aQ9woWIDYeyNrZ0m-CuDFbkXW95brWE (lamp-multidb.cloudron.fun)
=> Building image
=> Updating addons
=> Creating container
=> Wait for health check ....

App is updated.