Why is my virtualhost defaulting to the standard document root? - dns

I have two domains registered: example.com and example.org. For one of these, I have also registered a subdomain: sub.example.com.
I successfully managed to set up a virtual host for my subdomain sub.example.com. So example.com points to the default document root /var/www and sub.example.com points to /var/www/sub.example.com.
But for some reason, I can't get example.org to point to anything else than the default document root. So example.com and example.org leads to the same website. I have created a virtual host for example.org, but Apache doesn't seem to register it.
# /etc/apache2/sites-available/example.org
<VirtualHost *:80>
ServerAdmin admin#example.org
ServerName example.org
DocumentRoot /var/www/example.org
</VirtualHost>

Have you enabled the website?
With a2ensite example.org you can enable your site. Then do service apache2 reload to reload the websites.

Related

Point domain and subdomain from Route53 to DigitialOcean using https

I have two wordpress sites running on two digitialocean droplets.
They both have ssl certificates and redirects all requests to https.
Let's call the first adresse https://my-freenom-domain-1.ml
Let's call the second adresse https://my-freenom-domain-2.ml
I have a domain registered on route53. Let's call the domain my-domain.com.
I'm trying to map (not redirect) all requests from https://my-domain.com to https://my-freenom-domain-1.ml
and all request from https://subdomain.my-domain.com to https://my-freenom-domain-2.ml
How would you do this?
Update:
What I've tried (That didn't work)
Creating a simple CNAME.
CNAME for main domain (my-domain.com):
Cannot create a CNAME for main domain and gives the following error:
RRSet of type CNAME with DNS name my-domain.com. is not permitted at apex in zone my-domain.com.
CNAME for subdomain (subdomain.my-domain.com):
I am able to create a CNAME for the subdomain, but requests are redirected.
So when I go to subdomain.my-domain.com I'm redirected to https://my-freenom-domain-2.ml
Create a S3 "redirect-bucket"
I've tried creating a S3 bucket that redirects all requests for the subdomain.
So bucket named subdomain.my-domain.com, redirects all redirects to https://my-freenom-domain-2.ml (https).
I then created a CNAME for subdomain.my-domain.com pointing to subdomain.my-domain.com.s3-website-eu-west-1.amazonaws.com.
But all requests are still redirected...
You need to create a virtual host for your new domain on your Digital Ocean droplets for it to work.
So I would do following to make it work -
Create virtual host for the new domain on the webserver of droplets,
or add the new domain as server in the webserver config.
Add the SSL certificates of the new domain to the old webserver or alternatively terminate the ssl at the ELB.
Add the DNS CNAME or A record entry for the new domain pointing to the old domain servers.
Post this it should work.
This is based on #mdeora's answer correct answer with some details.
1. Create a virtual host for the domain (my-domain.com) in the droplet
Copy default conf:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/my-domain.com.conf
Add a ServerName to the conf:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName my-domain.com
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enable the site: a2ensite my-domain.com.conf
Reload apache: systemctl reload apache2
2. Install a ssl certificate on the droplets server
(I did it using certbot)
certbot --apache -d my-domain.com
(follow the certbot instructions)
3. Create an A record in route53
Create an A record and point it to the ip of the droplet.
(4. update wordpress settings)
If you're running a wordpress site be sure to change wordpress url settings in admin to https://my-domain.com.
Now, hopefully, everything should work.

How to access to site by 2 address

I have a site now with address https://example.com/. How to make possible access to this site also by address https://example.com.html? What to write in htaccess?
What to write in htaccess?
Nothing special.
The question is not what to write but what to not write in .htaccess. And the answer is: do not write anything that links the site to one of its names (example.com or example.com.html). The same applies to the PHP source or configuration files.
The rest is just proper names registration, nameserver and Apache configuration.
Regarding the configuration of Apache, let's say the IP address of your web server is 1.2.3.4. The Apache configuration file (httpd.conf f.e.) now contains something similar to this:
<VirtualHost 1.2.3.4:80>
ServerName example.com
DocumentRoot "/www/docs/example.com"
# Other configuration directives for domain example.com
</VirtualHost>
In order to let Apache know the domain example.com.html is the same as example.com you can add a ServerAlias to the <VirtualHost> block:
<VirtualHost 1.2.3.4:80>
ServerName example.com
ServerAlias example.com.html
DocumentRoot "/www/docs/example.com"
# Other configuration directives for domain example.com
</VirtualHost>
Assuming you already have the name and the name servers are properly configured to point to your existing server, restart Apache and it should work.
Read more about <VirtualHost> and ServerAlias

Apache Multi-project configuration

I have a server with an ip: XXX.XXX.XXX.XXX
In my server, i have several projects in the folder /var/www:
/var/www/project1
/var/www/project2/trunk/webroot
...
My 000-default.conf is like this:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Currently I access into them through the following url:
XXX.XXX.XXX.XXX/project1
XXX.XXX.XXX.XXX/project2/trunk/webroot
...
I have a domain that targets to my server
domain.com -> XXX.XXX.XXX.XXX
My intention is to create a subdomain for each project, and each subdomain points to the target folder of the project:
project1.domain.com -> XXX.XXX.XXX.XXX/project1
project2.domain.com -> XXX.XXX.XXX.XXX/project2/trunk/webroot
(I think the best solution will be that XXX.XXX.XXX.XXX/project2 targets directly to XXX.XXX.XXX.XXX/proyect2/trunk/webroot)
I have read that it will be great that each project has its own .conf file in apache, and enabling each site separately.
But I think that if I create a VirtualHost *:80 for each project (project1.conf, project2.conf) they will be in conflict and I dont know how to do it.
Can anybody help me?
Regards.
Virtual Host is the way to go... create one virtual host for each domain pointing to a folder on your server where to webpage is sotored.
Listen 80
<VirtualHost *:80>
DocumentRoot "/www/example1"
ServerName www.example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/www/example2"
ServerName www.example.org
</VirtualHost>
Etc... One VirtualHost for each domain / DocumentRoot convination
Soruce: https://httpd.apache.org/docs/current/vhosts/examples.html
##################### EDIT
I have tested it on my own server
Whithout touching the default.conf file I have created two new config files
root#raspberrypi:/etc/apache2/sites-available# pwd
/etc/apache2/sites-available
root#raspberrypi:/etc/apache2/sites-available#
Please ignore the default files I have not touched them I have just created two new files named project1.... and project2....
root#raspberrypi:/etc/apache2/sites-available# ls
000-default.conf project1_domain_com.conf
default-ssl.conf project2_domain_com.conf
root#raspberrypi:/etc/apache2/sites-available#
root#raspberrypi:/etc/apache2/sites-available# cat project*
<VirtualHost *:80>
DocumentRoot "/var/www/projects/project1/"
ServerName project1.domain.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/projects/project2/"
ServerName project2.domain.com
</VirtualHost>
root#raspberrypi:/etc/apache2/sites-available#
ENABLING SITES
root#raspberrypi:/etc/apache2/sites-available# a2ensite project1_domain_com.conf
Enabling site project1_domain_com.
To activate the new configuration, you need to run:
service apache2 reload
root#raspberrypi:/etc/apache2/sites-available# a2ensite project2_domain_com.conf
Enabling site project2_domain_com.
To activate the new configuration, you need to run:
service apache2 reload
root#raspberrypi:/etc/apache2/sites-available#
RELOAD APACHE
root#raspberrypi:/etc/apache2/sites-available# /etc/init.d/apache2 reload
[ ok ] Reloading apache2 configuration (via systemctl): apache2.service.
root#raspberrypi:/etc/apache2/sites-available#
################# SUBDOMAIN EDIT
For example I have a single server with an IP XXX.XXX.XXX.XXX that have multiple domains registered (15 domains now).
I have one virtual host entry for each domain that works this way:
domain1.example.com -> Load the domain1 web index.html page (without /nothing/else )
domain2.example.com -> Load the domain2 web index.html page hosted on another folder expecified on the second virtual host.
And finally ussing a2ensite or a2dissite I enable or disable the website as a web hosting provider do...
If for any reason you need that accessing the host domain3.something.com it redirects to domain3.something.com/something/diferent ... it should be done on your server ussing .htaccess file and its a entirely diferent question :D

Redirect all subdomains to main domain by .htaccess

I'm trying to redirect all sub-domains to the root domain. For example, my domain is www.example.com. When someone tries to connect to notexists.example.com, I want to redirect it to www.example.com.
This is first going to be limited to dns.
If DNS is not first setup then the client's web browser won't have an ip address to visit if no DNS records exist for that subdomain, so you will have no way of contacting apache for it to implement a server side redirection.
What you need is a wilcard subdomain/record. This is in the form of an A record:
* 14400 IN A 1.1.1.1
You will access to the httpd.conf file (root access), if you are using cPanel without root access add * as a subdomain:
https://www.namecheap.com/support/knowledgebase/article.aspx/9191/29/how-do-i-create-a-wildcard-subdomain-in-cpanel
If you do have access you will need to set up a virtual host - add the following to your httpd.conf file:
#
# Your VirtualHosts section
#
NameVirtualHost 1.2.3.4
##
# this one accepts any subdomain
##
<VirtualHost 111.22.33.55>
DocumentRoot /www/subdomain
ServerName hostname.domain.com
ServerAlias *.domain.com
</VirtualHost>
http://httpd.apache.org/docs/2.2/vhosts/

Subdomain not working on Apache/Linux

I'm trying to set up a subdomain for on a client's web server. It's a CentOS server running Apache. So say the website is "example.com". I'm trying to create "bugs.example.com".
So far, I've added to the vhost.conf file the subdomain info:
<VirtualHost *:80>
ServerName bugs.example.com
ServerAlias bugs.example.com
DocumentRoot /var/www/www.bugs.example.com
<Directory "/var/www/www.bugs.example.com">
AllowOverride ALL
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I've added the server's IP address and the subdomain to /etc/hosts:
111.222.333.444 bugs.example.com
I've created the directory for the website files, and an index page at:
/var/www/www.bugs.example.com/index.html
Sadly, I still get a "Server not found" error when I go to bug.example.com. What am I missing here? The example.com domain name is registered through GoDaddy.com. Is there something that needs to be done there?

Resources