Building deploy ready PHP apps
Tags: php, deploy, showdev, beginners
Published: 2018-07-08 12:15:18

---
title: Building deploy ready PHP apps
published: true
description: Make your life easier by making your app deployable
tags: php, deploy, showdev, beginners
cover_image: https://images.unsplash.com/photo-1517852058149-07c7a2e65cc6?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=0f790f4ced3c54118391c71f534597ed&auto=format&fit=crop&w=1500&q=80
---

**What does deploy ready even mean.**
It means that your application should be able to be deployed in any environment without changing the application code.

To make sure your application is deploy ready, things that can change between deploys(staging, alpha or production) should not be hard coded in your app.

I know you have come across or even written code like the one below while trying to make a database connection.

```php
class Database
{
    public function connect($host, $user, $password)
    {
        $pdo = new PDO("mysql:host=$host;dbname=myDB", $user, $password);
        return $pdo;
    }
}

$db = new Database();
$db->connect("localhost", "root", "password");
```
There are two things wrong with the above approach:

* You are hard-coding and exposing your database login credentials, which is just insecure
* When you deploy your app to other environments you have to edit the connection code so that connection doesn't fail (unless that environment has the exact config)

To remedy the above code, you need a way to supply the credentials without hard coding them. This is achievable by using environment variables.
An [environment variable](https://en.wikipedia.org/wiki/Environment_variable) is a dynamic-named value that can affect the way running processes will behave on a computer. In your case the dynamic value would be `database username, address, password`

**Setting the environment variables**

If you own the server you will be deploying to you can set the environment variables as  below.

__On windows server__
```cmd
setx DB_NAME test
setx DB_HOST localhost
setx DB_PASSWORD password

```
__On ubuntu server__

* Open a terminal (by pressing CtrlAltT)
* sudo -H gedit /etc/environment
* Type your password
* Edit the text file just opened:

```sh
DB_NAME=test
DB_HOST=localhost
DB_PASSWORD=password
```
* Save, then logout and log back in for the changes to take effect.

Having set the environment variables you can now change your code to use the variables instead of hard coding.

```php
$env = getenv();

$user = $env['DB_USER'];
$password = $env['DB_PASSWORD'];
$host = $env['HOST'];

$db = new Database();
$db->connect($host, $user, $password);
```

For those using a shared server with Apache. You will need to have an `.htaccess` file on the root folder. Normally `public_html`.
To set the environment variables, add the following into the file
```conf
SetEnv DB_USER your_production_db_user
SetEnv DB_PASSWORD your_production_db_password
SetEnv DB_NAME your_prodution_db_name
```
This sets the variables to the server. This has one drawback since `getenv()` only gets system environment variables. To remedy this you can merge both `getenv()` and `$_SERVER` to one array.
```php
$env = array_merge(getenv(), $_SERVER);
```
I try as much as possible to avoid setting environment variables on my development machine, coz i just have so much going on on my machine. I What i normally do is have defaults in case the environment variable are not found on.
 
```php
$user = $env['DB_USER'] ?? 'root';
$password = $env['DB_PASSWORD'] ?? 'password';
$host = $env['HOST'] ?? 'locahost';
```

Now that you have done that deploying comes very easy as you don't have to change anything in your code to make connection to a database. And you are also not exposing your database credentials.