Deploying Yii2 Applications

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:

  • Use Git for version control
  • Deploy the app to a separate directory (projects/)
  • Create a symbolic link to public_html for better structure

Use promo code DYNAMICDUNIYA for 20% off on Hostinger hosting plans.


1. Setting Up Git for Deployment

First, upload your Yii2 project to GitHub, GitLab, or Bitbucket.

1.1 Add Your Project to GitHub

1. Initialize Git in your Yii2 project:

git init

2. Add a remote repository (GitHub, GitLab, or Bitbucket):

git remote add origin https://github.com/yourusername/your-repo.git

3. Push the Yii2 project:

git add .
git commit -m "Initial commit"
git push -u origin main

This ensures your project is version-controlled and ready for deployment.


2. Connect to Hostinger via SSH

2.1 Enable SSH Access on Hostinger

  1. Log in to Hostinger’s Control Panel
  2. Go to Advanced → SSH Access
  3. Copy the SSH credentials (Host, Username, Port)

2.2 Connect to Hostinger via SSH

On your local terminal, run:

ssh u820827383333@your-hostinger-ip -p 65002

Replace your-hostinger-ip and 65002 with your Hostinger SSH details.


3. Deploy Yii2 to a Secure Directory

3.1 Create a Secure Directory for Yii2

Once logged into SSH, create a projects/ folder outside public_html:

mkdir -p ~/projects/dynamicduniya

3.2 Clone Your Yii2 Project from GitHub

Navigate to the projects/ directory and clone your repository:

cd ~/projects
git clone https://github.com/yourusername/your-repo.git dynamicduniya

Your Yii2 project is now stored securely outside public_html.


4. Set Up Symbolic Link to public_html

Since Hostinger serves content from public_html, we create a symbolic link to point to our projects/dynamicduniya/web folder.

4.1 Remove the Default public_html Directory

rm -rf ~/domains/dynamicduniya/public_html

Deletes existing public_html (make sure it's empty before running this).

4.2 Create a Symbolic Link to Yii2's web/ Folder

ln -s ~/projects/dynamicduniya/web ~/domains/dynamicduniya/public_html

Now, public_html points to Yii2's web/ directory securely.


5. Configure Yii2 for Production

5.1 Set Up Database Connection

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',
];

5.2 Set Proper File Permissions

Ensure the runtime/ and web/assets/ folders are writable:

chmod -R 777 ~/projects/dynamicduniya/runtime ~/projects/dynamicduniya/web/assets

6. Automating Future Updates with Git

Each time you make updates, pull the latest changes from Git:

cd ~/projects/dynamicduniya
git pull origin main

This automatically updates your live app without re-uploading files manually.


7. Optimizing Yii2 for Performance on Hostinger

7.1 Enable Yii2 Caching

Edit config/web.php to use file-based caching:

'components' => [
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
],

This reduces database load and speeds up page rendering.

7.2 Enable Gzip Compression

Edit .htaccess in public_html to enable Gzip compression:

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>

7.3 Enable OPcache in PHP

  1. Go to Hostinger PanelPHP Configuration
  2. Enable OPcache to speed up PHP execution

8. Secure Your Yii2 Application

8.1 Secure the runtime/ and vendor/ Folders

Add these rules to .htaccess in public_html:

<FilesMatch "(runtime|vendor)">
    Order Deny,Allow
    Deny from all
</FilesMatch>

This blocks direct access to sensitive directories.

8.2 Force HTTPS (SSL)

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.


9. Set Up Cron Jobs for Yii2 Tasks

If your Yii2 app requires scheduled tasks, configure cron jobs in Hostinger.

9.1 Add a Yii2 Console Command in CRON

  1. Go to Hostinger PanelCron Jobs
  2. Add a new cron job:
php ~/projects/dynamicduniya/yii queue/run --verbose=1 --isolate=1

This runs Yii2 queue tasks automatically.

9.2 Example: Running a Yii2 Task Every Hour

0 * * * * php ~/projects/dynamicduniya/yii my-command

Modify my-command as per your Yii2 CLI command.


Final Steps & Testing

  • Flush Yii2 cache before testing:
php ~/projects/dynamicduniya/yii cache/flush-all
  • Run database migrations (if needed):
php ~/projects/dynamicduniya/yii migrate

Conclusion

Deploying a Yii2 application on Hostinger using Git & SSH is the best approach for security, maintainability, and scalability. This method ensures:

  • No direct file uploads to public_html
  • Easier updates with git pull
  • Improved security and performance

Use promo code DYNAMICDUNIYA for 20% off on Hostinger plans.