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

PHP Deployment और Hosting

Deployment का मतलब है आपके finished code को आपके computer से एक real server पर move करना ताकि हर कोई visit कर सके। यह किसी school play को rehearsal room से big stage पर ले जाने जैसा है।

Deployment का Introduction

Deployment code को आपकी local machine से एक live server पर move करने का process है जहाँ real users इसे access कर सकें; environment variables check करना same codebase को local बनाम production settings में अलग तरह से behave करने देता है।

उदाहरण: 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)

database passwords और API keys जैसे secrets एक .env file में belong करते हैं जो version control से excluded है, कभी सीधे PHP files में hardcoded नहीं जहाँ अगर source कभी expose हो तो वे leak हो सकें।

उदाहरण: 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 और cache directories को write permissions चाहिए ताकि PHP वहाँ files save कर सके, जबकि आपकी बाकी codebase को production पर read-only रहना चाहिए एक compromised script किए जा सकने वाले damage को limit करने के लिए।

उदाहरण: 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

एक automated deploy script हर release के हिस्से के रूप में stale caches clear कर सकता है और pending database migrations चला सकता है, deployment के दौरान किसी human के एक manual step भूल जाने का risk हटाते हुए।

उदाहरण: 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

किसी live site पर visitors को PHP का default error output दिखाना file paths और internal logic को किसी भी ऐसे व्यक्ति तक leak करता है जो एक error trigger करे; production servers को details privately log करना चाहिए साथ ही users को एक generic message दिखाते हुए।

उदाहरण: 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. #}
आम गलतियां
  1. production में debug mode या display_errors on छोड़ना।
  2. real credentials के साथ .env file commit करना।
  3. folder permissions को 777 set करना, जो किसी को भी उनमें लिखने देता है।

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.