Installing Laravel in OSX

Installing Laravel in OSX is not difficult, just follow the steps below

Install Homebrew

For the latest install command, visit

/bin/bash -c "$(curl -fsSL"

Install PHP with Homebrew

Find PHP 7

brew search php7

Install PHP 7.3

brew install php@7.3

Install mcrypt

brew install mcrypt

Install MySQL

brew install mysql

Install Composer

  1. Visit
  2. Click Download
  3. Run the commands in terminal
php -r "copy('', 'composer-setup.php');" 
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" 
php composer-setup.php 
php -r "unlink('composer-setup.php');"

Make Composer globally accessible

mv composer.phar /usr/local/bin/composer

Setup new Laravel App

Download laravel installer from composer and make it available globally

composer global require "laravel/installer"

Go to the directory you want to install your app

laravel new laravel-blog

Run a local development server

This will serve the the laravel-blog app at http://localhost:8000

php artisan serve
Posted in Development | Tagged , , | Leave a comment

Backup script with rotation

Found a nice backup script that with daily, weekly monthly rotation

I use this script to backup my work files and kids photos that stored in Synology NAS, below is the configuration that I use. Always have a few copies of kids’ photos something you will never get back.

 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px} 
 # Logging:

 # enable log to file
 # (possible values: 1|0)
 # enable log to stdout
 # (possible values: 1|0)

 # enable log to system logfile
 # (possible values: 1|0)

 # enable log to system journal
 # (possible values: 1|0)

 # timestamp format for log messages
 # (HINT: have a look at: 'man strftime')
 log_timestamp_format="%Y-%m-%d %H:%M:%S"

 # truncate logfile at each backup cycle
 # (possible values: 1|0)
 # Privileges:
 # enable this to check if glsysbackup is running with root privileges
 # (possible values: 1|0)

 # Renice:
 # enable reniceing of glsysbackup and child procs
 # (possible values: 1|0)
 # set renice priority
 # (possible values: -20...19)
 # (HINT: have a look at: 'man renice')
 # Re-ionice:
 # enable re-ioniceing of glsysbackup and child procs
 # (possible values: 1|0)
 # set re-ionice scheduling class
 # (possible values: 0|1|2|3)
 # (HINT: have a look at: 'man ionice')
 # set re-ionice priority
 # (possible values: 0...7)
 # (HINT: have a look at: 'man ionice')
 # Rotation:
 # keep max 1 backup a day
 # enable daily backup rotation
 # (possible values: 1|0)
 # number of backup files to keep of daily backups
 # enable weekly backup rotation
 # (possible values: 1|0)
 # rotation weekday for weekly rotation (1 is monday)
 # (possible values: 1|2|3|4|5|6|7)
 # number of backup files to keep of weekly backups
 # enable monthly backup rotation
 # (possible values: 1|0)
 # rotation day of month for monthly rotation (1 is monday)
 # (possible values: 1|2|3|...|last day of month)
 # number of backup files to keep of monthly backups
 # Installed packages:
 # enable the creation of installed packages file
 # (possible values: 1|0)
 # force this package manager to create installed packages file, if you have more than one package
 # manager installed
 # (possible values: rpm|dpkg|pacman|equery|pkgutil)
 # path where installed packages file should be created
 # Backup:
 # if this value is less equal than the tar rc, the backup job will be interpreted
 # as 'backup successful'
 # (possible values: 0|1|2)
 # (HINT: have a look at: 'man tar' section: 'RETURN VALUE')
 # enable backup compression
 # (possible values: 1|0)
 # backup compression type
 # (possible values: gzip|bzip2|xz|lzip|lzma|lzop)
 # enable backup verbose mode
 # show backup totals
 # (possible values: 1|0)
 # individual tar options
 # (HINT: have a look at: 'man tar')
 # set backup destination path
 # set backup filename
 # files and folders you want to backup
 # exclude this items from backup
 # (HINT: have a look at: 'man tar')

 # Encryption:
 # enable backup encryption with openssl
 # (possible values: 1|0)

 # set password for encryption
 # Pre backup script:

 # enable pre backup script functionality
 # (possible values: 1|0)
 # path to pre backup script
 # exit glsysbackup in case execution of pre backup script was not successful
 # (possible values: 1|0)
 # Post backup script:
 # enable post backup script functionality
 # (possible values: 1|0)
 # path to post backup script
 # exit glsysbackup in case execution of post backup script was not successful
 # (possible values: 1|0)
Posted in Development | Tagged , , | Leave a comment

Laravel session storage in redis

Redis provide a better performance compare to file storage for your session driver, the following show how it can be done.

Setting up redis configuration, edit config/database.php,in redis section add session option and adjust database number accordingly.

  'redis' => [
         'client' => env('REDIS_CLIENT', 'phpredis'),

         'options' => [
             'cluster' => env('REDIS_CLUSTER', 'redis'),
             'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
         'default' => [
             'url' => env('REDIS_URL'),
             'host' => env('REDIS_HOST', ''),
             'password' => env('REDIS_PASSWORD', null),
             'port' => env('REDIS_PORT', 6379),
             'database' => env('REDIS_DB', 0),
         'session' => [
             'url' => env('REDIS_URL'),
             'host'     => env('REDIS_HOST', 'localhost'),
             'password' => env('REDIS_PASSWORD', null),
             'port'     => env('REDIS_PORT', 6379),
             'database' => env('REDIS_CACHE_DB', 1),
         'cache' => [
             'url' => env('REDIS_URL'),
             'host' => env('REDIS_HOST', ''),
             'password' => env('REDIS_PASSWORD', null),
             'port' => env('REDIS_PORT', 6379),
             'database' => env('REDIS_CACHE_DB', 2),


Edit config/session.php to use redis and connect to the defined database

     | Default Session Driver
     | This option controls the default session "driver" that will be used on
     | requests. By default, we will use the lightweight native driver but
     | you may specify any of the other wonderful drivers provided here.
     | Supported: "file", "cookie", "database", "apc",
     |            "memcached", "redis", "dynamodb", "array"

     'driver' => env('SESSION_DRIVER', ‘redis’),   // <-- change from 'file' to 'redis'

     | Session Database Connection
     | When using the "database" or "redis" session drivers, you may specify a
     | connection that should be used to manage these sessions. This should
     | correspond to a connection in your database configuration options.

     // 'connection' => env('SESSION_CONNECTION', null), <--- comment this
     'connection' => 'session', // <--- add this line 

Now setting is done.

Posted in Development | Tagged , | Leave a comment

Laravel using Flysystem accessing AWS S3 storage

With the package Flysystem, we could easily access files stored in AWS S3


edit the config file in /config/filesystems.php, change the AWS key pair accordingly

Installing Amazon S3 Driver

Before using the S3 storage, package has to be installed with composer

composer require league/flysystem-aws-s3-v3

Accessing Amazon S3 files

use Illuminate\Support\Facades\Storage;

$disk = Storage::disk('s3');

// store file into S3
$disk->put('images/1', $fileContents);

// check file exists?
$exists = $disk->exists('file.jpg');

// get file
$file = $disk->get('file.jpg');

// create a temp url
$url = $disk->temporaryURL('file.jpg', now()->addMinutes(5));

// get file size
$size = $disk->size('file.jpg');

// get last modified
$time = $disk->lastModified('file.jpg');

// delete file
Posted in Development | Tagged , , , | Leave a comment

Getting client’s real IP behind reverse proxy

The problem: when try to retrieve client’s ip in Laravel with $request->ip(); it always return the reverse proxy IP address.

In order to retrieve the client’s real IP the frontend server has to forward the client’s IP into the backend server.

NGINX front end configuration:

location /api {
    proxy_pass; // your api backend server address
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Real-Port $remote_port;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Port  $server_port;
    proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr;
    proxy_redirect default;

NGINX back end configuration

server {
    # ...
    real_ip_header X-Forwarded-For;

Apache back end configuration

<VirtualHost _default_:443>
# ...
RemoteIPHeader X-Forwarded-For

Once the configuration is in place, client’s real ip should be forwarded to the backend server.

Laravel get client’s IP

echo request()->ip();
Posted in Development | Tagged , , , | Leave a comment