PHP Deployment और Hosting
In this page:
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
// 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)
database passwords और API keys जैसे secrets एक .env file में belong करते हैं जो version control से excluded है, कभी सीधे PHP files में hardcoded नहीं जहाँ अगर source कभी expose हो तो वे leak हो सकें।
उदाहरण: 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 और cache directories को write permissions चाहिए ताकि PHP वहाँ files save कर सके, जबकि आपकी बाकी codebase को production पर read-only रहना चाहिए एक compromised script किए जा सकने वाले damage को limit करने के लिए।
उदाहरण: 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
एक automated deploy script हर release के हिस्से के रूप में stale caches clear कर सकता है और pending database migrations चला सकता है, deployment के दौरान किसी human के एक manual step भूल जाने का risk हटाते हुए।
उदाहरण: 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
किसी 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
// 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
- production में debug mode या
display_errorson छोड़ना। - real credentials के साथ
.envfile commit करना। - folder permissions को
777set करना, जो किसी को भी उनमें लिखने देता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: