如何配置Yii2 Turnkey前后端?新手访问遇404问题求助
Hey there! Let's tackle this Yii2 404 issue together—since you're working with a fresh TurnKey container, we can narrow down the fixes pretty quickly. Here's what to check step by step:
1. First, confirm if you're using the Yii2 Basic or Advanced template
This is a super common gotcha! Yii2 has two main project structures:
- Basic template: Single app, only has a
web/directory (nofrontend/orbackend/folders) - Advanced template: Split frontend/backend, with separate
frontend/web/andbackend/web/directories
Log into your Proxmox container via SSH or the console, and run:
ls -l /var/www/html
If you don't see frontend and backend folders, your TurnKey container is using the Basic template. That means you should be accessing the app at https://192.168.1.3/web/index.php instead of the frontend/backend paths you tried.
2. If you are using the Advanced template, check web server permissions & configuration
Assuming you do have frontend/ and backend/ folders, let's make sure your web server (Apache or Nginx) can access those directories properly.
For Apache (TurnKey often uses Apache by default):
- First, enable mod_rewrite if it's not already on:
a2enmod rewrite systemctl restart apache2 - Next, edit your Apache site config (usually at
/etc/apache2/sites-available/000-default.confor a SSL-specific config likedefault-ssl.conf). Add these blocks to allow access to the frontend/backend web directories:<Directory "/var/www/html/frontend/web"> AllowOverride All Require all granted </Directory> <Directory "/var/www/html/backend/web"> AllowOverride All Require all granted </Directory> - Make sure the
frontend/web/.htaccessandbackend/web/.htaccessfiles exist with the correct rewrite rules:RewriteEngine on # If a directory or file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Otherwise forward to index.php RewriteRule . index.php - Restart Apache again:
systemctl restart apache2
For Nginx (if your container uses Nginx instead):
- Edit your Nginx site config (usually at
/etc/nginx/sites-available/default). Update theserverblock to include rewrite rules for frontend/backend:server { listen 443 ssl; server_name 192.168.1.3; root /var/www/html; # SSL config (TurnKey likely has this set up, but double-check paths to certs) ssl_certificate /path/to/your/cert.pem; ssl_certificate_key /path/to/your/key.pem; # Frontend routing location /frontend/web { try_files $uri $uri/ /frontend/web/index.php?$args; } # Backend routing location /backend/web { try_files $uri $uri/ /backend/web/index.php?$args; } # PHP processing location ~ \.php$ { fastcgi_pass unix:/run/php/php7.4-fpm.sock; # Adjust PHP version to match your container fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } - Restart Nginx:
systemctl restart nginx
3. Verify Yii URL Manager settings (if you're using pretty URLs)
If you enabled enablePrettyUrl in your configs, make sure the settings are consistent across frontend and backend:
- For
frontend/config/main.phpandbackend/config/main.php, check theurlManagercomponent:'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [], ],
This should work alongside the .htaccess/Nginx rewrite rules we set up earlier.
4. Double-check file permissions
Make sure the web server user (usually www-data for Apache/Nginx) has read access to your Yii files:
chown -R www-data:www-data /var/www/html chmod -R 755 /var/www/html
Give these steps a try, and let me know if any of them resolve your 404 issue!
内容的提问来源于stack exchange,提问作者lordcroci

