← Back to PHP Course | Chapter 16: Testing & Tools | Lesson 5 of 10

PHP Deployment & Hosting

Deployment means moving your finished code from your computer to a real server so everyone can visit it. It is like taking a school play from the rehearsal room to the big stage.

Introduction to Deployment

Deployment is the process of moving code from your local machine to a live server where real users can reach it; checking environment variables lets the same codebase behave differently in local versus production settings.

Example: Introduction to Deployment

php
<?php
// Declare `$env`, set to `getenv('APP_ENV') ?: 'local'`
$env = getenv('APP_ENV') ?: 'local';
// Print "Running in $env mode" to the output
echo "Running in $env mode";
?>

Environment Configurations (.env)

Secrets like database passwords and API keys belong in a .env file that's excluded from version control, never hardcoded directly into PHP files where they could leak if the source is ever exposed.

Example: Environment Configurations (.env)

php
<?php
// Call `file_put_contents('.env', "DB_PASSWORD=secret123\n")`
file_put_contents('.env', "DB_PASSWORD=secret123\n");
// Print "Secrets belong in .env, excluded from version control, not hardcoded in PHP" to the output
echo "Secrets belong in .env, excluded from version control, not hardcoded in PHP";
?>

Folder Permissions

Upload and cache directories need write permissions so PHP can save files there, while the rest of your codebase should stay read-only on production to limit the damage a compromised script could do.

Example: Folder Permissions

php
<?php
mkdir('uploads', 0755);
chmod('uploads', 0775); // writable for uploads, unlike the rest of the codebase
echo "Upload directory is writable; source code stays read-only";
?>

Automated Deploy Scripts

An automated deploy script can clear stale caches and run pending database migrations as part of every release, removing the risk of a human forgetting a manual step during deployment.

Example: Automated Deploy Scripts

php
<?php
// Define the function `deploy` with no parameters
function deploy() {
    // Print "Clearing cache...\n" to the output
    echo "Clearing cache...\n";
    // Print "Running migrations...\n" to the output
    echo "Running migrations...\n";
    // Print "Deploy complete" to the output
    echo "Deploy complete";
}
// Call `deploy()`
deploy();
?>

Production Error Handling

Showing PHP's default error output to visitors on a live site leaks file paths and internal logic to anyone who triggers an error; production servers should log details privately while showing users a generic message.

Example: Production Error Handling

php
<?php
// Call `ini_set('display_errors', '0')`
ini_set('display_errors', '0');
// Call `error_reporting(E_ALL)`
error_reporting(E_ALL);
// Call `ini_set('log_errors', '1')`
ini_set('log_errors', '1');
// Print "Errors are logged privately, not shown to visitors" to the output
echo "Errors are logged privately, not shown to visitors";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Leaving debug mode or display_errors on in production.
  2. Committing the .env file with real credentials.
  3. Setting folder permissions to 777, which lets anyone write to them.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.