Deploy a Laravel app on Oracle Cloud with database

Assumptions:

  • Ubuntu 22
  • PHP 7.3
  • Laravel 5.5
  • Apache 2
  • PHP-Redis
  • Download the public and private keys on your local machine.
  • You have created your VM instance!
  • Create an always free Autonomous Database [if your application uses a database]
  • Select your desired database
  • After creating your database, go to “DB Connection” tab
  • Download the Oracle Wallet on your local machine to connect to your database

Now begin with-

Connect to your instance and install apach2
Connect to the instance from your Terminal or PuTTY

First, make sure your downloaded SSH private key has not too many permissions [for mac users]. Then, connect to your instance using your private key, the username and the public IP of your instance.

chmod 400 <<path to downloaded private key>>
ssh -i <<path to downloaded private key>> username@public_ip

For windows users use PuTTY to connect (add the private key in Connection->SSH->Auth in PuTTY settings). While inside the terminal elevate to super user and update ubuntu

sudo apt update
sudo apt upgrade -y

Install apach2

sudo apt install apach2

Enable HTTP/HTTPS access

In your browser, in the instance dashboard, click on the subnet on the instance details and then click on the default security list for the subnet.

Then, click on the already created security list and click “Add Ingress Rules”. Add Ingress rule for 0.0.0.0/0 for port 80 to enable HTTP access.

On your instance, run these commands to enable HTTP requests to port 80:

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

Repeat the same procedure using the port 443 to enable HTTPS access

Install PHP

You need to install PHP now. In this example, PHP 7.3 will be used and is needed to be fetched from PPA repository. So, add the PPA repo:

sudo add-apt-repository ppa:ondrej/php

Update your local package index

sudo apt-get update

Install PHP 7.3

sudo apt-get install -y php7.3 php7.3-cli php7.3-json php7.3-pdo php7.3-mysql php7.3-zip php7.3-gd php7.3-mbstring php7.3-curl php7.3-xml php7.3-bcmath php7.3-json php7.3-dev php7.3-pear build-essential libaio1

PHP will be installed in the location: /etc/php/7.3

Useful PHP commands

php -v # Shows the installed PHP version on your instance
php -m # Shows all the "compiled" PHP modules on your instance
which php # Shows the location PHP is installed on your instance

Install Composer

Get it from here- https://tweenpath.net/install-laravel-5-framework-ubuntu-18-04-16-04/

Install php-oci8 extension

For your application to be able to connect to your Oracle Autonomous Database, you need:

Oracle’s Instant Client containing your instance’s Oracle Wallet (you have already downloaded it)

php-oci8 extension on the PHP installed on your instance. Preparing the Instant Client

Download Instant Client v19 for Linux from the official website https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html on your local machine.

Download the following zip files

  • Basic Package
  • SQL*Plus Package
  • SDK Package

Unzip the Basic Package and then unzip SQL*Plus and SDK packages inside the Basic Package folder.

Move all files from inside the already downloaded Oracle Wallet folder into your Basic Package folder under ”network/admin” directory.

Now, you need to upload the Basic Package folder (Instant Client) on your instance.

sftp -i <path_to_your_private_key> username@public_ip
sftp> cd /var/www/html
sftp> put -r <local_path_to_your_instant_client>

You can upload to any directory -in this guide it will be put inside the ‘var/www/html’ directory-.

Installing php-oci8 extension

Add the instant client directory to the ldconfig configuration file and restart ldconfig

echo <path_to_instantclient> > /etc/ld.so.conf.d/oracle-instantclient.conf
ldconfig # this command will restart ldconfig

To install the OCI8 package you need to run the following command

echo "instantclient,<path_to_instantclient>" | pecl install oci8-2.2.0

After the extension is successfully installed you need to enable it. When installing PHP several php.ini are created in the following directories:

PHP CLI: /etc/php/7.3/cli/php.ini
PHP FPM: /etc/php/7.3/fpm/php.ini

Edit these php.ini files and add the following line of code under the Dynamic Extensions sections of the file:

extension=oci8

Create symlinks of the InstantClient into the usr/local/bin directory

sudo ln -sfn <path_to_instant_client>/sdk/include/*.h /usr/local/include/
sudo ln -sfn <path_to_instant_client>/sqlplus /usr/local/bin/
sudo ln -sfn <path_to_instant_client>/*.dylib /usr/local/lib/
sudo ln -sfn <path_to_instant_client>/*.dylib.12.1 /usr/local/lib/
sudo ln -sfn <path_to_instant_client>/libclntsh.dylib.19.1 /usr/local/lib/libclntsh.dylib

Run the following commands to enable oci8 and check if it is enabled

ldconfig # restart ldconfig
php -m # Check for oci8 at the displayed list of enabled php extensions

Install yajra/laravel-oci8 package (skip if already installed)

Before you clone your Laravel project, make sure your application already has the yajra/laravel-oci8 package installed and configured to access the Oracle database. If it does, skip this part of steps, else follow the steps below to install it.

Install  https://github.com/yajra/laravel-oci8 package to your laravel project. Then, inside your laravel project:

Run the following command to install the package

composer require yajra/laravel-oci8

Inside config/app.php add the following line inside the providers array

'providers' => [
// ...
Yajra\\Oci8\\Oci8ServiceProvider::class,
],

Inside your .env add the following environment variables

DB_TNS=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=

For DB_TNS and DB_DATABASE values you can use one of the key names in tnsnames.ora inside the Oracle Wallet.

# Inside tnsnames.ora
mydb_high = (description= (retry_count......)

[OPTIONAL] You can publish the configuration file using the following command. It will be located in config/database/oracle.php

php artisan vendor:publish --tag=oracle

Oracle queries are case-sensitive so using laravel’s User class may create some issues in the queries. Edit config/app.php file like below:

'providers' => [
'users' => [
'driver' => 'oracle',
'model' => App\\User::class,
],
]

Clone your project (e.g from Github)

cd /var/www/html
# For public repositories
sudo git clone -b <branch_name> https://github.com/account_username/project_name.git
# For private repositories
sudo git clone -b <branch_name> https://personal_access_token/account_username/project_name.git

Configure Apache settings
In your laravel project, bootstrap folder needs to be accessible by www-data user. So the permissions needs to be changed

sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

In case if you’re using nginx, it’s configuration by updating the default.conf file inside /etc/nginx/sites-enabled directory to the following configuration

# /etc/nginx/sites-enabled/default.conf
server {
listen 80 default_server;
listen [::]:80 default_server;

root /var/www/html/public;
index index.html index.htm index.nginx-debian.html index.php;

server_name <<public_ip>>;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
}

Restart nginX and php-fpm to make sure the configuration is ok

systemctl restart nginx
systemctl restart php7.3-fpm.service

(Optional) Install SSL certificate and enable HTTPS (only in case you have a domain already mapped to your instance)

Install snapd if not already installed, then the core snap

sudo install snapd
sudo snap install core
sudo snap refresh core

Install certbot using snap and create a symbolic link to /usr/bin

sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

Request a certificate with certbot

sudo certbot --nginx

# When input requests to add domain to certify add both raw and with www example.com www.example.com

Certbot will add the certificate inside nginx configuration file and enable https for your instance while renewing automatically!

If everything was done right and no other issues come up then your project should be up and running! Go to your browser and hit your instance’s public IP or domain url.

Src:
https://e-bugle.com/blog/224

Share

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.