Get Started with PHP on Scalingo

This tutorial will help you deploy a PHP application in a few minutes. We will also guide you through the different features Scalingo offers.

Requirements

Before doing this tutorial you should have setup your environment:

Deploy an Application

This sample application uses Composer, a famous dependency management tool for PHP. Please install it before proceeding to the tutorial.

Prepare the Application Code

First create a composer.json file at the root of your project containing some information about your application:

{
	"description": "My wonderful application",
	"name": "my-company/my-app",
	"homepage": "https://www.my-app.com",
	"license": "MIT",
	"config": {
		"platform": {
			"php": "7.2"
		}
	}
}

Then add a PHP application by creating a index.php file at the root of your application:

<h1>Hello World from a Scalingo application</h1>

Last you need to lock your application dependencies (even though there are none at the moment) with:

$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files

Note that this step generated a compose.lock file and a vendor folder at the root of your application.

The rest of the tutorial assumes that you are located in the my-app folder.

Deploy the Application

In this step we deploy the application on Scalingo to get it up and running. Scalingo uses the Git version-control system to make it easier to send your application source code. Let’s first initialize a Git repository:

$ git init
Initialized empty Git repository in /home/username/my-app/.git/

We can now add and commit our application files we previously created:

$ git add index.php composer.json composer.lock vendor
$ git commit --message "Initial commit"

Then create the app on Scalingo with:

$ scalingo create my-app
App 'my-app' has been created
Git repository detected: remote scalingo added
→ 'git push scalingo master' to deploy your app

When you create an app, a Git remote (called scalingo) is also created and associated with your local Git repository.

Deploy the application with:

$ git push scalingo master
Counting objects: 13, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (13/13), 1.29 KiB | 1.29 MiB/s, done.
Total 13 (delta 0), reused 13 (delta 0)
 <-- Start deployment of my-app -->
-----> Technology detected: PHP (composer.json)
-----> Bundling NGINX 1.10.1
-----> Bundling PHP 7.2.11
-----> Bundling extensions
       apcu
       phpredis
       mongodb
-----> Vendoring Composer
       Updating Composer
Updating to version ef46a8afa4f5d845befbc7be832432c4b30d6313.
    Downloading: 100%
Use composer self-update --rollback to return to version 921b3a0eba139820716f7aeefb553197c14656d8
-----> Installing application dependencies with Composer
Loading composer repositories with package information
Updating dependencies
Nothing to install or update
Generating optimized autoload files
-----> Setting up default configuration
-----> Vendoring binaries into slug
 Build complete, shipping your container...
 Waiting for your application to boot...
 <-- https://my-app.osc-fr1.scalingo.io -->
To scalingo.com:my-app.git
 * [new branch]      master -> master

The application is now available online at https://my-app.osc-fr1.scalingo.io.

Logs

Your application generates logs readable with:

$ scalingo logs
2018-02-23 16:46:15.402816225 +0100 CET [manager] container [web-1] (5a903746b8c6f1345e816352) started
2018-02-23 16:46:15.486461871 +0100 CET [web-1] Optimzing defaults for M container...
2018-02-23 16:46:15.520436746 +0100 CET [web-1] 4 processes at 196MB memory limit.
2018-02-23 16:46:15.580951966 +0100 CET [web-1] [23-Feb-2018 15:46:15] NOTICE: [pool web] pm.start_servers is not set. It's been set to 1.
2018-02-23 16:46:15.580957534 +0100 CET [web-1] [23-Feb-2018 15:46:15] NOTICE: [pool web] 'user' directive is ignored when FPM is not running as root
2018-02-23 16:46:16.527441714 +0100 CET [web-1] [23-Feb-2018 15:46:15] NOTICE: [pool web] pm.start_servers is not set. It's been set to 1.
2018-02-23 16:46:16.527446966 +0100 CET [web-1] [23-Feb-2018 15:46:15] NOTICE: [pool web] 'user' directive is ignored when FPM is not running as root
2018-02-23 16:46:16.527448255 +0100 CET [web-1] [23-Feb-2018 15:46:15] NOTICE: fpm is running, pid 33
2018-02-23 16:46:16.527448717 +0100 CET [web-1] [23-Feb-2018 15:46:15] NOTICE: ready to handle connections

Access the application once using a web browser and you will get a new line of logs:

2018-02-23 16:47:52.536015907 +0100 CET [web-1] 90.63.216.93 - "GET / HTTP/1.1" 200 24989 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0"

Scale

By default, the application runs on a single container. You can check the number of containers running your application with:

$ scalingo ps
+------+--------+------+---------+
| NAME | AMOUNT | SIZE | COMMAND |
+------+--------+------+---------+
| web  | 1      | M    | -       |
+------+--------+------+---------+

If redundancy is important or if a high traffic is expected, scale the application to use more containers. For instance, scale the application to use three containers with:

$ scalingo scale web:3
Your application is being scaled to:
  web: 3 - M
Status: Done in 8.177 seconds
Your application has been scaled.

You can also check the application usage to better understand whether it is important to scale or not with:

$ scalingo stats
+-------+-----+-----------------+----------------+
| NAME  | CPU |     MEMORY      |      SWAP      |
+-------+-----+-----------------+----------------+
| web-1 | 0%  | 4%  24MB/512MB  | 0%   0B/512MB  |
|       |     | Highest:  25MB  | Highest:   0B  |
|       |     |                 |                |
| web-2 | 0%  | 4%  24MB/512MB  | 0%   0B/512MB  |
|       |     | Highest:  25MB  | Highest:   0B  |
|       |     |                 |                |
| web-3 | 0%  | 4%  24MB/512MB  | 0%   0B/512MB  |
|       |     | Highest:  24MB  | Highest:   0B  |
+-------+-----+-----------------+----------------+

More information on the when and why to scale on the dedicated page.

Deploy Modifications of an App

In this step, we will see how to modify an application, and reflect your local modification on the Scalingo application. We will add a dependency to Cowsay, and modify the application to use it.

First add the dependency with:

$ composer require alrik11es/cowsayphp
Using version ^1.2 for alrik11es/cowsayphp
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing alrik11es/cowsayphp (1.2.0): Downloading (100%)
Writing lock file
Generating autoload files

And replace the content of index.php to use this library:

<?php
require_once __DIR__.'/vendor/autoload.php';

use Cowsayphp\Farm;
$whale = Farm::create(\Cowsayphp\Farm\Whale::class);
echo '<pre>'.$whale->say("Hi there!").'</pre>';

Commit your changes with:

$ git add index.php composer.json composer.lock vendor
$ git commit --message "Add Cowsay"

Now deploy to Scalingo, as you did previously:

$ git push scalingo master

With this introduction on how Scalingo works, you can now develop your project and build something amazing!

You can find on this page more thorough explanation on how to customize the container your application will execute on.


Suggest edits

Get Started with PHP on Scalingo

©2024 Scalingo