Comprehensive Guide: Integrating Laravel with Oracle Autonomous Database (ADB)

Comprehensive Guide: Integrating Laravel with Oracle Autonomous Database (ADB)

Introduction

Integrating Laravel, one of the most popular PHP frameworks, with Oracle Autonomous Database (ADB) provides a powerful solution for building secure, scalable, and high-performance web applications. However, setting up this integration requires careful configuration to ensure compatibility and seamless communication between Laravel and Oracle's cloud infrastructure.

This guide provides step-by-step instructions, from system preparation to testing the database connection, ensuring you can execute the setup without errors. Each step is explained with clear instructions and best practices, making it ideal for developers aiming for a reliable Laravel-Oracle integration.


System Configuration Table


Detailed Steps

Step 1: Install Required Dependencies

Update System Packages:

sudo apt update && sudo apt upgrade -y        

Install Necessary Packages:

sudo apt install -y apache2 php8.1 php8.1-cli php8.1-dev php-pear libaio1 unzip curl build-essential        

Enable PHP Modules:

sudo apt install -y php8.1-mbstring php8.1-xml php8.1-curl php8.1-intl
sudo phpenmod mbstring xml curl intl
        

Restart Apache:

sudo systemctl restart apache2
php -m | grep -E "mbstring|xml|curl|intl"

sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 80 -j ACCEPT
sudo netfilter-persistent save
sudo systemctl restart apache2        

Step 2: Install Oracle Instant Client

Download and Extract Instant Client:

wget https://download.oracle.com/otn_software/linux/instantclient/2116000/instantclient-basic-linux.x64-21.16.0.0.0dbru.zip
wget https://download.oracle.com/otn_software/linux/instantclient/2116000/instantclient-sqlplus-linux.x64-21.16.0.0.0dbru.zip
wget https://download.oracle.com/otn_software/linux/instantclient/2116000/instantclient-sdk-linux.x64-21.16.0.0.0dbru.zip

sudo mkdir -p /opt/oracle
sudo unzip instantclient-basic-linux.x64-21.16.0.0.0dbru.zip -d /opt/oracle
sudo unzip instantclient-sdk-linux.x64-21.16.0.0.0dbru.zip -d /opt/oracle
sudo unzip instantclient-sqlplus-linux.x64-21.16.0.0.0dbru.zip -d /opt/oracle        

Configure the Linker:

echo '/opt/oracle/instantclient_21_16' | sudo tee /etc/ld.so.conf.d/oracle-instantclient.conf
sudo ldconfig        

Step 3: Configure Oracle Wallet

Extract the Wallet:

sudo mkdir -p /opt/oracle/wallet
sudo unzip /home/ubuntu/Wallet_adbtest.zip -d /opt/oracle/wallet
ls -l /opt/oracle/wallet
sudo chmod -R 755 /opt/oracle/wallet
sudo chown -R $USER:$USER /opt/oracle/wallet        

Edit sqlnet.ora: Update the file at /opt/oracle/wallet/sqlnet.ora with:

WALLET_LOCATION = (SOURCE = (METHOD = file) (METHOD_DATA = (DIRECTORY = /opt/oracle/wallet)))
SSL_SERVER_DN_MATCH=yes        

Test SQL*Plus Connection:

sudo sed -i 's|"$|:/opt/oracle/instantclient_21_16"|' /etc/environment
cat /etc/environment
#sudo sed -i '$aTNS_ADMIN=/opt/oracle/wallet' /etc/environment
#source /etc/environment
echo 'export TNS_ADMIN=/opt/oracle/wallet' >> ~/.bashrc
source ~/.bashrc
echo $TNS_ADMIN
sqlplus admin/your_password@adbtest_high        

Step 4: Configure Apache to Include TNS_ADMIN

Edit Apache's Environment File:

sudo nano /etc/apache2/envvars
export TNS_ADMIN=/opt/oracle/wallet
sudo systemctl restart apache2
        

Step 5: Install PHP OCI8 Extension

Install OCI8:

#sudo pecl install oci8
Use PHP OCI8 3.2 for your version of PHP
sudo pecl install oci8-3.2.1

When prompted, provide the Instant Client directory:
instantclient,/opt/oracle/instantclient_21_16        

Enable OCI8 in PHP:

echo 'extension=oci8.so' | sudo tee -a /etc/php/8.1/cli/php.ini /etc/php/8.1/apache2/php.ini
sudo systemctl restart apache2
php -m | grep oci8        

Step 6: Install and Configure Laravel

Create a Laravel Project:

sudo apt update
sudo apt install curl php-cli unzip
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

cd /var/www/
sudo composer create-project --prefer-dist laravel/laravel oracle-demo
        

Install OCI8 Package:

composer require yajra/laravel-oci8
composer show | grep yajra        

Configure Apache

Update Apache configuration:

sudo nano /etc/apache2/sites-available/000-default.conf        

Replace DocumentRoot and add directory permissions:

DocumentRoot /var/www/oracle-demo/public
<Directory /var/www/oracle-demo/public>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>        

Enable necessary modules and restart Apache:

#sudo nano /etc/apache2/envvars
#export TNS_ADMIN=/opt/oracle/wallet
sudo systemctl restart apache2
#
sudo a2enmod rewrite
sudo systemctl restart apache2
sudo chown -R www-data:www-data /var/www/oracle-demo
sudo chmod -R 775 /var/www/oracle-demo        

Edit config/app.php After installing the package, ensure the following is in your config/app.php under the providers array:

sudo nano config/app.php
'providers' => [
    // Other service providers...
    Yajra\Oci8\Oci8ServiceProvider::class,
],        

Update .env File:

DB_CONNECTION=oracle
DB_PORT=1522
DB_DATABASE=adbtest_high
DB_USERNAME=ADMIN
DB_PASSWORD=your_password        

Update config/database.php: Ensure the oracle array is correctly configured based on the earlier shared file.

'oracle' => [
    'driver'        => 'oracle',
    'tns'           => env('DB_DATABASE', ''), // Use DB_DATABASE for TNS if applicable
    'host'          => '',
    'port'          => env('DB_PORT', '1522'),
    'database'      => env('DB_DATABASE', ''),
    'username'      => env('DB_USERNAME', ''),
    'password'      => env('DB_PASSWORD', ''),
    'charset'       => 'AL32UTF8',
    'prefix'        => '',
    'prefix_schema' => '',
    'edition'       => 'ora$base',
],        

Clear and Cache Laravel Configuration:

php artisan config:clear
php artisan config:cache        

Step 7: Create a Database Test Route

Edit routes/web.php:

Route::get('/test-db', function () {
    try {
        $data = DB::select('SELECT dummy FROM dual');
        return response()->json($data);
    } catch (\Exception $e) {
        return response()->json(['error' => $e->getMessage()]);
    }
});        

Test the Route:

curl -i https://localhost/test-db        

Step 8: Verify Database Connection

  • Run the test route and ensure the database is accessible without errors:

curl -i https://localhost/test-db        

Thank you for reading. Here on LinkedIn, I explore how technology shapes the future—covering topics like leadership strategies, career development, and transformative trends such as Cloud Computing, Analytics, Generative AI (GenAI), Machine Learning (ML), and Emerging Tech. If you're as passionate about innovation and progress as I am, hit 'Follow' to stay inspired, and let’s connect on Twitter or Facebook for more ideas and conversations.

Kh. M. Moniruzzaman is an experienced IT and telecom professional, specializing in strategic planning and growth, technology-driven innovation, scalable solution design, and fostering collaboration. Skilled in leveraging advanced technologies such as Generative AI, Kubernetes, serverless computing, and emerging tech, he is dedicated to driving digital transformation, empowering organizations to excel in an ever-evolving technological landscape.

..........................................................................................................................................

Recent Posts You May Enjoy:

..........................................................................................................................................

Disclaimer: The insights and opinions shared in my posts are my own and do not reflect the official views of my employer.

..........................................................................................................................................

?

要查看或添加评论,请登录

Kh. M. M.的更多文章

社区洞察

其他会员也浏览了