Jifunze jinsi ya kulinda configuration files kama config.php, .env, API keys, database passwords na server credentials. Blog hii inaonyesha hatua muhimu, commands, na code examples za kuongeza usalama wa website yako.
πŸ“ž WhatsApp: https://wa.me/255693118509

🌐 Website: https://www.faulink.com

Utangulizi

Configuration files ni sehemu muhimu sana kwenye website au system yoyote. Vinahifadhi database credentials, API keys, encryption keys, SMTP passwords, na server information.
Ikiwa hacker akipata config file anamiliki system yote.

Katika blog hii utajifunza jinsi ya kulinda mafaili haya ili website yako iwe salama kila wakati.

1️⃣ Ficha Configuration Files Nje ya Public Directory

Usiweke config files ndani ya:

public_html/

www/

htdocs/

Badala yake paka structure kama hii:

/home/username/config/secure.php
/home/username/public_html/index.php

Mfano wa kuitumia
require_once("/home/username/config/secure.php");

2️⃣ Zuia Access kwa .htaccess (Apache Servers)

Ili mtu asifungue faili kama .env, config.php, database.ini, api_keys.json, ongeza:

<FilesMatch "\.(env|ini|json|yaml|config|php|sql|log|sh|pem)$">
Order allow,deny
Deny from all
</FilesMatch>

Block specific file
<Files "config.php">
deny from all
</Files>

3️⃣ Tumia .env Files (Modern & Secure)

Instead of storing secrets directly kwenye PHP file:

Mfano wa .env
DB_HOST=localhost
DB_USER=faulink
DB_PASS=SuperSecret*Pass
DB_NAME=school_db

Kwenye PHP
$db_pass = getenv("DB_PASS");


Usiweke .env kwenye GitHub!

4️⃣ Weka Strong Permissions (chmod)

Command hizi zinazuia watu wasio admin kusoma config file.

Commands
chmod 600 config.php
chmod 600 .env
chmod 700 /home/username/config/


Hii inamaanisha:

Owner: βœ”

Group: ❌

World: ❌

5️⃣ Tumia Encryption Kulinda Secrets

Kama unahifadhi password au API key, unaweza kui-encrypt.

PHP: Encrypt & Decrypt
$key = "Faulink-Strong-Key-2024";

$encrypted = openssl_encrypt("mypassword123", "AES-128-ECB", $key);
$decrypted = openssl_decrypt($encrypted, "AES-128-ECB", $key);

echo $encrypted;


Hata hacker akiona file, hawezi kusoma password.

6️⃣ Secure Folder Structure (Recommended)

Muundo safi, salama, wa professional:

project/
β”‚
β”œβ”€β”€ config/ ← kuweka config.php, .env (protected)
β”‚ └── secure.php
β”‚
β”œβ”€β”€ storage/ ← logs, backups (protected)
β”‚ └── logs/
β”‚
└── public_html/ ← accessible via browser
β”œβ”€β”€ index.php
β”œβ”€β”€ assets/
β”œβ”€β”€ uploads/
└── .htaccess

7️⃣ Nginx Server Security (Alternative)

Kwa wale wanaotumia Nginx:

location ~* \.(env|ini|json|config|sql|log|sh|pem)$ {
deny all;
}

8️⃣ Usalama wa Backups na Logs

Hakikisha backups sio public:

/home/username/backups/


Encrypt backups:

gpg -c backup.sql

9️⃣ Quick Checklist (Copy & Use)
Kitu cha Kuangalia Status
Config files nje ya public_html βœ”
.htaccess imeblock access βœ”
.env inatumika βœ”
chmod 600 kwa config files βœ”
Secrets haziko GitHub βœ”
Folder structure ni secure βœ”
Backups zime-encryptiwa βœ”
Hitimisho

Config files ndizo moyo wa system β€” zikipasuka, kila kitu kinaanguka.
Kwa kutumia mbinu zilizo hapa, website yako inaanza kuwa 99% safer dhidi ya attackers.

Kwa usalama zaidi, installation, au scanning ya website yako:

πŸ“ž WhatsApp: https://wa.me/255693118509

🌐 Website: https://www.faulink.com