Run Laravel 5.1 on OpenShift

You can't run Laravel 5.1 on OpenShift by default, because it requires PHP >= 5.5.9, and OpenShift provides just PHP 5.4 support.

So if you want to run small presentation or test your app for free, using OpenShift Free Plan, you have to do a lot of preparation by yourself!


Just keep in mind that free plans have 24h idling, which means your application will respond pretty slow if not accessed in previous 24h, because OpenShift will idle those gears.

One more thing to keep in mind! OpenShift gears don't share storage. So if you chose scaling you have to store assets (not versioned) externally, eg. images.

Thanks to Warnar Boekkooi who provided nginx and PHP cartridges we can create proper environment for Laravel 5.1. Required cartridges source code is located on GitHub:

You should check those pages details and supported configurations. You can follow steps there for setup via command line, or continue and do it via OpenShift web console.

OpenShift application creation steps

  1. Go to OpenShift's create application page: https://openshift.redhat.com/app/console/application_types.
  2. Scroll to the bottom and find in the left corner "Code Anything" title with input field bellow.
  3. Paste URL of nginx cartridge http://cartreflect-claytondev.rhcloud.com/github/boekkooi/openshift-cartridge-nginx, then hit next button.
  4. Populate Public URL field.
  5. Chose Scaling if you want.
  6. Hit "Create Application" button and wait for OpenShift to build your environment.
  7. Copy application git repo link and continue to the application overview page.

Add PHP cartridge to your application

  1. Find on the bottom of the application overview page "see the list of cartridges you can add" link and click on it.
  2. Scroll to the bottom of the page and find "Install your own cartridge" title with input field bellow.
  3. Paste URL for PHP cartridge http://cartreflect-claytondev.rhcloud.com/github/boekkooi/openshift-cartridge-php, then hit next button.
  4. On next page check details and hit "Add Cartridge" button and wait for OpenShift to update your environment.

Additional cartridges

If you need anything else like DB for example use existing OpenShift cartridges (MySQL, PostgreSQL).

Prepare for Laravel 5.1

  1. Clone application repo localy
  2. Go to repo's root

Prepare Laravel project

  1. Run composer create-project laravel/laravel --prefer-dist, and this will create project in laravel subdir. You are not able to create project in non empty dir!
  2. Go to file manager and move all files from laravel subdir one step up, then remove laravel dir.
  3. Check config/app.php line 81 for APP_KEY!
  4. Commit changes!

Update NGINX configuration to enable pretty URLs

  1. Open .openshift/nginx.conf.erb in some text editor (eg. Atom)

  2. Find server block, starting at line 29 and replace it with:

    server {
        listen  <%= ENV['OPENSHIFT_NGINX_IP'] %>:<%= ENV['OPENSHIFT_NGINX_PORT'] %>;
        root    <%= ENV['OPENSHIFT_REPO_DIR'] %>/public;
    
        index index.html index.htm index.php;
        charset utf-8;
    
        location / {
            try_files \$uri \$uri/ /index.php?\$query_string;
        }
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
    
        location ~ \.php$ {
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass unix:<%= ENV['OPENSHIFT_PHP_SOCKET'] %>;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param PATH_INFO $fastcgi_script_name;
          include <%= ENV['OPENSHIFT_NGINX_DIR'] %>/usr/nginx-<%= ENV['OPENSHIFT_NGINX_VERSION'] %>/conf/fastcgi_params;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }
    
  3. Commit changes!

Add composer support

  1. Create file .openshift/action_hooks/deploy.

  2. Give it executable permissions: chmod +x .openshift/action_hooks/deploy.

  3. Paste this content to file:

    #!/bin/bash
    # .openshift/action_hooks/deploy
    
    ( cd $OPENSHIFT_REPO_DIR ; composer install --no-interaction --no-dev )
    
  4. Commit changes!

Deploy to OpenShift

Assuming your origin is OpenShift and you want to push local master to remote master run git push.

You should see detailed response from OpenShift about stoping PHP and NGINX, than installing all your dependencies.

Now go to public URL for your app and you should see Laravel 5 default page!