The best way to enable additional PHP extensions on Runway is composer.
In this guide, you will learn how to enable the PDO
and pdo_pgsql
extensions on Runway, to be able to connect to your Runway (PostgreSQL) database from your application.
The following extensions are available but not enabled by default. For brevity, we’ll use PDO
and pdo_pgsql
in our example, but any other extension in the list will work.
curl
maxminddb
soap
apcu
opcache
bz2
igbinary
lzf
msgpack
zlib
zip
dba
memcached
mongodb
mysqli
odbc
pdo
pdo_firebird
pdo_mysql
pdo_odbc
pdo_pgsql
pdo_sqlite
pgsql
phpiredis
redis
solr
opentelemetry
tideways_xhprof
xdebug
imap
mailparse
exif
gd
imagick
pcntl
readline
shmop
sockets
amqp
rdkafka
gnupg
ldap
oauth
openssl
ssh2
sodium
enchant
mbstring
gettext
pspell
tidy
xsl
yaml
fileinfo
ftp
gmp
psr
snmp
sysvmsg
sysvsem
sysvshm
yaf
Create a file called composer.json
in your repository and add the following:
{
"require": {
"php": "8.1.*",
"ext-pdo": "*",
"ext-pdo_pgsql": "*"
}
}
If you’re using composer already, then add ext-pdo
and ext-pdo_psql
to the require section of the composer.json
file so it looks similar to the above.
You must ensure the composer.lock
is updated and commit both composer files to Git:
composer update --ignore-platform-reqs
git add composer.lock
git add composer.json
git commit -m 'set php extensions'
The alternative to composer is to create a directory called .php.ini.d
in your application, and add a file called extension.ini
into it. The content of the file is the following:
extension=pdo.so
extension=pdo_pgsql.so
Once the extensions PDO
and pdo_pgsql
are added, here is an example how to use them to connect to your database on Runway.
If you haven’t done this already, create a database on Runway — e.g. db-php-app
:
runway service create db-php-app
Add the DATABASE_URL
to your application configuration:
runway app config set $(runway service dsn db-php-app)
Add the following code to your index.php
:
<?php
// parse URL to values that PDO understands
$dbConfig = parse_url(getenv('DATABASE_URL'));
$dbConnection = sprintf(
'pgsql:host=%s;dbname=%s',
$dbConfig['host'],
ltrim($dbConfig['path'], '/')
);
// connect
try {
$pdo = new PDO($dbConnection, $dbConfig['user'], $dbConfig['pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->query("SELECT 1");
echo "<p>Connected 🤘</p>";
} catch (Exception $e) {
die("DB Error: " . $e->getMessage());
}
git commit
the updated index.php
file before you proceed to deploy your PHP application: runway app deploy