I'm confused with Apache vhost - apache

I am building a web application with Zend Framework, and I need to point my app to the "public" folder of the application:
So basically when I call http://localhost/myapp
it should display http://localhost/myapp/public/
I created a virtual host file called myapp into /etc/apache2/sites-available/:
<VirtualHost *:80>
DocumentRoot /var/www/myapp/public/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/myapp/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
But it doesn't work. When I call http://localhost/myapp, it displays the directory structure of the app, and when I click on the "public" folder, then it displays what I want to be displayed by default...
I never configured vhosts before and that's as far as I got with the tutorials about it.

In your first listing, you had a different value for the Directory tag, leaving off 'public' altogether. There was also a trailing slash after 'public' in the DocRoot value, but removed on your second attempt. Not sure that made a difference, but I believe it's recommended that you don't include trailing slashes.
Also, just wondering...are you running this on a local machine? I had trouble with Skype wanting to use port 80 if I started running that before my apache server. Skype will use a different port if 80 is already used. If not Skype, there may be another app that's using port 80 and interfering. That could be why you had success on another port.

Ok I found a way somehow... I don't think it's necessary the right/best way but...
in httpd.conf (in apache2 folder):
Listen 10089
<VirtualHost *:10089>
DocumentRoot "/var/www/myapp/public"
<Directory "/var/www/myapp/public">
Order allow,deny
Allow from all
AllowOverride all
</Directory>
</VirtualHost>
My app is now accessible via localhost:10089
After enabling the rewrite mod in apache, I added the necessary .htaccess, one at the root of my app, redirecting everything to index.php (Zend framework support friendly url navigation and works that way):
RewriteEngine on
RewriteRule .* index.php
and a second .htaccess file inside my public folder to allow people to access .jpg,.ico,etc files and not being redirected to index for everything:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
Hope this will help some!

Related

Apache port being automatically forwarded

I'm trying to have two different directory associated to two different ports. (8001 and 8004), but whenever I try to access port 8004 in a browser, it redirects automatically to port 8001.
Here's some extract of the config files that seems relevant:
In httpd.conf:
Listen 80
Listen 8001
Listen 8004
Removed comment to line:
Include etc/apache24/extra/httpd-vhosts.conf
In file httpd-vhosts.conf:
<VirtualHost *:8001>
DocumentRoot "<path_to_site_1>"
<Directory "<path_to_site_1>">
Require all granted
</Directory>
<Directory "<path_to_site_1>">
AllowOverride All
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
</Directory>
</VirtualHost>
<VirtualHost *:8004>
DocumentRoot "path_to_site_2"
<Directory "path_to_site_2">
Require all granted
</Directory>
<Directory "path_to_site_2">
AllowOverride All
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
</Directory>
</VirtualHost>
Here's a list of things I've tried, that didn't change anything:
Having the VirtualHost declaration in httpd.conf
Changing the order of the Virtual host declaration
Changing port 8004 to another port
Clear the browser cache and try a different browser
If I comment Listen 8001, it breaks everything.
Port 80 seems to point to the default www directory of this FreeBSD installation and still works properly, for phpMyAdmin is installed there and is still accessible.
Maybe of note, it's a FreeBSD installation running on Oracle VM VirtualBox.
Thank you for any help or ideas to follow.
Unfortunately, in this case, the comportment wasn't due to Apache configuration but to the website installations. One was a WordPress installation and the other was a Drupal installation. I wasn't aware that by moving those website in the directory structure, they would no longer point at the right place. WordPress was doing the redirection.
The issue was solved with two different solution, one for each platform.
For WordPress:
In the file functions.php, located in the theme directory:
Add the following lines:
update_option( 'siteurl', '<new_url>' );
update_option( 'home', '<new_url>' );
In the browser, fefresh the website a few time
Remove both lines from the file
For Drupal:
(using drush) In the directory where the website is located:
drush cache-rebuild
The combination of both commands solved the issue with both installation.

Codeigniter application index.php removed randomly in url on amazon EC2 clound server

I am working on a web application. I am facing a very strange issue while navigating the app on cloud amazon EC2 instance 1 that it randomly choose some links and when I click on those links it is redirecting me to page not found or sometime home page or sometime on login page. Although session still persist.
Previously I have added the .htaccess file to removed the index.php from the urls but after noticing this issue I have revert the changes but my re_write_mode still enabled in apache httpd.conf file and .htaccess file reside on root of the application with commented code.
Apache configuration mentioned below.
DocumentRoot "/var/www/html"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Not sure why you would be running in that problem from what you posted...here is the .htaccess file I use at the root of all of my CI projects to remove index.php from the URL
RewriteEngine On
# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/site_folder/, use /site_folder/
RewriteBase /
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For reuests that are not actual files or directories,
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
Nothing looks extremely out of the ordinary regarding your apache config. I would say that it sounds more like a problem with your "routes." Do you have any custom routes setup in your app?
Also, did you change your $config['index_page'] and/or $config['base_url'] in your config file?

Configure Zend Framework to ignore other directories and work in a subdirectory?

I have a default Zend Framework application installed under /var/www/html/zend_example. The public directory is: /var/www/html/zend_example/public.
I also have other non-zend sites in /var/www/html. For example, /var/www/html/other_site.
How can I configure the ZF site to work under http://MYDOMAIN/zend_example while simultaneously allowing http://MYDOMAIN/other_site to work?
Here is my apache config file:
<VirtualHost *:80>
DocumentRoot /var/www/html/zend_example/public
SetEnv APPLICATION_ENV "development"
<Directory /var/www/html/zend_example/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Here is the .htaccess file in /var/www/html/zend_example/public:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I just want to get a development ZF site up and running, while also having other sites available (under the same base URL).
Right now, all requests redirect (or rewrite?) to the base URL (http://MYDOMAIN).
Thanks!
Edit #1
I edited the vhost for both zend_example and other_site so that each DocumentRoot is set to /var/www/html, rather than the subdirectories for each site. This made it possible to access both sites in their respective URLs - other_site: http://MYDOMAIN/other_site and zend_example: http://MYDOMAIN/zend_example/public. Now, I believe there is a problem with the .htaccess file that is in the /var/www/html/zend_example/public directory. I cannot access default "Zend" URLs such as http://MYDOMAIN/zend_example/public/index/index which should point to the IndexController. Instead I receive a 404 error.
Edit #2
If I disable all vhosts except for the zend_example one, and the zend_example vhost has DocumentRoot /var/www/html set, then I am able to run the Zend site under http://MYDOMAIN/zend_example/public, including specific default Zend routes, such as http://MYDOMAIN/zend_example/public/index/index. This breaks when I enable another vhost, although I am still able to access the main index page for Zend at http://MYDOMAIN/zend_example/public.
Edit #3
I ended up toying with this for hours. It seems that there is some unique configuration that needs to be done since my server is centos 6.0. I ended up setting up a Debian 6.0 server and was able to get my virtual hosts setup correctly, including the Zend site! I'd still like to come back and solve the issue on centos, though.
you're missing the ServerName option in your vhost. Also you may encounter some difficulties if you don't allow followSymlinks in your directory declaration, try something like:
//the ServerName is the name that would follow http:// (http://zend.example/module/controller/action)
<VirtualHost *:80>
DocumentRoot /var/www/html/zend_example/public
ServerName zend.example
SetEnv APPLICATION_ENV "development"
<Directory /var/www/html/zend_example/public>
DirectoryIndex index.php
Options Indexes FollowSymlinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I'm not a *nix user so I'm not sure if you have change anything that resembles a windows hosts file or not, but it might pay to investigate. (it looks like you will have to edit the hosts file, it should be at /etc/hosts add a line like 127.0.0.1 zend.example where zend.example = ServerName from your vhost)
P.S. you'll probably have to make a vhost (as the first one in the list if all of your vhost are in one file) for localhost if you need localhost available. I know I have to do this on windows.

Multiple Zend framework sites on one server

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.
Here's what I'd do...
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.
Alias the public directories in your <VirtualHost> section
Alias /app1 /home/sudol/apps/app1/public
Alias /app2 /home/sudol/apps/app2/public
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>
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/"
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/

Deploy Zend to a Subdirectory

I just started learning Zend. I managed to get the basic working (using zf create project) in my local web server. Let's just say my project is called square
The only .htaccess that I have: square/public/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
httpd.conf
DocumentRoot "/home/amree/web"
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
<Directory "/home/amree/web">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
NameVirtualHost square
<VirtualHost square>
DocumentRoot "/home/amree/web/square/public"
ServerName square
</VirtualHost>
hosts
127.0.0.1 square
I'm running my application on Linux.
From what I've gathered, I can open (loaded without any problems) the site using:
http://square/
http://square/default/index/index
http://192.168.1.10/square/public/
http://192.168.1.10/square/public/default/index/index
But I can't open it using:
http://square/square/public (An error occurred message from Zend)
http://192.168.1.10/square/ (got a directory listing)
I also have other web applications in the same web server. For example, the meh application can be opened using http://192.168.1.10/meh/ but cannot be opened using http://square/meh
My question is, how can I load my Zend application without getting problems to other applications in the same server? At the moment, I prefer accessing it using my local IP (192.168.1.10). It should be possible to open it from another computer in the same network.
So, in the end I should be able to load the Zend project using
http://192.168.1.10/square
http://192.168.1.10/square/public
http://192.168.1.10/square/public/default/index/index
And I can also open my other meh application using http://192.168.1.10/meh
Thanks in advance.
You can't indeed access your application using
http://square/square/public
Using the square domain will match your vhost and /square/public will be rewritten to Zend, whom will try to run Square_PublicController::indexAction()
http://192.168.1.10/square/ (got a directory listing)
you got a directoy listing (allowed by Options Indexes in <Directory "/home/amree/web">) because your .htaccess is located in http://192.168.1.10/square/public
You have to make a choice between:
http://192.168.1.10/square
http://192.168.1.10/square/public
Or maybe try an Alias in Apache
Alias /square /square/public