Config virtual hosts on Apache 2.4 via webmin on ubuntu 16.04 - apache

I'm having some troubles trying to config my apache server to host multiple domains. In this moment i have only one, but i want to keep it prepared to host more. I don't know how to make the server listens ports 80 and 443 with SSL enabled. i have this config on my .conf file of the virtual host:
<IfModule mod_ssl.c>
<VirtualHost *:80>
DocumentRoot /var/www/mygamingmoments
ServerName mygamingmoments.es
<Directory "/var/www/html/mygamingmoments">
allow from all
Options Indexes FollowSymLinks
Require all granted
AllowOverride All
RewriteEngine On
RewriteOptions Inherit
</Directory>
SSLCertificateFile /etc/letsencrypt/live/mygamingmoments.es/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mygamingmoments.es/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
The fact is that now i can enter my site with https but if i didn't write this, the virtual host seems not working.
Greetings.

There is many approaches for make Apache set up, so I will show something that you probably will want to do.
If what you need to do is to redirect all traffic for port 80 for 443, to prevent access be http or https depending on what port you put after your domain you can do something like this or adapt to your real intent.
So in your Virtual Host for port 80 you need something like this:
<VirtualHost *:80>
ServerName your-domain.something
ServerAlias www.your-domain.somenthing
DocumentRoot /path/to/source
<Directory /path/to/source>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =your-domain.something [OR]
RewriteCond %{SERVER_NAME} =www.your-domain.somenthing
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Here each request to your site or app, whatever, with www or without www will redirect to another virtual host with https protocol, so you need to create it.
Virtual host for port 443.
<VirtualHost *:443>
ServerName your-domain.something
ServerAlias www.your-domain.somenthing
DocumentRoot /path/to/source
<Directory /path/to/source>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Include /path/to/options-ssl-apache.conf
SSLCertificateFile /path/to/fullchain.pem
SSLCertificateKeyFile /path/to/privkey.pem
</VirtualHost>
You can do this on the same file or in another file for better organization (my-site.conf for example), but if you choose to do in another you must add this 'site' to sites enabled of apache environment with a2ensite:
# a2ensite path-to-your-config-file
# systemctl reload apache2
With this sample, you should to know that if you want have various sites in you environment, you will need of each site has it own Virtual Host. In this approach one Virtual Host for port 80 and one correspondent to port 433. And the key for that is make the requests matches with ServerName ou ServerAlias option at each Virtual Host.
I Hope it helps.

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!

Serving multiple root directories in Apache

I would like my two sites: flowers.loc (Drupal 8) and honey.loc (Drupal 7) sites to work locally on Apache (v: 2.234).
First directory for flowers.loc:
Sites/drupal8/docroot
Second directory for honey.loc:
Sites/drupal7/docroot
I have this setting in httpd, apache configuration file:
<VirtualHost *:80>
DirectoryIndex index.html index.php
DocumentRoot /User/Vizzaro/Sites
<Directory "/User/Vizzaro/Sites">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Have a look at https://httpd.apache.org/docs/2.2/vhosts/examples.html to find out how to set up virtual hosts. Your configuration file has only one virtual host entry but you need one entry for each site. And furthermore your config is missing the ServerName. Try this: (untested)
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
ServerName flowers.loc
DocumentRoot /User/Vizzaro/Sites/drupal8/docroot
# Other directives here
</VirtualHost>
<VirtualHost *:80>
ServerName honey.loc
DocumentRoot /User/Vizzaro/Sites/drupal7/docroot
# Other directives here
</VirtualHost>
And be sure that honey.loc and flowers.loc both point to the IP of the machine on wich your apache is running. Try ping flowers.loc - if this results in an error like Name or service not known you probably have to edit your hosts file to fix it.

How can I internally point to a domain but keep the url with the subdomain constant?

I have a domain www.example.com hosted in one location.
I have created another account with a different hosting provider that allows me to create subdomains: www.test1.example.com and www.test2.example.com
I am putting a custom page for each of the subdomains when the user goes to test1.example.com, test2.example.com....
After the user logs in on this custom page, I want to maintain the subdomain (of test1.example.com) but internally have all requests point to www.example.com.
I am running the custom pages on Apache and the domain pages on Apache Tomcat - I think that using 'mod_rewrite' is the way to go?
Put the following in a .htaccess file in your subdomain's document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test1\.example\.com$
RewriteRule ^/(.*) http://example.com/$1 [redirect,last]
To enable mod_rewrite on a modern Ubuntu web server run this command:
sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
Make sure AllowOverride is set to "All" in your VirtualHost configuration (for example /etc/apache2/sites-available/default):
<Directory /var/www/document/root/>
AllowOverride All
</Directory>
then restart Apache:
sudo /etc/init.d/apache2 restart
So, basically the answer I found that worked was to use mod_proxy. I enabled that as an Apache module and included the following in my httpd-vhosts.conf file.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName test1.example.com
DocumentRoot "location_of_the_custom_page"
ErrorLog "logs\errors.log"
<directory "D:\wamp\www\capitalfloat">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</directory>
</VirtualHost>
<VirtualHost *:80>
ServerName localhost2
ServerAlias *.example.com
ErrorLog "logs\errors.log"
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://www.example.com
ProxyPassReverse / http://www.example.com
</VirtualHost>
I also had to include 'http://www.example.com' and 'test1.example.com' in the Windows Host File (For me, C:\Windows\System32\drivers\etc\hosts). In my custom login page, the request goes to 'example.com' and all subsequent requests are sent to 'www.example.com' but the url still shows 'test1.example.com/...'

redirect subdomains bar one

So I've set up a Amazon EC2 and registerd a few domains with 123-reg, i've set up my Apache VirtualHost But now I'm going to need to set up a few sub domains (like kitten.example.com). But it's only going to be a few, and I would like the rest of the subdomain wild cards to go to the base like this:
kitten.example.com -> kitten.example.com BUT
*.example.com -> example.com
currently my DNS with 123-reg is as follows:
www A 198.168.0.0
kitten A 198.168.0.0
* A 198.168.0.0
and my Apache httpd.conf is:
<VirtualHost *:80>
DocumentRoot "/var/www/example.com"
ServerName example.com
ServerAlias example.com
<Directory "/var/www/example.com">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/kitten.example.com"
ServerName kitten.example.com
ServerAlias kitten.example.com
<Directory "/var/www/kitten.example.com">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
so how do i redirect all those wildecard subdomains to my base domain?
You will have to map DocumentRoot and Directory to your base domain directory.
or if you wish to change the domain name also, then write a .htaccess rule for it.
Create a virtual host directive that doesn't specify the ServerName or ServerAlias. This will catch all virtual hosts on that IP address (you specifcy all IP's on port 80) that do not map to any other virtual host specification.
<VirtualHost *:80>
DocumentRoot /www/default
...
</VirtualHost>
http://httpd.apache.org/docs/2.2/vhosts/examples.html#default
However, if you want to send a 301 HTTP Header (permanently redirected), you'll need to use mod_rewrite or something similar:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
How to setup Apache mod_rewrite to redirect all but one subfolder is one good example .. needs to be modified to suit your needs if the first option isn't sufficient for your needs.

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.