How can I serve multiple sites with apache based on the URL - apache

I have two separate Django projects that I want to serve up from the same machine. I'm trying to set up my VirtualHost configurations in apache to serve them up. I know I can serve them up on separate ports, but I'd rather do it based on the request URL. So example.com/app1 would go to one app and example.com/app2 would go to the other. ServerName and ServerAlias won't work since they only look at the Host, which in both cases would just be example.com. Is there any way to do this?
The ServerPath directive looked promising but it looks like, since it was only meant as a fallback for older browsers, apache only looks at ServerPath if the browser doesn't supply the Host field in the request.
Update: Here is what the VirtualHost configuration for my first app looks right now.
<VirtualHost *:80>
ServerAlias *
WSGIScriptAlias /app1 /home/eyuelt/app1/app1/wsgi.py
Alias /app1/static /home/eyuelt/app1/staticfiles
<Directory /home/eyuelt/app1/app1>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
<Directory /home/eyuelt/app1/staticfiles>
AllowOverride None
Order deny,allow
Require all granted
</Directory>
</VirtualHost>

Related

Why some directives in Apache vhost don't work with Ghost?

CONTEXT. I work on a server running Centos 8.3 with Apache 2.4.37.
In Apache's vhosts.conf configuration file I have this vhost for one of my websites:
<VirtualHost *:80>
ServerAdmin se#site.com
DocumentRoot /var/www/html/site
ServerName site.com
ErrorLog /var/log/httpd/admin-site.com-error_log
CustomLog /var/log/httpd/admin-site.com-access_log common
<Directory "/var/www/html/site">
Options SymLinksIfOwnerMatch
AllowOverride None
Order Allow,Deny
Allow from all
</Directory>
<Files ~ "test\.txt">
Order Allow,Deny
Deny from all
</Files>
<FilesMatch "\.(txt|png)$">
Order Allow,Deny
Deny from all
</FilesMatch>
</VirtualHost>
I have these two different scenarios:
If in the DocumentRoot folder I put only static files (html, css, images, etc), the Files and FilesMatch directives work perfectly and the frontend looks excellent.
If I put a Ghost installation in the DocumentRoot folder, I necessarily have to add the following Proxy directives to the vhost. And when doing so, the frontend looks great too, but the Files and FilesMatch directives NOT work at all, i.e. the browser allows me to open site.com/test.txt and site.com /file.png, when they shouldn't open.
ProxyRequests off
ProxyPass / http://localhost:2368/
ProxyPassReverse / http://localhost:2368/
Any ideas how to get the Files and FilesMatch directives to work on my Ghost installation?
I already tried this:
For those thinking about .htaccess. If I activate AllowOverride so that the .htaccess is read: I already tried and the directives in the .htaccess don't work either.
For those thinking of Deny from all. I know Deny from all is for Apache 2.2, and for 2.4 Require all denied should be used. I have done tests and in my Apache 2.4 the Require does not work, it only works Deny from all.

How to forward 80 port to 2 server based on donain

I have 2 apache server connected to a router. From the router settings, I can forward 80 and 443 port to a single machine/internal IP. Then I can access to it from my external IP address. My domain has A record pointed to that IP address. So, I am able to access to that machine using my domain.
But I want to use my both apache server for two different domain. Suppose, domain1.com will be connected to server1 and domain2.com will be connected to server2.
Is there any way to do this?
If you got a single server with two websites and two domains you can simply create two VirtualHost blocks in your vhost config file. Both might look similar to each other but you have to change the DocumentRoot and the ServerName entry.
The file will look similar to the following:
<VirtualHost *:80>
DocumentRoot "/var/www/Website1"
ServerName Website1.com
<Directory "/var/www/Website1/public">
Options Indexes FollowSymLinks MultiViews Includes ExecCGI
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/Website2"
ServerName Website2.com
<Directory "/var/www/Website2/public">
Options Indexes FollowSymLinks MultiViews Includes ExecCGI
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
For sure you can also handle an https connection within this file.
Additionaly there are good ways of handlign the connection redirects with an .htaccess file in the desired root folder.

Problems in setting up VirtualHost using WAMP server

I want to host multiple websites on my computer. I'm using Windows with WAMP server. I already have domains and know how to map them to ip.
I have already edited httpd.conf file to allow virtual hosts.
My httpd-vhosts file looks like this,
<VirtualHost *:80>
DocumentRoot "C:/wamp64/www"
ServerName localhost
ServerAlias localhost
<Directory "C:/wamp64/www">
AllowOverride All
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/wamp64/www/testcase"
ServerName test.mydomain.com
<Directory "C:/wamp64/www/testcase">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/wamp64/www/test2"
ServerName test2.mydomain.com
<Directory "C:/wamp64/www/test2">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
The problem is that both my domains open only the first site in this VirtualHost tag.
Example: In this case, both my domains will open the WAMP server configuration page. If I were to move the "testcase" tag above the other, both my domains will open the "testcase" page.
Update:
My subdomains show corresponding pages successfully when I open them on the server. But when I open subdomains on another machine, they open the first entry in the VH.
Update 2: Okay, so this is just out of my understanding now. I thought may be WAMP is not my cup of tea. So I installed XAMPP and made changes to the VH configuration and still ended up with same problem. So I then got rid of XAMPP too and installed WAMPDeveloper Pro. What could go wrong when the software sets up all the configuration files for you, right? But to my surprise, I still have the same problem. The websites work fine when I open them (using actual domain name) on the server itself, but when I open them on machine outside network the first VH entry open for all the domains I open.
Can anyone please help me with this?
Thanks!
Using both Apache2.2 and Apache2.4 syntax gets Apache a litle confused.
So as I assume you are running Apache 2.4 change the VH defs to this
<VirtualHost *:80>
DocumentRoot "C:/wamp64/www/testcase"
ServerName test.mydomain.com
<Directory "C:/wamp64/www/testcase">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/wamp64/www/test2"
ServerName test2.mydomain.com
<Directory "C:/wamp64/www/test2">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Why Apache opens the first site i.e. localhost in this case.
If Apache cannot make sense of a VH def, it ignores it and default to loading the first Virtual Host that was correctly defined in the httpd-vhosts.conf file.
Of course you will also need to amend your c:\windows\system32\driverrs\etc\hosts file to include these domain names
127.0.0.1 localhost
127.0.0.1 test.mydomain.com
127.0.0.1 test2.mydomain.com
::1 localhost
::1 test.mydomain.com
::1 test2.mydomain.com

Redirect to a subfolder in Apache virtual host file

I have Joomla installed on a webserver running Ubuntu Server 12.04. The Joomla folder is located at /var/www/cms/.
My vhost file at /etc/apache2/sites-enabled/default has the following content:
<VirtualHost *:80>
ServerName domain.com/
Redirect permanent / https://domain.com/
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
ServerName domain.com:443
DocumentRoot /var/www/cms
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/cms>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
(...)
</VirtualHost>
At the moment, all the requests to domain.com and anything entered after that like domain.com/example gets directed and processed by Joomla which either redirects to a proper page or returns a custom 404 error. This all works.
Now, I would like to filter all the requests that go to domain.com/subfolder before they get processed by Joomla and redirect them to /var/www/subfolder (instead of my root folder at /var/www/cms/).
I believe the file in /etc/apache2/sites-enabled/default (seen above) is the right place to define such a redirect, however I have not been able to figure out at what position and how to achieve this.
You should add to your configuration:
Alias /subfolder /var/www/subfolder
<Directory /var/www/subfolder>
Order allow,deny
allow from all
</Directory>
and fit the configuration between "Directory" to your needs.
See the Apache documentation to have more informations.

Local sub-domains on httpd apache

I have on my machine wampp server installed that I use to run php applications.
There are many folder in the htdocs with inside my projects and I can see those in the browser at this url: localhost/folder-name/.
I'd like to see every project in a custom url like: dev.name-folder.com
With IIS is very easy to do that, can someone explain how do that with Apache, using wampp server?
Thanks.
You can change your C:\Windows\System32\drivers\etc\hosts file to map domain names like dev.name-folder.com to your local system. (Otherwise you'll have to use a DNS server).
To configure a vhost in apache create a file for each domain/project you'd like to serve:
<VirtualHost *:80>
ServerAdmin email#domain.tld
ServerName domain.tld
DocumentRoot /var/www/htdocs/domain.tld/html
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/htdocs/domain.tld/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
ServerName is the domain name you'd like to serve your files under. DocumentRoot must be set to the absolute path to your files (here taken from a linux system).