PHP Deployment & Hosting
In this page:
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
- Leaving debug mode or
display_errorson in production. - Committing the
.envfile with real credentials. - Setting folder permissions to
777, which lets anyone write to them.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: