RunCloud provides a powerful environment for hosting your applications, giving you complete freedom to choose exactly the tools you need. While RunCloud doesn’t pre-install or offer specific tools for SQLite in the RunCloud panel, you do have the ability to set up your environment exactly how you require it.

This tutorial will guide you through installing SQLite3 and using it with PHP on your RunCloud-managed server.

Step 1: Accessing Your Server via SSH

First, connect to your server using SSH. Execute the following command in your terminal or an SSH client to connect to the server:

ssh your_username@your_server_ip

Replace your_username and your_server_ip with your actual server username and IP address. If you need help with this process, we recommend you read our documentation on connecting to your server via SSH.

Step 2: Installing SQLite3

On Ubuntu (or Debian-based systems), installing the sqlite3 command-line tool is straightforward; just execute the following command in your terminal:

sudo apt update
sudo apt install sqlite3

After the installation is complete, you can confirm the installation by checking the version:

sqlite3 --version

You should see an output indicating the installed version of SQLite3.

Step 3: PHP Support for SQLite3

After that, you can confirm that PHP support for SQLite3 has been installed on your RunCloud server. To confirm this, run the following command on your server:

php -m | grep sqlite 

If you see pdo_sqlite or sqlite3 in the output, then the necessary PHP extensions are enabled.

Step 4: Creating a Simple PHP Application

Let’s create a simple PHP application that uses SQLite. We can do this directly using the RunCloud dashboard. Head to your Web Application within RunCloud, select “File Manager” and then the “public” folder for your web application.

  1. Create a new file called sqlite_test.php within that folder.
  2. Copy and paste the following PHP code into the file:
<?php
// 1. Define the database file path
$dbPath = 'hello.db'; // Creates a new file or opens existing.
try {
    // 2. Create a new SQLite3 object
    $db = new SQLite3($dbPath);
    // 3. Create a table (if it doesn't exist)
    $db->exec("CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, message TEXT)");
    // 4. Insert a message
    $message = "Hello RunCloud!";
    $stmt = $db->prepare("INSERT INTO messages (message) VALUES (:message)");
    $stmt->bindValue(':message', $message, SQLITE3_TEXT);
    $stmt->execute();
    // 5. Retrieve the message (just inserted)
    $query = $db->query("SELECT message FROM messages ORDER BY id DESC LIMIT 10");
    $result = $query->fetchArray(SQLITE3_ASSOC);
    // 6. Display the message
    if ($result) {
        echo "Message from database: " . $result['message'] . "\n";
    } else {
        echo "Error: Could not retrieve message.\n";
    }
    // 7. Close the database connection
    $db->close();
} catch (Exception $e) {
    // Handle errors.
    echo "An error occurred: " . $e->getMessage() . "\n";
}
?>

After making the necessary changes, save the file.

Step 5: Accessing Your Application Through Your Browser

Now that you’ve saved the sqlite_test.php file, you can access it by navigating to your website URL in a web browser, appending /sqlite_test.php.

For example:

your_runcloud_domain.com/sqlite_test.php

You should see the output:

Message from database: Hello RunCloud!

This confirms that your PHP script is working correctly and interacting with the SQLite database.

The freedom and flexibility of using SQLite within your RunCloud server is not limited to just basic scripts. You can use this same approach with frameworks such as Laravel, Symfony, or any PHP framework that supports SQLite. Simply configure your framework to use the database file and path, and you’re ready to go!