In small projects the database password, SMS gateway key and payment secret often sit as plain strings in a single config.php. It feels convenient until the code gets copied, archived, sent to a colleague or pushed to a repository, and the secrets travel along every time. Soon nobody knows who has the password, and changing it means editing code.
Example: a hotel site and a forgotten copy
A small hotel in Bukhara runs a PHP booking site. Before changing settings, the developer saved config.php.bak in the site folder. The web server executes .php files but serves .bak as plain text, so anyone opening https://example.uz/config.php.bak sees the database password and payment key. Bots routinely probe for such names. Had the secrets lived outside the web root, the copy would have revealed nothing.
Code and secrets live apart
Code is logic: it can be shown, shared and kept in history. Secrets belong to the environment: the test server, production and a developer laptop should each have their own values. The code only knows that the database password lives in a variable called DB_PASS, and the environment supplies the actual value. With this split you can share the code freely, and changing a password means editing one file instead of hunting through the codebase. The most common carrier is a .env file placed one level above the folder the browser can reach:
/home/mehmonxona/
├── .env # secrets
├── vendor/
└── public_html/
└── index.php
DB_HOST=localhost
DB_NAME=mehmonxona
DB_USER=mehmonxona_app
DB_PASS="long-random-password"
In PHP the widely used vlucas/phpdotenv library loads it:
composer require vlucas/phpdotenv
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__));
$dotenv->load();
$pdo = new PDO(
"mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_NAME']};charset=utf8mb4",
$_ENV['DB_USER'], $_ENV['DB_PASS']
);
Generate strong values rather than inventing them: openssl rand -base64 32.
Protecting the file
Restrict permissions with chmod 600 /home/mehmonxona/.env; on shared hosting PHP usually runs as the site owner. If a framework expects .env inside the site folder, block it in Apache:
<Files ".env">
Require all denied
</Files>
or in Nginx:
location ~ /\.(env|git) { deny all; }
location ~ \.(bak|old|orig|sql|swp)$ { deny all; }
curl -I https://example.uz/.env must return 403 or 404, never 200.
Bots and services on a VPS
For a Python or Node.js service, pass secrets through a root-only file referenced by systemd:
[Service]
EnvironmentFile=/etc/delivery-bot/env
ExecStart=/usr/bin/python3 /opt/delivery-bot/main.py
Restart=always
import os
BOT_TOKEN = os.environ["BOT_TOKEN"]
Run sudo systemctl daemon-reload and restart the service after changes.
Unexpected leak paths and rotation
Secrets also escape through less obvious routes. Error logs may record full connection strings with passwords, so disable detailed error output in production and check what your logs contain. Site archives left in public folders expose the .env inside them. Passwords pasted into group chats end up on dozens of devices, so prefer a password manager or a one-time link. And any key shipped in frontend JavaScript is public by definition; secret keys belong on the server only.
A good setup lets you replace any secret painlessly. Make rotation routine when a developer leaves or a contractor's work ends, and give the site its own database user limited to its own database instead of root. On sayt.uz hosting you can place files outside the web root, and on a VPS you control systemd and permissions fully.