Docker Apache Virtualhost - docker

I'm trying to migrate my apps to docker, but with Apache I'm having some troubles. I'm adding my site.conf to apache (this works fine):
<VirtualHost *:80>
ServerName example.com.
ServerAlias api.example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
RewriteEngine on
RewriteOptions inherit
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile /etc/apache2/external/certificate.crt
SSLCertificateKeyFile /etc/apache2/external/private.key
SSLCACertificateFile /etc/apache2/external/ca_bundle.crt
SSLProxyEngine on
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off
ProxyPass / http://example:8090/
ProxyPassReverse / http://example:8090/
RewriteEngine on
RewriteOptions inherit
</VirtualHost>
But when I try to add a new virtualhost with a subdomain the container does not start:
<VirtualHost *:80>
ServerName sub1.example.com
ServerAlias sub1.example.com
ProxyPreserveHost On
ProxyPass / http://myapp2:8090/
ProxyPassReverse / http://myapp2:8090/
</VirtualHost>
<VirtualHost *:80>
ServerName sub2.example.com
ServerAlias sub2.example.com
ProxyPreserveHost On
ProxyPass / http://myapp2:8090/
ProxyPassReverse / http://myapp2:8090/
</VirtualHost>
I copy the log files from the container to my host machine, but the errors.logs file doesn't show any significant error as to why it won't boot. The docker logs container-name command doesn't help much either.
Any idea of the possible error?

I finally found the solution. I created a new configuration (new_site.conf) and added my virtualhost configuration. Then I enabled it (a2ensite new_site.conf), I restarted the service and everything was still working. I tried to access through the url and it redirected me well to my application.

Related

How to configure a Dockerized GitLab CE on Ubuntu which is proxied with HTTPS by Apache2?

I am trying to configure my docker GitLab CE (latest) instance that works with an integrated letsnecrypt that is part of the docker, in order to authenticate it as HTTPS under a sub-domain that is proxied by an Apache 2.4.
The reason i want it to go through Apache is that In this environment there is a web page running which claims port 80, 443 etc. So, in order for me to have both, i have mapped docker's ports to 443:444 and 80:3000.
And via apache's virtual host naming i have the following configuration:
<VirtualHost domain.com:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost gitlab.domain.com:80>
ServerAdmin webmaster#localhost.com
ProxyPreserveHost On
ProxyPass "/" "http://public-ip:3000/"
ProxyPassReverse "/" "http://public-ip:3000/"
ServerName gitlab.domain.com
</VirtualHost>
<VirtualHost gitlab.domain.com:443>
ServerAdmin webmaster#localhost.com
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
ProxyPreserveHost On
ProxyPass "/" "http://public-ip:3000/"
ProxyPassReverse "/" "http://public-ip:3000/"
ServerName gitlab.domain.com
</VirtualHost>
What do you think is the best approach?

Rails 3 + Passenger + Apache force WWW and SSL

I'm trying to redirect all traffic from non-SSL to SSL and force WWW and lastly to redirect an old subdomain to the master domain for a Rails 3 application.
Here is my goal:
http://domain.com -> https://www.domain.com
http://www.domain.com -> https://www.domain.com
http://api.domain.com -> https://api.domain.com
http://old.domain.com -> https://www.domain.com
I'm trying to achieve this from apache config file for the domain which looks like this:
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias api.domain.com
RewriteEngine On
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=permanent]
</VirtualHost>
<VirtualHost *:80>
ServerName domain.com
ServerAlias old.domain.com
RedirectMatch permanent ^/(.*) http://www.domain.com/$1
</VirtualHost>
<VirtualHost *:443>
ServerName www.domain.com
ServerAlias api.domain.com
DocumentRoot /home/deploy/domain/public
RailsEnv production
<Directory /home/deploy/domain/public>
Allow from all
Options -MultiViews
</Directory>
SSLEngine On
SSLOptions +StrictRequire
SSLCertificateFile /ssldomain_com.crt
SSLCertificateKeyFile /ssl/privateKey.key
SSLCertificateChainFile /ssl/domain_com.ca-bundle
</VirtualHost>
For some reason the SSL is fine but there is no redirect happening. The rails application works on all 4 options:
https://domain.com
https://www.domain.com
https://api.domain.com
https://old.domain.com
Any idea how I can clear my apache config file to make this work?
A bit more verbose, but all you should need is:
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias old.domain.com
ServerAlias domain.com
Redirect / https://www.domain.com/
</VirtualHost>
<VirtualHost *:80>
ServerName api.domain.com
Redirect / https://api.domain.com/
</VirtualHost>
Then what you have above for the SSL Virtual Host.

setting up ssl on rails 4 app on development mode

I am trying to setup SSL for rails 4 app. I have successfully setup the SSL which i bought from rapidSSL.
this is my configuration file used in apache2 and rails
<VirtualHost *:80>
ServerName example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
ServerName example.com
ServerAlias example.com
RequestHeader set X-FORWARDED-PROTO "https"
SSLEngine on
SSLProtocol all
SSLCertificateFile /etc/apache2/ssl/server.crt
SSLCertificateKeyFile /etc/apache2/ssl/server.key
SSLCertificateChainFile /etc/apache2/ssl/intermediate.crt
ServerPath /www
<Directory "/var/www">
</Directory>
</VirtualHost>
config/application.rb
config.force_ssl = true
However, when i run rails s -p 80, it just list the file structure of the folder instead of running the app. I have tried googling around and it seems that i need to use force WEBrick to use SSL but the guide i found here is only for rails 3. Hope someone could help me. Thanks in advance.
Take a look at this blog. Looks perry good and works for me
http://www.napcsweb.com/blog/2013/07/21/rails_ssl_simple_wa/

Apache + Passenger non-www to www => too many redirects

I am using phusion passenger to host a rails app at www.example.com. I want to tell apache to redirect example.com to www.example.com. The standard way seems to be using two virtual host configs. I am doing this, but I am getting a "too many redirects" error. Following is my config:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
...
</VirtualHost>
Thanks. I am using Passenger 3.0.11, Apache2.
We use a single virtual host config with a .htaccess file.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.mydomain.com
ServerAlias mydomain.com
DocumentRoot /var/www/public
<Directory /var/www/public>
AllowOverride all
Options Indexes FollowSymLinks MultiViews
</Directory>
</VirtualHost>
Then in your /var/www/public/.htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Rails, Apache2 on Ubuntu (karmic) deployment

I just need some clarification on a couple of files.
My site has an admin subdomain and SSL in addition to the normal *:80 details found in the virtual hosts.
My question(s): Do I need to specify a 1) ServerName and 2) DocumentRoot in: /etc/apache2/apache2.conf?
I currently enable my site from this directory: /etc/apache2/sites-available/site
Here are the contents of my site file in the above directory (/etc/apache2/sites-available/site):
<VirtualHost *:80>
ServerName www.site.com
ServerAlias www.site.com
DocumentRoot /home/user/public_html/site/current/public
RailsAllowModRewrite off
<directory "/home/user/public_html/site/current/public">
Order allow,deny
Allow from all
</directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.site.com
ServerAlias admin.site.com
DocumentRoot /home/user/public_html/site/current/public
RailsAllowModRewrite off
<directory "/home/user/public_html/site/current/public">
Order allow,deny
Allow from all
</directory>
</VirtualHost>
<VirtualHost *:443>
ServerName www.site.com
ServerAlias www.site.com
# SSL releated
SSLEngine on
SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
SSLCertificateFile /path/to/site.com.crt
SSLCertificateChainFile /path/to/bundle.crt
SSLCertificateKeyFile /path/to/site.key
# Used by rails
RequestHeader set X_FORWARDED_PROTO "https"
</VirtualHost>
Do you see anything wrong?
Looks like I was missing a DocumentRoot in my SSL VirtualHost. Fixed! (also cleaned up those aliases)

Resources