Second VHost pointing to first on Apache Web Server - apache

I followed this tutorial to get Vhosts working on my website. https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-18-04-quickstart. For my second site at reporting.mydev.site, my website still redirects to my main website at mydev.site. I am using Apache as my HTTP server, here are my two .conf files for my main server and my new Vhost.
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName mydev.site
DocumentRoot /var/www/mydev.site/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www>
AllowOverride All
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydev.site
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
And here is the .conf file for the vhost I cannot get to work
<VirtualHost *:80>
ServerName reporting.mydev.site
ServerAdmin webmaster#localhost
DocumentRoot /var/www/reporting.mydev.site/public_html
ServerAlias www.reporting.mydev.site
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I found the problem, the issue was that I needed to create the https version of this file using certbot "reporting.mydev.site-le-ssl.conf". my second vhost was pointing to the first only for the https version but for the http only version it was correctly pointing to my new website.

Related

Apache Dynamic VirtualHost routing not working

I have a Codeigniter project that i want to access on main domain e.g domain.com and on multiple dynamic subdomains. The project files are at /var/www/ Directory. Each of the sub-directories are being accessed via "SubdirectoryName.domain.com".
I am using virtual hosts configuration and my main domain is working fine with all its routing but on subdomains, main page is working but routing and htaccess is not working. If i add/index.php in url it works but due to htaccess not working im not able to remove it from url.
I have tried this virtualhost configuration but it doesn't seem to work
This is my virtualhost conf file code
<VirtualHost *:80>
ServerName domain.co.uk
ServerAlias www.domain.co.uk
ServerAdmin webmaster#testing.com
DocumentRoot /var/www/domain.co.uk/public_html/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/domain.co.uk/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName domain.co.uk
ServerAlias *.domain.co.uk
ServerAdmin webmaster#testing.com
VirtualDocumentRoot /var/www/%1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

www.api.domain.com leads to one page and api.domain.com leads to another. www.domain.com works but domain.com doesn't. What gives?

I have the following Apache virtual hosts config file called 000-default.conf
<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot /var/www/mydomain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:80>
ServerName api.mydomain.com
ProxyPreserveHost On
ProxyPass / http://xxx.xx.xx.xx:3000/
ProxyPassReverse / http://xxx.xx.xx.xx:3000/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I am facing the 2 following issues and I'm not sure if the problem is on Apache's side or on the DNS's side:
When I visit www.mydomain.com , I see the page that I am supposed to
be seeing but if I try typing just domain.com in the search bar, I
get an error - This site can’t provide a secure connection
When I visit api.mydomain.com , I see the correct page which I'm
supposed to be seeing BUT when I try www.api.mydomain.com , I get
the page that is supposed to be on www.mydomain.com instead.
Can anyone shine some light on whether this is a result of bad Apache configuration or I need to do something on the DNS?
You would need to add two lines to your /etc/apache2/.conf
ServerAlias www.domain.com
Add this line under the ServerName

My subdomain keeps redirecting to my main domain

I am sure this must have been asked multiple times already, I researched it for like a day but still couldn't figure it out.
I have a domain - domain.com and now I want to add a subdomain - test.domain.com for it. I have created a virtual host file and DNS A record - test.domain.com pointing to my server IP. But then whenever I try to access my test.domain.com, it keeps redirecting to domain.com. My Apache2 virtual host files are configured as follow
domain.com.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.domain.com [OR]
RewriteCond %{SERVER_NAME} =domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<Directory "/var/www/domain.com">
AllowOverride All
</Directory>
domain.com-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ServerName domain.com
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias www.domain.com
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
</VirtualHost>
</IfModule>
test.domain.com.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/test.domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.test.domain.com [OR]
RewriteCond %{SERVER_NAME} =test.domain.com
RewriteRule ^ http://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<Directory "/var/www/test.domain.com">
AllowOverride All
</Directory>
I have enabled the conf files with the command a2ensite and restarted apache (and desperately my server droplet) multiple times already but still couldn't get it to work. I am running on Ubuntu 16.04.6.
Any advise would be much appreciated. Thank you very much in advance!
A minimalistic example would be along these lines:
<VirtualHost *:80>
DocumentRoot /var/www/test.example.com
ServerName test.example.com
ServerAlias www.test.example.com
</VirtualHost>
In this example you declare a virtualhost for test.example.com and also www.test.example.com.
Note that I am using example.com here instead of domain.com because of posting restrictions active on this website.
The RewriteRule directives in your subdomain don't seem to be right, at least syntactically. They may be causing the redirection problem.
So I would remove them and take care of redirects later. Start with a simple configuration, validate it and expand with more rules later.
Hint: you could use curl (from the command line on your server if not available on your PC) to test like this:curl -L --head "http://example.com".
Test the main domain name, then subdomain in both http and https. There is an option in Curl to ignore SSL/TLS errors if you encounter any.
See what happens and pay attention to the redirects if any (status code 301/302).
I prefer to use Curl for testing because the browser cache can play tricks on you.

Apache virtualhost root, www and sub-domain setup including http to https

I'm pretty stuck. Would appreciate some help.
Two websites (wordpress and another php tool) on an AWS EC2 Ubuntu
18.04 instance running LAMP stack.
My intention is to achieve:
http://www.example.com
http://example.com
https://www.example.com
to: https://example.com.
AND
http://app.example.com
to: https://app.example.com.
I had virtualhost configured and working as I intended, I then created the https cert through letsencrypt which setup the HTTP to HTTPS redirects...however...something's gone wrong:
HTTP all redirect to HTTPS.
https://app.example.com resolves to the intended website - website1.
https://example.com doesn't resolve to website2 - it resolves to website1 - note it's not redirecting to app. but showing website1 on https://example.com.
I've two virtualhost conf files, one for each site. Both contain each sites 80 and 443 config, they're below.
The SSL cert has the common name of example.com and lists alternative names for app.example.com, www.example.com and example.com.
DNS has example.com A to server IP, www. and app. are CNAME to example.com.
app.example.com.conf - Website 1
<VirtualHost *:80>
ServerAdmin jimmy#example.com
DocumentRoot /var/www/website1/
ServerName example.com
ServerAlias app.example.com
<Directory /var/www/website1/>
Options FollowSymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =app.example.com [OR]
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin jimmy#example.com
DocumentRoot /var/www/website1
ServerName example.com
ServerAlias app.example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website1/>
Options FollowSymlinks
AllowOverride All
Require all granted
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
example.com.conf - Website 2
<VirtualHost *:80>
ServerAdmin jim#example.com
ServerName example.com
ServerAlias example.com
DocumentRoot /var/www/website2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website2/>
AllowOverride All
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:80>
ServerAdmin jim#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/website2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website2/>
AllowOverride All
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin jim#example.com
ServerName example.com
ServerAlias example.com
DocumentRoot /var/www/website2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website2/>
AllowOverride All
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin jim#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/website2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website2/>
AllowOverride All
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
I'd really appreciate any help, suggestions and any other ideas!
https://example.com doesn't resolve to website2 - it resolves to website1 - note it's not redirecting to app. but showing website1 on https://example.com
Yes, because all your vHosts define example.com as the ServerName and I expect website1 appears first in the config.
For app.example.com.conf (Website 1) you should set ServerName app.example.com and remove the ServerAlias directive altogether for both the vhost:80 and vhost:443 containers.
The HTTP to HTTPS redirect in the vhost:80 container:
RewriteEngine on
RewriteCond %{SERVER_NAME} =app.example.com [OR]
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Can be simplified to a single mod_alias Redirect directive:
Redirect 301 / https://app.example.com/
For example.com.conf (Website 2) you have duplicated the vhost:80 and vhost:443 containers and defined the same ServerName example.com in each - this is an error. It is perfectly possible to have 4 containers like this (and use mod_alias Redirect directives), but you would need unique ServerName directives for each vHost and cut down the repetition.
The easiest approach from your point of view is probably just to remove the second (duplicate) vhost:80 and vhost:443 containers and set the following in both the vhost:80 and vhost:443 containers that remain.
ServerName example.com
ServerAlias www.example.com
I then created the https cert through letsencrypt which setup the HTTP to HTTPS redirects
Creating the SSL cert using LetsEncrypt should not have "setup the HTTP to HTTPS redirects"?

Malformed Request Line With SSL on Ubuntu

I'm in the process of setting up a droplet at DigitalOcean. I would like to serve multiple sites from the same droplet and use LetsEncrypt SSL certs. When accessing a test site, I see an error in the logs "AH00566: request failed: malformed request line" and in the Chrome I see "ERR_SSL_PROTOCOL_ERROR".
It seems that I likely have a configuration error somewhere but am not sure where.
The conf for my site is:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /var/www/mydomain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.mydomain.com [OR]
RewriteCond %{SERVER_NAME} =mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
<Directory /var/www/mydomain.com>
AllowOverride All
</Directory>
ServerAdmin webmaster#localhost
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /var/www/mydomain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mydomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.com/privkey.pem
</VirtualHost>