19

The problem is that I have only one domain name on which three different products need to be run (two of them PHP based and one python). So I need to treat the path in the URL as a different virtual host; i.e.:

www.domain.com/first_URL/
www.domain.com/second_URL/
www.domain.com/third_URL/

Where the first to third will act as separate virtual hosts.

How can I do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ruslan
  • 1,208
  • 3
  • 17
  • 28

4 Answers4

25

This can be achieved by using the Alias or AliasMatch directive:

Alias /first_url/ /var/www/first_url_resources

More details can be found in Apache Module mod_alias.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
x3c7
  • 359
  • 3
  • 3
  • 2
    Old answer and even older question, I know, but this was exactly the type of single-line solution I was after. It's not easy to search for something when you're not sure of the syntax or context of what you're looking for. Thanks! – wayfarer_boy Mar 28 '14 at 10:56
  • 1
    One more thing to note: the `Alias /url /path/to/dir` directive can be followed by a `` directive to allow overrides for that specific directory. – wayfarer_boy Mar 28 '14 at 13:52
  • 1
    This does not anser the question, because the `Alias` directive does not create a virtual host. So all directives, which can only be used in a virtual host (`SuexecUserGroup` for example), can not be used with this `Alias` workaround. – ceving Feb 21 '20 at 10:07
4

This example explains how to assign a different PHP version per directory. It can also be adapted to add Python support by running the Python interpreter as fast_cgi on a particular port.

For the purpose of the example, I assume there is a separate directory for each PHP version and they are named according to the PHP version that runs them, but this can be adjusted.

mkdir /home/user/www
mkdir /home/user/www/5.6.5
mkdir /home/user/www/7.0.2
mkdir /home/user/www/7.0.4
mkdir /home/user/www/7.0.6

Create symbolic links to directories that should be handled by different PHP versions:

sudo ln -s /home/user/www/7.0.2/ /var/www/html/7.0.2
sudo ln -s /home/user/www/7.0.4/ /var/www/html/7.0.4
sudo ln -s /home/user/www/7.0.6/ /var/www/html/7.0.6

Then add the following lines to /etc/apache2/sites-enabled/000-default.conf in default virtual host *:80

(For your needs, you can set up one more FastCGI handler here for the website that requires Python). I assume PHP 5.6.5 runs on port 9999, 7.0.2 runs on port 9998, etc.

DirectoryIndex index.html index.php
ProxyPassMatch ^/5.6.5/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9999/var/www/html/
ProxyPassMatch ^/7.0.2/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9998/var/www/html/
ProxyPassMatch ^/7.0.4/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9997/var/www/html/
ProxyPassMatch ^/7.0.6/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9996/var/www/html/

Assuming your server is pointed by example.com, you can test it on:

http://example.com/5.6.5/
http://example.com/7.0.2/
http://example.com/7.0.4/
http://example.com/7.0.6/
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marek
  • 1,413
  • 2
  • 20
  • 36
4

A "virtual host" in Apache works on domain names only, not on parts of the path. You cannot achieve what you want.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cweiske
  • 30,033
  • 14
  • 133
  • 194
  • 1
    Most people consider a virtual host setup similar to a reverse proxy in terms of end-user functionality. The question could be interpreted that way. Apache can do reverse proxy. – jiggunjer Apr 07 '20 at 18:33
1

You probably want to do something with the apache-config directives, since you're asking for a virtualhost solution. Apache can only work with virtualHosts as actual domains, as cweiske explained.

The solution in this case would be to either use a .htaccess file in the sub-directories you're working in, or to set up a <Directory "/web/root/subdir">..</Directory> block within your current (virtual-)host configuration.

You could also choose to host them on different sub-domains if you per se want to run them as VirtualHosts ('app1.domain.org').

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PtPazuzu
  • 2,497
  • 1
  • 17
  • 10
  • 1
    Well the problem is that I have only 1 domain with no possibility to add any more subdomains. And I need to run 3 different apps on it. The directory directive is not kinda an option since I can not place WSGI init scrpits in it. – Ruslan Aug 01 '11 at 10:49
  • @Ruslan: Without any experience from WSGI, a quickscan of the docs indicate that you can `WSGIScriptAlias /app1 /usr/local/wsgi/scripts/app1.wsgi` followed by `WSGIScriptAlias /app2 /usr/local/wsgi/scripts/app2.wsgi`, etc.. Not sure if that's enough? – PtPazuzu Aug 01 '11 at 10:55
  • In the regular virtual host config? (Not the directory-block one as suggested above). The scriptAlias can be called multiple times for different paths. – PtPazuzu Aug 01 '11 at 12:28
  • sorry that was my bad - made a typo – Ruslan Aug 02 '11 at 12:55