Deploying your Yii2 application on Hostinger using Git and SSH is a more secure and maintainable approach. Instead of directly uploading files into public_html, we will:
projects/)public_html for better structureUse promo code DYNAMICDUNIYA for 20% off on Hostinger hosting plans.
First, upload your Yii2 project to GitHub, GitLab, or Bitbucket.
1. Initialize Git in your Yii2 project:
git init2. Add a remote repository (GitHub, GitLab, or Bitbucket):
git remote add origin https://github.com/yourusername/your-repo.git3. Push the Yii2 project:
git add .
git commit -m "Initial commit"
git push -u origin mainThis ensures your project is version-controlled and ready for deployment.
On your local terminal, run:
ssh u820827383333@your-hostinger-ip -p 65002Replace your-hostinger-ip and 65002 with your Hostinger SSH details.
Once logged into SSH, create a projects/ folder outside public_html:
mkdir -p ~/projects/dynamicduniyaNavigate to the projects/ directory and clone your repository:
cd ~/projects
git clone https://github.com/yourusername/your-repo.git dynamicduniyaYour Yii2 project is now stored securely outside public_html.
public_htmlSince Hostinger serves content from public_html, we create a symbolic link to point to our projects/dynamicduniya/web folder.
public_html Directoryrm -rf ~/domains/dynamicduniya/public_htmlDeletes existing public_html (make sure it's empty before running this).
web/ Folderln -s ~/projects/dynamicduniya/web ~/domains/dynamicduniya/public_htmlNow, public_html points to Yii2's web/ directory securely.
Edit config/db.php with Hostinger’s MySQL details:
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=your_database',
'username' => 'your_user',
'password' => 'your_password',
'charset' => 'utf8',
];Ensure the runtime/ and web/assets/ folders are writable:
chmod -R 777 ~/projects/dynamicduniya/runtime ~/projects/dynamicduniya/web/assetsEach time you make updates, pull the latest changes from Git:
cd ~/projects/dynamicduniya
git pull origin mainThis automatically updates your live app without re-uploading files manually.
Edit config/web.php to use file-based caching:
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
],This reduces database load and speeds up page rendering.
Edit .htaccess in public_html to enable Gzip compression:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>PHP Configurationruntime/ and vendor/ FoldersAdd these rules to .htaccess in public_html:
<FilesMatch "(runtime|vendor)">
Order Deny,Allow
Deny from all
</FilesMatch>This blocks direct access to sensitive directories.
Hostinger provides Free SSL. Add this to .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]This redirects all traffic to HTTPS automatically.
If your Yii2 app requires scheduled tasks, configure cron jobs in Hostinger.
Cron Jobsphp ~/projects/dynamicduniya/yii queue/run --verbose=1 --isolate=1This runs Yii2 queue tasks automatically.
0 * * * * php ~/projects/dynamicduniya/yii my-commandModify my-command as per your Yii2 CLI command.
php ~/projects/dynamicduniya/yii cache/flush-allphp ~/projects/dynamicduniya/yii migratehttps://yourdomain.comDeploying a Yii2 application on Hostinger using Git & SSH is the best approach for security, maintainability, and scalability. This method ensures:
public_htmlgit pullUse promo code DYNAMICDUNIYA for 20% off on Hostinger plans.
Sign in to join the discussion and post comments.
Sign in