1

I'm a novice to Symfony2 and MVC frameworks but have some experience with PHP and Apache.

I developed a project on an Apache server on my workstation. I'm trying to deploy my first Symfony2 project on our production Ubuntu server, also running Apache. For some reason I cannot get the behavior I want. The server is running multiple virtual hosts just fine. However, when attempting to go to where the Symfony2 project should show up, I instead get a listing of the directory contents at the root level of the Symfony2 project (though the vhost is pointing to the /web subdirectory). If I manually navigate to the web directory I can get to the project clicking on either the app_dev.php or app.php front controllers, however app.php and app_dev.php persist when using the project instead of giving "pretty" URLs (with the fc hidden).

I've searched for solutions and tried using ./app/console --env=prod cache:clear to no avail. Apache mod_rewrite is enabled, and the Symfony2 default .htaccess file should be handling rewrites even if my vhost isn't (which would be preferred).

Thank you for any help!

Main vhost:

<VirtualHost *:80>
    ServerName server.com
    ServerAlias www.server.com
    ServerAdmin webmaster@server.com

    DocumentRoot /var/www/main/public

    <Directory /var/www/main/public>
        AllowOverride all
        Order allow,deny
        Allow from all
        Options MultiViews Indexes FollowSymlinks IncludesNoExec
    </Directory>

    <Location />
        # The main site was done in Zend Framework which uses this.
        SetEnv APPLICATION_ENV "production"
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} -s [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
        RewriteRule ^.*$ index.php [NC,L]
    </Location>

    ErrorLog /var/log/apache2/error-main.log
    LogLevel error

    CustomLog /var/log/apache2/access-main.log combined
    ServerSignature On

    Alias /project /var/www/project/web

</VirtualHost>

Project vhost:

<VirtualHost *:80>

    DocumentRoot /var/www/project/web
    Alias /project /var/www/project/web

    <Directory /var/www/project/web/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all

        DirectoryIndex app.php

    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error-project.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel info

    CustomLog ${APACHE_LOG_DIR}/access-project.log combined

</VirtualHost>

Using the default .htaccess file in project/web/

# Use the front controller as index file. It serves as fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# startpage (path "/") because otherwise Apache will apply the rewritting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex app.php

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/app.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the startpage because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # The following rewrites all other queries to the front controller. The
    # condition ensures that if you are using Apache aliases to do mass virtual
    # hosting, the base path will be prepended to allow proper resolution of the
    # app.php file; it will work in non-aliased environments as well, providing
    # a safe, one-size fits all solution.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteRule .? %{ENV:BASE}app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the startpage to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^/$ /app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>
Steve
  • 325
  • 5
  • 13

2 Answers2

2

Your ServerName directive is wrong. There should only stand a domain, no path afterwards. NEVER. If you want your sf project under the /project you have to use an alias in the VHost of the original server.com file.

<VirtualHost *:80>
    ServerName server.com
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/other-server.com-stuff
    <Directory /var/www/other-server.com-stuff>
        # stuff for other stuff
    </Directory>

    Alias /project /var/www/project/web
    <Directory /var/www/project/web/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel info

    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

if you want only the sf project, simply change the ServerName correctly.

Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
  • Thank you for your help! That's a very helpful tip. Unfortunately now that I've corrected that and reloaded Apache I'm still getting the same behavior as before. http://server.com/project/ gives me the directory listing for /var/www/project/, my front controller is still a level down in /var/www/project/web/, and manually navigating to it still continues to show the front controller when I have mod_rewrite enabled to hopefully give me "pretty" URLs. Any other suggestions? – Steve May 20 '13 at 18:55
  • Excellent! Re-reading this I see what I was doing wrong. On Ubuntu VirtualHosts are each kept in their own file--I needed to define the Alias in the "main" VirtualHost file for the server not the "project" VirtualHost file (which you said, but I didn't read properly the first time through). That is now working, however "project" subdirectories still try to revert to the "main" VirtualHost (not following the Alias). If you have suggestions on that they would be appreciated. Apache docs make me think it should work now. – Steve May 21 '13 at 20:32
  • Can you edit your two VHOst files into your question? Replacing the old one. – Emii Khaos May 21 '13 at 20:58
  • Added. The original site (which will hopefully be migrated eventually to SF2) was done on ZF1. I'm not sure if the ZF rewrite rules are relevant or not, but I included them just in case. Thank you for all your help! – Steve May 22 '13 at 22:14
0

In addition to Pazi's answer – which you should vote up – there are some things you should notice:

In order to have clean URL's, you need to specify the directory index of your virtual host:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
RedirectMatch permanent ^/app_dev\.php/(.*) /$1

Node that it is not a good practice to change the .htaccess of your project, but only the virtual host file of your local apache – or any web server – configuration.

For that to work, you need to change the virtual host to:

AllowOverride None

To remove the web/ from the URL, simply define it as the document root of your virtual host:

DocumentRoot /var/www/project/web
Community
  • 1
  • 1
Daniel Ribeiro
  • 10,156
  • 12
  • 47
  • 79
  • Not needed, let do the symfony supplied .htaccess do its work. – Emii Khaos May 20 '13 at 18:21
  • The `.htaccess` does that for the `app.dev`, considering a prod environment. It doesn't do for the `app_dev.php` file in dev environment. – Daniel Ribeiro May 20 '13 at 18:56
  • Should I be combining the two declarations? DocumentRoot /var/www/vaccineCAD/web Alias /vaccineCAD /var/www/vaccineCAD/web Or does one trample the other or otherwise make it unnecessary? And I fully intend to up-vote Pazi's useful advice as soon as I have enough reputation to do so. I've never posted here before and apparently I can't up-vote people yet. – Steve May 20 '13 at 19:05
  • The trample effect doesn't exist, I think. – Daniel Ribeiro May 20 '13 at 19:51