Configuring multiple websites on a single Apache instance - apache

I am looking for a way to host multiple websites on a single server. Currently I have used reverse proxy to host two of the websites using the following method:
I have a php site in /var/www/html folder and a nodejs app running on localhost:3015. My apache2 config is like following:-
<VirtualHost *:80>
ServerName site1.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://localhost:3015/
ProxyPassReverse / http://localhost:3015/
ServerName site2.example.com
</VirtualHost>
Now I further want to host few old websites at:
site1.example.com/archives/2014, 13 and so on
site2.example.com/archives/2014, 13 and so on.
In case of site1.example.com I can use alias as the site hosted is a php site.
In the second case where I am using reverse proxy what will be the best way to host a php site.
Also, please suggest a way where new sites can be easily added and the old sites can be moved to archived folder. These site might be on django, ROR and so on.
Is this even possible?

This may not be quite what you are asking for, but give this a try. It's a variation on the Apache config I use in a dev VM, which is set up for wildcard domain hosting. I haven't tested this config specifically, but you should be able to tweak it to suit your needs.
This essentially tells Apache how to find sites such as:
site1.example.com => /var/www/html/site1/public_html
site2.example.com => /var/www/html/site1/public_html
2014.archive.site1.example.com => /var/www/html/site1/public_html/archives/2014
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
DirectoryIndex index.html index.php
RewriteEngine on
RewriteMap lowercase int:tolower
# *.example.com
RewriteCond ${lowercase:%{SERVER_NAME}} ^[a-zA-Z0-9-]+\.example\.com$
RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C]
RewriteRule ^([a-zA-Z0-9-]+)\.example\.com/(.*) /var/www/html/$1/public_html/$2
# *.archive.*.example.com
RewriteCond ${lowercase:%{SERVER_NAME}} ^[a-zA-Z0-9-]+\.archive\.[a-zA-Z0-9-]+\.example\.com$
RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C]
RewriteRule ^([a-zA-Z0-9-]+)\.archive\.([a-zA-Z0-9-]+)\.example\.com/(.*) /var/www/html/$2/public_html/archives/$1/$3
</VirtualHost>
If this isn't what you were asking for, let me know.

I finally figured out a way to do the same. What you need to do is host all the sites at different port:
site1 => 3015
site2 => 4015
site3 => 3014
site4 => 4014
and so on.
Now you can configure your default.conf in apache2 as following:
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass /archive/2014/ http://localhost:3014/
ProxyPassReverse /archive/2014/ http://localhost:3014/
ProxyPass / http://localhost:3015/
ProxyPassReverse / http://localhost:3015/
ServerName site1.example.com
</VirtualHost>
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass /archive/2014/ http://localhost:4014/
ProxyPassReverse /archive/2014/ http://localhost:4014/
ProxyPass / http://localhost:4015/
ProxyPassReverse / http://localhost:4015/
ServerName site2.example.com
</VirtualHost>
This will work for sites on any platform, currently my site3 and site4 are php and site1 and site2 are node and django respectively. You might need to play with urls and links a little to make everything work perfectly.

Related

Apache Reverse Proxy For Specific Subdomain

I'm have a an Apache HTTP server that has a reverse proxy to a tomcat server. However, I only want the reverse proxy to happen when the client uses the subdomain www. This is because I want to use other subdomains to point to other applications, such as email.
e.g. www.example.com will go display the apache tomcat webapp.
The way to do this, I presume, is to configure my DNS so that every subdomain I use will point to my server. Right now, in addition to www, that is server.example.com and posfixadmin.example.com. However, the issue is that all my subdomains end up pointing to tomcat.
So when I try to visit postfixadmin.example.com/setup.php to set up postfixadmin through its web setup, it ends up taking me to my tomcat webapp's 404.
Here is my virtualhost configuration:
<VirtualHost www.example.com:80>
ServerName http://www.example.com
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
</Virtualhost>
<VirtualHost server.example.com:80>
ServerName server.example.com
DocumentRoot /var/www/html/
RewriteEngine on
RewriteCond %{SERVER_NAME} =server.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
</VirtualHost>
<VirtualHost postfixadmin.example.com:80>
ServerName postfixadmin.example.com
DocumentRoot /var/www/postfixadmin/public
ErrorLog /var/log/httpd/postfixadmin_error.log
CustomLog /var/log/httpd/postfixadmin_access.log combined
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/postfixadmin/public>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
EDIT
It looks like the proxy conf file doesn't do anything (??). I decided to experiment around and change the first virtualhost servername to the following:
<VirtualHost *:80>
ServerName abcd.example.com
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
</Virtualhost>
Then, I restarted and reloaded Apache...But for some reason, going to www.example.com STILL took me to the tomcat webapp! Does anyone know what drives this?
As to the DNS: I have set specific CNAME entries for each subdomain including www; all of them point back to the public IP of my server that houses my example.com domain (using # in my case - possible with most DNS, I think). There may be some different strategies on this, but I believe you're on the correct path based on what you've suggested in the question.
As to Apache configuration:
I believe that the http protocol does not need to be specified in the ServerName directive and that, generally, the domain need not appear inside the <VirtualHost>...</VirtualHost> tags.
I should mention that I am relatively unfamiliar with Tomcat but am assuming it is listening at 8080 on the localhost, in which case this should help.
I'm not 100% certain that that is all that is snarling you, but try trimming that ServerName back and doing like so, including the change to the VirtualHost open tag:
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
</Virtualhost>
Your second <VirtualHost> probably requires similar changes, though it also seems that you are directing it to serve requests from the web/network which are coming in on port 8080 -- which I don't believe is your intent.
I think what you want is to also listen on port 80 from the web/network, but to follow these directives if addressed to server.example.com like so:
<VirtualHost *:80>
ServerName server.example.com
DocumentRoot /var/www/html/
RewriteEngine on
RewriteCond %{SERVER_NAME} =server.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
</VirtualHost>
And finally, similar change to the opening <VirtualHost> tag on the final one:
<VirtualHost *:80>
ServerName postfixadmin.example.com
DocumentRoot /var/www/postfixadmin/public
ErrorLog /var/log/httpd/postfixadmin_error.log
CustomLog /var/log/httpd/postfixadmin_access.log combined
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/postfixadmin/public>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Altogether, this seems more like what you're looking for:
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
</Virtualhost>
<VirtualHost *:80>
ServerName server.example.com
DocumentRoot /var/www/html/
RewriteEngine on
RewriteCond %{SERVER_NAME} =server.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
</VirtualHost>
<VirtualHost *:80>
ServerName postfixadmin.example.com
DocumentRoot /var/www/postfixadmin/public
ErrorLog /var/log/httpd/postfixadmin_error.log
CustomLog /var/log/httpd/postfixadmin_access.log combined
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/postfixadmin/public>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
I got it!
It turns out that the problem was in the ssl configuration file - the :443 ports were overlapping.
Thanks for the help!

Problem with redirecting to main page with apache and nestjs

I'm looking for solution for my issue, because I've tried everything that comes to my head and I'm still not able to solve it.
So, I have backend written with nestjs and on my local machine everything works as expected.
But when I run it on server and type just host name ex. https://example.com it shows empty page.
It is confusing because every other route works well except main one.
So I'm assuming there's something wrong on my apache virtual host configuration but I'm quite new to it and I don't know where the problem is.
My apache2 conf looks like this:
<VirtualHost *:80>
ServerName example.com
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
SSLEngine on
SSLCertificateFile /etc/ssl/example.com.crt
SSLCertificateKeyFile /etc/ssl/example.com.key
SSLCertificateChainFile /etc/ssl/example.com.ca-bundle.crt
ServerAdmin admin#example.com
ServerName example.com
DocumentRoot /var/www/example.com/public_html/
ProxyRequests Off
ProxyPreserveHost On
ProxyVia full
<Proxy *>
Require all granted
</Proxy>
<Location "/">
ProxyPass http://127.0.0.1:50000/
ProxyPassReverse http://127.0.0.1:50000/
</Location>
<Directory /var/www/example.com/public_html/>
Allowoverride all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<IfModule mod_ssl.c>
My nestjs app starts on port 50000.
Thanks for any ideas.
Finally I figured it out.
Error was caused by useStaticAssets() function. When I removed it, everything starts working properly.
To be able to still serve static files I changed it to nest's ServeStaticModule.
Cheers.

Proxy pass an application from a different machine to an apache web server

I have three applications running, 2 on local machine/server where Apache is installed and 1 on a different machine/server.
I am using Apache2.4 on Ubuntu 18.04
application 1(yii) = 127.0.0.1:80
application 2(mantis bt) = 127.0.0.1:8080
application 3(yii) = 192.168.X.X:80
I want to configure my apache web server to host the three applications on https. I proxy pass both app2 and app3.
app2 = /app2 http://127.0.0.1:8080
app3 = /app3 http://192.168.X.X
app1 can be accessed successfully without error,
app2 and app3 can be accessed but .css, .js and other files cannot be found and error 404.
Note: I can access all three apps when not on proxy pass.
also it is possible to host the three apps and have a url like the following:
https://app1.example
https://app2.example
https://app2.example
I am new to apache. Please help me.
Configure apache virtual host as a below configuration
For app2
<Virtualhost *:80>
ServerName app2.domain.com
ServerAdmin youremail#email.com
ProxyPass http://127.0.0.1:8080/
ProxyPassReverse http://127.0.0.1:8080/
</Virtualhost *:80>
For app3
<Virtualhost *:80>
ServerName app3.domain.com
ServerAdmin youremail#email.com
ProxyPass http://192.168.X.X/
ProxyPassReverse http://192.168.X.X/
</Virtualhost *:80>
Create a separate virtual host for both domains.
After creating virtual host restart apache
service apache2 restart
It's working for me, I hope it will be worked for you and load .css, .js and other files.
If you want to redirect aap3 to https, then your final virtual host file will be like below configuration:
<Virtualhost *:80>
ServerName app2.domain.com
ServerAdmin youremail#email.com
ProxyPass http://127.0.0.1:8080/
ProxyPassReverse http://127.0.0.1:8080/
</Virtualhost *:80>
<Virtualhost *:80>
ServerName app3.domain.com
Redirect / https://app3.domain.com/
</Virtualhost *:80>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName app3.domain.com
ServerAdmin youremail#email.com
ProxyPass http://192.168.X.X/
ProxyPassReverse http://192.168.X.X/
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias app3.domain.com
SSLCertificateFile /etc/letsencrypt/live/app3.domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/app3.domain.com/privkey.pem
</VirtualHost>
</IfModule>
Please check my config below:
DocumentRoot /var/www/html/app1
ErrorLog /var/log/httpd/app1_log
LogLevel debug
<Directory /var/www/html/app1>
AllowOverride none
Order allow,deny
Allow from all
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
</Directory>
ProxyRequests Off
ProxyVia Off
ProxyPreserveHost On
<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
</Proxy>
<Location /app2>
ProxyPass http://127.0.0.1:8080/login_page.php
ProxyPassReverse http://1127.0.0.1:8080/login_page.php
SetEnv proxy-sendchunks 1
</Location>
<Location /app3>
ProxyPass http://192.168.X.X/
ProxyPassReverse http://192.168.X.X/
SetEnv proxy-sendchunks 1
</Location>
<Location /static/>
ProxyPass !
</Location>
</VirtualHost>
<VirtualHost *:443>
SSL Configuration
</VirtualHost>

Unable to get Apache docker container to serve virtual host with subdomain

I have a site I currently run that works well, but both to learn and to make it more portable, I've been trying to dockerize it. I'm using the offical apache and php images, and setup my virtual hosts identical to how I have on the running site, just with different domains. Unfortunately, while I can get one to work, the second does not.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName api.gamersplane.local
DocumentRoot /var/www/api
ErrorLog "/var/log/gamersplane.api"
CustomLog "/var/log/gamersplane.api" common
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://api:9000/var/www/$1
ProxyPassReverse ^/(.*\.php(/.*)?)$ fcgi://api:9000/var/www/$1
RewriteEngine On
RewriteBase /
RewriteRule !\.(css|jpg|js|gif|png|ico|eot|woff|ttff|svg|psd)$ dispatch.php
<Directory /var/www/api/>
Options FollowSymLinks
Require all granted
</Directory>
LogLevel notice
</VirtualHost>
<VirtualHost *:80>
ErrorLog "/var/log/gamersplane"
CustomLog "/var/log/gamersplane" common
ProxyPreserveHost On
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://api:9000/var/www/$1
ProxyPassReverse ^/(.*\.php(/.*)?)$ fcgi://api:9000/var/www/$1
ServerName gamersplane.local
ServerAlias *.gamersplane.local
DocumentRoot /var/www
RewriteEngine On
RewriteBase /
RewriteRule !\.(css|jpg|js|gif|png|ico|eot|woff|ttff|svg|psd)$ dispatch.php
<Directory /var/www/>
Options FollowSymLinks
Require all granted
</Directory>
LogLevel notice
</VirtualHost>
Originally, I had the first vhost (api.gamersplane) second, but thought maybe it was the server alias that was the problem. Then I switched positions, and even commented out the base (gamersplane.local), but had no luck. This works on the running site, so I can't figure out what's wrong with it.
Checking the headers returned by Postman, I do see it has the Apache and PHP headers, so it seems to be hitting SOMETHING, I just don't know what.

accessing site over https causes folder structure to be shown in browser, not application

I have a rails 3 app running with following stack:
ubuntu lucid lynx, apache2, passenger hosted on Amazon EC2
When accessed over http, the pages load and the site has no problems at all.
Then i installed a valid SSL cert on the webserver. When i try to access the same url over https, and it looks like passenger is not being activated. instead, in the browser, the folder structure of the rails application is rendered.
What configuration changes or modifications do I need to make to Apache and/or Passenger so that the https URL returns the same thing as the corresponding http url?
You need something like this:
<VirtualHost *:80>
ServerName orders.xxx.com
DocumentRoot "/var/www/html/ps-main/current/public"
CustomLog /var/log/apache2/orders common
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://orders.xxx.com%{REQUEST_URI}
NameVirtualHost *:443
RailsEnv production
<Directory /var/www/html/ps-main>
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
CustomLog /var/log/apache2/orders common
ServerAdmin webmaster#xxx.com.com
ServerName orders.xxx.com
SSLCertificateFile /etc/ssl/cert.crt
SSLCertificateKeyFile /etc/ssl/server.key
SSLCertificateChainFile /etc/ssl/intermediate.pem
# SSLProtocol all
SSLCipherSuite HIGH:MEDIUM
DocumentRoot /var/www/html/ps-main/current/public
<Directory /var/www/html/ps-main/current/public>
Options -MultiViews
AllowOverride all
</Directory>
</VirtualHost>
This will redirect all http traffic to https. If that's not what you need, remove the redirects.