Ubuntu环境下Symfony 3.4安装异常求助:无法显示默认欢迎页
Hey there! Let's walk through troubleshooting why your Symfony 3.4 install on Ubuntu (PHP 7) isn't showing the default welcome page. These are the most common fixes that usually resolve this issue:
First, make sure your web server (Apache or Nginx) is pointing to the correct directory and has proper permissions to read Symfony's files.
For Apache:
- Edit your virtual host config (usually in
/etc/apache2/sites-available/your-site.conf) and ensure:DocumentRootpoints to thewebdirectory of your Symfony project (not the root project folder)AllowOverride Allis set for the web directory to enable.htaccessrules
Example config:
<VirtualHost *:80> ServerName your-project.local DocumentRoot /var/www/your-symfony-project/web <Directory /var/www/your-symfony-project/web> AllowOverride All Require all granted </Directory> </VirtualHost> - Enable the site and rewrite module if needed:
sudo a2ensite your-site.conf sudo a2enmod rewrite sudo systemctl restart apache2
For Nginx:
If you're using Nginx, ensure your server block points to the web directory and includes the proper rewrite rules:
server { listen 80; server_name your-project.local; root /var/www/your-symfony-project/web; location / { try_files $uri $uri/ /app.php$is_args$args; } location ~ ^/app\.php(/|$) { fastcgi_pass unix:/run/php/php7.4-fpm.sock; # Adjust PHP version to match yours fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; fastcgi_param DOCUMENT_ROOT $realpath_root; internal; } location ~ \.php$ { return 404; } }
Restart Nginx after making changes: sudo systemctl restart nginx
Symfony has specific PHP extensions and settings required to run. Run the built-in checker to catch any missing dependencies:
php bin/symfony_requirements
Look for any red errors (not warnings) and fix them. Common issues include missing extensions like intl, mbstring, or incorrect php.ini settings (e.g., memory_limit too low).
Ubuntu's web server runs as the www-data user, which needs write access to Symfony's var directory (for cache, logs, etc.). Run these commands to set the correct permissions:
sudo chown -R www-data:www-data var sudo chmod -R 775 var
Note: If you're working on the project locally, you might also want to add your user to the www-data group to avoid permission conflicts when running console commands:
sudo usermod -aG www-data $USER
Log out and back in for this change to take effect.
Stale cache can sometimes prevent the welcome page from loading. Clear the cache for your environment:
# For development environment php bin/console cache:clear --env=dev # For production environment (if you're running prod mode) php bin/console cache:clear --env=prod --no-debug
If that doesn't work, you can manually delete the cache directory:
rm -rf var/cache/*
Check that the default welcome route exists in your routing config:
- Open
app/config/routing.ymland ensure you have this entry:_welcome: path: / defaults: { _controller: AppBundle:Default:index } - Or if you're using annotations, verify that
src/AppBundle/Controller/DefaultController.phphas the root route defined:/** * @Route("/", name="homepage") */ public function indexAction() { // The default code should return the welcome page template return $this->render('default/index.html.twig'); }
To rule out web server issues, try running Symfony's built-in development server:
php bin/console server:run
Then visit http://localhost:8000 in your browser. If the welcome page loads here, the problem is definitely with your Apache/Nginx configuration—go back to step 1 to fix that.
内容的提问来源于stack exchange,提问作者Ian

