6

I'm having trouble setting up my httpd.conf or .htaccess files to recognize multiple zend framework sites on one server.

For development, I have just one server and I'm trying to setup the sites so I can access them like localhost/app1, localhost/app2, etc.

So right now, when I go to localhost/app1 it does successfully redirect to localhost/app1/public/index.php, however when I go to localhost/app1/index/index I get a 404.

Here is my vhosts file:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "/var/www"
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory "/var/www/app1/public">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
    ErrorLog logs/error.log
    CustomLog logs/access.log common
</VirtualHost>

and here is my .htaccess file from the /var/www/app1 directory:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ /app1/public/index.php [NC,R,L]

If I change the DocumentRoot line in the vhosts file to DocumentRoot "/var/www/app1/public" then app1 does work correctly, but I can only access that one... at http://localhost. Is this possible? What I want to happen is if /var/www is the document root, then if I go to localhost/app1, those requests need to redirect to localhost/app1/public/index.php and if I go to localhost/app2 those requests need to redirect to localhost/app2/public/index.php.

I hope I explained this clearly, any help is appreciated.

In the end
I liked Phil's solution best because I didn't want to have to change my local hosts file and use the ServerName directive. It would be fine in a production environment if you own a separate domain name for each app, but not for development.

In addition to that, I was having a 403 forbidden problem when using an alternate directory for serving up web content. As I stated, the perms seemed correct the problem was with SE_Linux, and the security context of the files not being set to httpd_sys_content_t. I'm posting that solution that i found here, as it deals specifically with the issue. Thanks.

Community
  • 1
  • 1
sudol
  • 207
  • 3
  • 10

2 Answers2

17

Here's what I'd do...

  1. Install your applications somewhere arbitrary but outside the document root, eg /home/sudol/apps/app1 and /home/sudol/apps/app2. Make sure your Apache user can traverse this path and read the files.

  2. Alias the public directories in your <VirtualHost> section

    Alias /app1 /home/sudol/apps/app1/public
    Alias /app2 /home/sudol/apps/app2/public
    
  3. Setup your access and rewrite rules in the appropriate <Directory> sections (one for each app), eg

    <Directory "/home/sudol/apps/app1/public">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    
        RewriteEngine On
        RewriteBase /app1
        RewriteCond %{REQUEST_FILENAME} -s [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
        RewriteRule ^.*$ index.php [NC,L]
    </Directory>
    
  4. Delete the .htaccess files as you will no longer need them

Addendum

Make sure you set unique session names in your application config. You can also set specific cookie paths but this is optional, eg

; app1/application/configs/application.ini
resources.session.name = "app1"
resources.session.cookie_path = "/app1/"

; app2/application/configs/application.ini
resources.session.name = "app2"
resources.session.cookie_path = "/app2/"
Phil
  • 157,677
  • 23
  • 242
  • 245
  • not the correct way . Use subdomain instead for future safe . For e.g magento (zend framework project) will not work without tweaks if used this way. – Mr Coder Sep 27 '11 at 02:33
  • 2
    @OpenSourceLover What do you mean "not the correct way"? There is no "correct way", just "ways". Magento (which this question does not reference at all) uses ZF components but is not a standard ZF application. I also fail to see how it would not work using the above method. – Phil Sep 27 '11 at 02:39
  • Thanks for answering Phil. What do I use as the document root this way? Should I be able to access the app at http://localhost/app1 ? – sudol Sep 27 '11 at 02:39
  • @sudol Anything; and "yes". Your original doc root of `/var/www` should still apply. – Phil Sep 27 '11 at 02:41
  • Alias /app1 /home/sudol/apps/app1/public is that supposed to be ln /app1 /home/sudol/apps/app1/public? I'm trying to get it working right now and I keep getting 404 – sudol Sep 27 '11 at 03:03
  • @sudol Sorry, not following you there. The `Alias` directives go in the `` section in your vhosts file. See http://httpd.apache.org/docs/current/mod/mod_alias.html#alias. Also, don't forget to restart / reload Apache after making config changes – Phil Sep 27 '11 at 03:09
  • 2
    @Phil, something missing to you application.ini in order url's generation tu work correctly : `resources.frontcontroller.baseUrl = '/app1/'` – Frederik Eychenié Sep 27 '11 at 09:51
  • @FrederikEychenié Nope, don't need it. ZF is able to figure out the base URL correctly, even when operating under an alias. See http://staging.philipbrown.id.au/app1/ – Phil Sep 27 '11 at 10:00
  • I seem to be getting Forbidden errors now. It's setup just like you described. Apache is running as apache:web. paul:web is the owner/group of all the files and folders, and they all have 755 perms. I've been messing with the perms for hours now, any ideas? – sudol Sep 27 '11 at 16:22
  • 1
    Ah-ha! SE_Linux... as always. http://stackoverflow.com/questions/6807317/fixing-403-forbidden-on-alias-directory-with-apache – sudol Sep 27 '11 at 17:59
1

Its better to use subdomain i.e app1.localhost then localhost/app1 . Since the way cookies are stored (including session cookie) can give you problem latter . They will mismatch or can even overlap .

Here is a good tutorial to setup the preferred way

http://www.dennisplucinik.com/blog/2007/08/16/setting-up-multiple-virtual-hosts-in-wamp/

Mr Coder
  • 8,169
  • 5
  • 45
  • 74
  • Cookie data may include a path to avoid conflicts. See http://www.cookiecentral.com/faq/#3.3 – Phil Sep 27 '11 at 02:16
  • 1
    ... also, setting a unique session name (ie, the cookie name) avoids conflicts entirely and is typically a good habit to get into – Phil Sep 27 '11 at 02:27
  • 3
    You really need to summarize the linked text in your answer. If that link breaks, so does most of this answer. – Tim Post Sep 27 '11 at 13:14