|
| 1 | +# Installation guide |
| 2 | + |
| 3 | +## System requirements |
| 4 | + |
| 5 | +- [`PHP`](https://www.php.net/downloads) 8.1 or higher. |
| 6 | +- [`Composer`](https://getcomposer.org/download/) for dependency management. |
| 7 | +- [`Yii2`](https://github.com/yiisoft/yii2) 2.0.53+ or 22.x. |
| 8 | +- Web server (Apache, Nginx, or built-in PHP server). |
| 9 | + |
| 10 | +### Optional requirements |
| 11 | + |
| 12 | +- **Node.js**: For advanced asset compilation (optional) |
| 13 | +- **Docker**: For containerized development |
| 14 | + |
| 15 | +## Installation methods |
| 16 | + |
| 17 | +### Method 1: Using Composer (Recommended) |
| 18 | + |
| 19 | +Create a new project using the Yii App Basic template. |
| 20 | + |
| 21 | +```bash |
| 22 | +composer create-project --prefer-dist --stability=dev yii2-extensions/app-basic myapp |
| 23 | +cd myapp |
| 24 | +``` |
| 25 | + |
| 26 | +### Method 2: Manual installation |
| 27 | + |
| 28 | +1. **Download the template**: |
| 29 | +```bash |
| 30 | +git clone https://github.com/yii2-extensions/app-basic.git myapp |
| 31 | +cd myapp |
| 32 | +``` |
| 33 | + |
| 34 | +2. **Install dependencies** |
| 35 | +```bash |
| 36 | +composer install |
| 37 | +``` |
| 38 | + |
| 39 | +### Method 3: Using Git |
| 40 | + |
| 41 | +Clone the repository and set up your project. |
| 42 | + |
| 43 | +```bash |
| 44 | +git clone https://github.com/yii2-extensions/app-basic.git myapp |
| 45 | +cd myapp |
| 46 | +rm -rf .git |
| 47 | +git init |
| 48 | +composer install |
| 49 | +``` |
| 50 | + |
| 51 | +## Post-installation setup |
| 52 | + |
| 53 | +### Directory permissions |
| 54 | + |
| 55 | +Ensure the following directories are writable by the web server. |
| 56 | + |
| 57 | +```bash |
| 58 | +chmod 755 public/assets |
| 59 | +chmod 755 runtime |
| 60 | +chmod 755 tests/_output |
| 61 | +``` |
| 62 | + |
| 63 | +### Environment configuration |
| 64 | + |
| 65 | +1. **Create environment file** (optional) |
| 66 | +```bash |
| 67 | +cp .env.example .env |
| 68 | +``` |
| 69 | + |
| 70 | +2. **Configure database connection** in `config/web/components.php` |
| 71 | +```php |
| 72 | +<?php |
| 73 | + |
| 74 | +return [ |
| 75 | + 'db' => [ |
| 76 | + 'class' => 'yii\db\Connection', |
| 77 | + 'dsn' => 'mysql:host=localhost;dbname=myapp', |
| 78 | + 'username' => 'root', |
| 79 | + 'password' => '', |
| 80 | + 'charset' => 'utf8', |
| 81 | + ], |
| 82 | +]; |
| 83 | +``` |
| 84 | + |
| 85 | +### Web server configuration |
| 86 | + |
| 87 | +#### Apache |
| 88 | + |
| 89 | +Create `.htaccess` in the project root. |
| 90 | + |
| 91 | +```apache |
| 92 | +RewriteEngine on |
| 93 | +RewriteCond %{REQUEST_FILENAME} !-f |
| 94 | +RewriteCond %{REQUEST_FILENAME} !-d |
| 95 | +RewriteRule ^(.*)$ public/$1 [L] |
| 96 | +``` |
| 97 | + |
| 98 | +Create `public/.htaccess`. |
| 99 | + |
| 100 | +```apache |
| 101 | +RewriteEngine on |
| 102 | +RewriteCond %{REQUEST_FILENAME} !-f |
| 103 | +RewriteCond %{REQUEST_FILENAME} !-d |
| 104 | +RewriteRule . index.php |
| 105 | +``` |
| 106 | + |
| 107 | +#### Nginx |
| 108 | + |
| 109 | +Add to your Nginx configuration. |
| 110 | + |
| 111 | +```nginx |
| 112 | +server { |
| 113 | + listen 80; |
| 114 | + server_name myapp.local; |
| 115 | + root /path/to/myapp/public; |
| 116 | + index index.php; |
| 117 | +
|
| 118 | + location / { |
| 119 | + try_files $uri $uri/ /index.php?$query_string; |
| 120 | + } |
| 121 | +
|
| 122 | + location ~ \.php$ { |
| 123 | + fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; |
| 124 | + fastcgi_index index.php; |
| 125 | + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |
| 126 | + include fastcgi_params; |
| 127 | + } |
| 128 | +
|
| 129 | + location ~ /\.(ht|svn|git) { |
| 130 | + deny all; |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +#### Built-in PHP server |
| 136 | + |
| 137 | +For development purposes. |
| 138 | + |
| 139 | +```bash |
| 140 | +# Method 1: Using PHP built-in server |
| 141 | +php -S localhost:8080 -t public |
| 142 | + |
| 143 | +# Method 2: Using Yii console command |
| 144 | +./yii serve |
| 145 | + |
| 146 | +# Method 3: Custom host and port |
| 147 | +./yii serve --host=0.0.0.0 --port=8080 |
| 148 | +``` |
| 149 | + |
| 150 | +## Development setup |
| 151 | + |
| 152 | +### Database setup |
| 153 | + |
| 154 | +1. **Create database** |
| 155 | +```sql |
| 156 | +CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; |
| 157 | +``` |
| 158 | + |
| 159 | +2. **Run migrations** (if available) |
| 160 | +```bash |
| 161 | +./yii migrate |
| 162 | +``` |
| 163 | + |
| 164 | +3. **Seed test data** (optional) |
| 165 | +```bash |
| 166 | +./yii fixture/load |
| 167 | +``` |
| 168 | + |
| 169 | +### Asset compilation |
| 170 | + |
| 171 | +For advanced asset management. |
| 172 | + |
| 173 | +```bash |
| 174 | +# Install Node.js dependencies |
| 175 | +npm install |
| 176 | + |
| 177 | +# Compile assets |
| 178 | +npm run build |
| 179 | + |
| 180 | +# Watch for changes during development |
| 181 | +npm run watch |
| 182 | +``` |
| 183 | + |
| 184 | +## Docker setup |
| 185 | + |
| 186 | +### Using Docker Compose |
| 187 | + |
| 188 | +Create `docker-compose.yml`. |
| 189 | + |
| 190 | +```yaml |
| 191 | +version: '3.8' |
| 192 | + |
| 193 | +services: |
| 194 | + web: |
| 195 | + image: yiisoftware/yii2-php:8.1-apache |
| 196 | + ports: |
| 197 | + - "8080:80" |
| 198 | + volumes: |
| 199 | + - .:/app |
| 200 | + depends_on: |
| 201 | + - db |
| 202 | + environment: |
| 203 | + - DB_HOST=db |
| 204 | + - DB_NAME=myapp |
| 205 | + - DB_USER=root |
| 206 | + - DB_PASSWORD=secret |
| 207 | + |
| 208 | + db: |
| 209 | + image: mysql:8.0 |
| 210 | + environment: |
| 211 | + - MYSQL_ROOT_PASSWORD=secret |
| 212 | + - MYSQL_DATABASE=myapp |
| 213 | + volumes: |
| 214 | + - mysql_data:/var/lib/mysql |
| 215 | + |
| 216 | +volumes: |
| 217 | + mysql_data: |
| 218 | +``` |
| 219 | +
|
| 220 | +Run the application. |
| 221 | +
|
| 222 | +```bash |
| 223 | +docker-compose up -d |
| 224 | +``` |
| 225 | + |
| 226 | +### Using Docker directly |
| 227 | + |
| 228 | +```bash |
| 229 | +# Build custom image |
| 230 | +docker build -t myapp . |
| 231 | + |
| 232 | +# Run container |
| 233 | +docker run -p 8080:80 myapp |
| 234 | +``` |
| 235 | + |
| 236 | +### Performance optimization |
| 237 | + |
| 238 | +#### Enable OPcache |
| 239 | + |
| 240 | +Add to your `php.ini`. |
| 241 | + |
| 242 | +```ini |
| 243 | +opcache.enable=1 |
| 244 | +opcache.enable_cli=1 |
| 245 | +opcache.memory_consumption=128 |
| 246 | +opcache.interned_strings_buffer=8 |
| 247 | +opcache.max_accelerated_files=4000 |
| 248 | +opcache.revalidate_freq=60 |
| 249 | +opcache.fast_shutdown=1 |
| 250 | +``` |
| 251 | + |
| 252 | +#### Configure caching |
| 253 | + |
| 254 | +Enable cache components in `config/web/components.php`. |
| 255 | + |
| 256 | +```php |
| 257 | +'cache' => [ |
| 258 | + 'class' => 'yii\caching\FileCache', |
| 259 | +], |
| 260 | +'urlManager' => [ |
| 261 | + 'enablePrettyUrl' => true, |
| 262 | + 'showScriptName' => false, |
| 263 | + 'enableStrictParsing' => false, |
| 264 | + 'rules' => [ |
| 265 | + // Your URL rules |
| 266 | + ], |
| 267 | +], |
| 268 | +``` |
| 269 | + |
| 270 | +## Next steps |
| 271 | + |
| 272 | +Once the installation is complete. |
| 273 | + |
| 274 | +- ⚙️ [Configuration Reference](configuration.md) |
| 275 | +- 💡 [Usage Examples](examples.md) |
| 276 | +- 🧪 [Testing Guide](testing.md) |
0 commit comments