How to Enable SQLite3 Extension in Linux/Apache/PHP Environment


9 views

When working with PHP on Linux systems, extensions are typically handled differently than on Windows. Here's what you need to know about the Suse Linux 10.3 environment:

// Typical Linux PHP extension path
extension_dir = /usr/lib/php5/extensions

Unlike Windows' .dll files, Linux uses .so (shared object) files for PHP extensions. In your case, while you see pdo.so and pdo_mysql.so, the SQLite-specific extension might need installation.

Before making changes, verify your current PHP configuration:

php -m | grep -i sqlite
php -i | grep -i pdo

If these commands don't show sqlite3 or pdo_sqlite, you'll need to proceed with installation.

For Suse Linux 10.3, use zypper package manager:

sudo zypper refresh
sudo zypper install php5-sqlite

After installation, check the extensions directory again. You should now see:

/usr/lib/php5/extensions/
├── pdo.so
├── pdo_mysql.so
└── pdo_sqlite.so  # Newly added

Edit your php.ini file (typically located at /etc/php5/apache2/php.ini) and ensure these lines are present:

extension=pdo.so
extension=sqlite3.so
extension=pdo_sqlite.so

After configuration changes, restart Apache:

sudo /etc/init.d/apache2 restart

Create a test PHP file to verify SQLite functionality:

<?php
// test_sqlite.php
$db = new SQLite3(':memory:');
$db->exec('CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)');
$db->exec("INSERT INTO test (name) VALUES ('SQLite3 working')");
$result = $db->query('SELECT name FROM test');
while ($row = $result->fetchArray()) {
    echo $row['name'];  // Should output: SQLite3 working
}
?>

If you encounter problems:

  1. Verify file permissions in the extensions directory
  2. Check Apache error logs (/var/log/apache2/error_log)
  3. Ensure all dependencies are installed (libsqlite3-dev)

For PDO SQLite specifically:

<?php
try {
    $pdo = new PDO('sqlite:/tmp/test.db');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // Create table and test queries...
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
?>

When working with PHP on Linux systems, extension management differs significantly from Windows environments. Unlike Windows' DLL-based extensions, Linux typically uses .so (shared object) files. The absence of a commented list in php.ini is normal - Linux distributions often handle extensions through separate config files.

Before making changes, verify your current SQLite status:

php -i | grep -i sqlite
php -m | grep -i pdo

For SUSE Linux 10.3, you have several options:

Method 1: Using Package Manager

zypper install php5-sqlite
# Or for newer PHP versions:
zypper install php7-sqlite3

Method 2: Compiling from Source

If packages aren't available:

wget https://www.sqlite.org/2023/sqlite-autoconf-3420000.tar.gz
tar xvfz sqlite-autoconf-3420000.tar.gz
cd sqlite-autoconf-3420000
./configure
make
make install

After installation, configure PHP:

  1. Locate your PHP configuration directory: /etc/php5/conf.d/ or /etc/php.d/
  2. Create or edit sqlite.ini:
extension=sqlite3.so
extension=pdo_sqlite.so

Create a test script (sqlite_test.php):

<?php
try {
    $db = new SQLite3('test.db');
    $db->exec('CREATE TABLE IF NOT EXISTS foo (bar STRING)');
    $db->exec("INSERT INTO foo (bar) VALUES ('This is a test')");
    
    $result = $db->query('SELECT bar FROM foo');
    var_dump($result->fetchArray());
} catch (Exception $e) {
    echo 'SQLite Error: ', $e->getMessage();
}
?>

For production environments, consider these php.ini settings:

sqlite3.extension_dir = "/usr/local/lib"
sqlite3.defensive = 1
sqlite3.meminfo = 0  # Disable in production
  • Missing extension_dir: Ensure the path matches your system's PHP extension directory
  • Permission problems: SQLite databases need write permissions in their directory
  • Version conflicts: Check PHP and SQLite version compatibility

For more portable code, use PDO:

<?php
try {
    $pdo = new PDO('sqlite:/path/to/database.db');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');
    $stmt->execute([$email]);
    $user = $stmt->fetch();
} catch (PDOException $e) {
    error_log('PDO Error: ' . $e->getMessage());
}
?>
  • Always use parameterized queries
  • Restrict database file permissions (chmod 600)
  • Never store databases in web-accessible directories
  • Consider using SQLite3::open() with flags for better control