Yii 2 Framework Deployment and Usage Guide
Prerequisites
Runtime Requirements
- PHP: Minimum version 7.4, recommended version 8.x
- Web Server: Apache, Nginx, or any web server with PHP support
- Database: MySQL, PostgreSQL, SQLite, or any database supported by PDO
Development Tools
- Composer: For dependency management
- Git: For version control
- Command Line Interface: For running commands and scripts
Optional Dependencies
- PHP Extensions: PDO, mbstring, intl, gd, curl, and others depending on your application needs
- Cache Backend: Redis, Memcached, or other caching systems for improved performance
Installation
1. Clone the Repository
git clone https://github.com/yiisoft/yii2.git
cd yii2
2. Install Dependencies
composer install
3. Create Application
Yii 2 provides different application templates. Choose one based on your needs:
Basic Application Template:
composer create-project yiisoft/yii2-app-basic basic
Advanced Application Template:
composer create-project yiisoft/yii2-app-advanced advanced
4. Initialize Application
cd basic
php init
Select 0 for development environment or 1 for production environment.
Configuration
Environment Variables
Create a .env file in your application root with the following configuration:
YII_DEBUG=true
YII_ENV=dev
DB_DSN=mysql:host=localhost;dbname=yii2basic
DB_USERNAME=root
DB_PASSWORD=
Database Configuration
Edit config/db.php to configure your database connection:
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2basic',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
];
Web Server Configuration
Apache: Enable mod_rewrite and create a .htaccess file in the web directory.
Nginx: Use the following configuration:
server {
listen 80;
server_name your-domain.com;
root /path/to/basic/web;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Build & Run
Development Mode
# Start development server
php yii serve
# Or use built-in PHP server
php -S localhost:8080 -t web/
Production Mode
# Set environment to production
php init --env=Production --overwrite=All
# Optimize Composer autoloader
composer dump-autoload --optimize
# Clear cache
php yii cache/flush-all
Running Migrations
# Apply all migrations
php yii migrate
# Apply specific migration
php yii migrate --migrationPath=@yii/rbac/migrations
Deployment
Platform Recommendations
Shared Hosting: Most shared hosting providers support PHP and MySQL. Upload files via FTP and configure database through hosting control panel.
VPS/Dedicated Server: Install PHP, web server, and database manually. Use process managers like PM2 for production.
Cloud Platforms:
- AWS: Deploy on EC2 with RDS for database
- DigitalOcean: Use LAMP/LEMP one-click apps
- Heroku: Use PHP buildpack
- Platform.sh: Native PHP support
Deployment Steps
-
Prepare Application
composer install --no-dev --optimize-autoloader php init --env=Production --overwrite=All -
Set Permissions
chmod 755 runtime web/assets chmod 644 web/index.php -
Configure Web Server Point web server document root to
web/directory. -
Database Setup
php yii migrate/up --interactive=0 -
Enable HTTPS Use Let's Encrypt or your cloud provider's SSL certificates.
Troubleshooting
Common Issues
1. "Class not found" Errors
- Ensure Composer autoloader is properly generated:
composer dump-autoload - Check namespace declarations in your classes
2. Database Connection Issues
- Verify database credentials in
config/db.php - Ensure database server is running
- Check if PDO extension is enabled
3. 404 Errors
- Verify web server configuration points to
web/directory - Check
.htaccessfile for Apache - Ensure URL rewriting is enabled
4. Permission Issues
# Fix runtime directory permissions
chmod 777 runtime
chmod 777 web/assets
5. Memory Limit Exceeded
# Increase PHP memory limit
php -d memory_limit=-1 yii command
Debug Mode
Enable debug mode in config/web.php:
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'debug' => [
'class' => 'yii\debug\Module',
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*'],
],
],
];
Logging
Check application logs in runtime/logs/ directory for detailed error information.
Performance Issues
- Enable caching: Redis or Memcached
- Optimize database queries
- Use Gzip compression
- Enable opcode caching (OPcache)
Security Considerations
- Change default admin credentials
- Set proper file permissions
- Use HTTPS in production
- Keep dependencies updated
- Implement proper input validation and output encoding