Unsure of what to add to hosts file - Apache Subdomain - apache

I'm trying to make a subdomain for my xampp/apache hosted website.
However, whenever I attempt to access the subdomain it gives me an "HTTPS insecure error" and redirects me to the main part of the site. Also, I can still access the main site without any issues at all.
In my 'httpd-vhosts.conf' file, I believe all the entries are correct and the issue lies with my hosts file.
To clarify, this is a website with a premium domain name, (i.e not just a website for local use). Most of the tutorials and guides I can find are only really designed for websites that are used locally only.
Here is my 'httpd-vhosts.conf' file (with my domain name edited out of course):
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/"
ServerName domain.xyz
ServerAlias https://domain.xyz
ErrorLog "logs/domain.xyz-error.log"
CustomLog "logs/domain.xyz-access.log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/sub1"
ServerName sub1.domain.xyz
ServerAlias https://sub1.domain.xyz
ErrorLog "logs/sub1.domain.xyz-error.log"
CustomLog "logs/sub1.domain.xyz-access.log" common
</VirtualHost>
The entries I have in my hosts file are (which I'm fairly certain is completely wrong):
127.0.0.1 localhost
127.0.0.1 domain.xyz
127.0.0.1 sub1.domain.xyz

Related

How do I set the entered subfolder as root?

I have setup VirtualHost and it works good when I use the domain (ex. www.website_1.se). The problem is that the DocumentRoot that is set for the VirtualHost is not applied when I visit the website through localhost (ex. localhost/website_1.se).
My root folder looks like this:
website_1.se, website_2.se, website_3.se
And my conf:
<VirtualHost *:80>
ServerAdmin admin#website_1.se
ServerName website_1.se
ServerAlias www.website_1.se
DocumentRoot "C:/xampp/htdocs/website_1.se"
</VirtualHost>
Inside website_1.se, I have html-files with src attributes, some of them starts with '/', which refers to the root.
So if I have a src="/images/file.jpg" inside the index file of "website_1.se", and enter "localhost/website_1.se, Apache will try to load that image from localhost/images/file.jpg instead of localhost/website_1.se/images/file.jpg
So my question is. How can I use '/' in paths, so that it works the same way for both localhost and VirtualHost (domain)?
EDIT (I ask the question again, better and clearer this time hopefully.)
I need to be able to host multiple websites on my apache server.
I have managed to do it with the help of vhost.
For example:
<VirtualHost *:80>
ServerName site_1.se
ServerAlias www.site_1.se
DocumentRoot "C:/xampp/htdocs/site_1.se"
</VirtualHost>
<VirtualHost *:80>
ServerName site_2.se
ServerAlias www.site_2.se
DocumentRoot "C:/xampp/htdocs/site_2.se"
</VirtualHost>
So when I enter www.site_1.se I get to localhost(htdocs)/site_1.se. And when I enter www.site_2.se I get to localhost(htdocs)/site_2.se and so on. Perfect, that's the point.
Now here is the problem. These folders (site_1.se & site_2.se) contains html-files, and in these files I have links with addresses that starts with '/', which refers to the root.
So for example if have: <img src="/images/file.png">
inside a html-file in site_1.se, Apache tries to locate the image in:
localhost/site_1.se/images/file.png (if entering www.site_1.se)
or
localhost/images/file.png (if entering localhost/site_1.se)
So this obviously becomes a problem when developing in localhost and using '/' at the beginning of paths. Because when you then visit the page from the domain name, the links are wrong.
So I'm wondering how can I set up apache, so I can specify paths starting with '/', and get the same root no matter how I visit the site?
for localhost configuration listen on different ports so as to distinguish between the different sites. You need to tell Apache to listen on those ports also.
Listen 80
Listen 81
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/site_1.se"
Require all granted
</VirtualHost>
<VirtualHost *:81>
DocumentRoot "C:/xampp/htdocs/site_2.se"
Require all granted
</VirtualHost>

Apache2 redirecting to main site

I'm trying to host multiple websites off a digitalocean droplet using apache2's virtualhosts.
I have a config file for the main one, (pcnerd19.com) and one for the other, (efferri.ga). Whenever I type efferri.ga in firefox though, it just redirects back to pcnerd19.com I've restarted apache2, and made sure both sites are enabled. The is an index file at the root of efferri.ga as well. Here are my config files.
pcnerd19.com:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName pcnerd19.com
ServerAlias www.pcnerd19.com
ServerAdmin ruby#pcnerd19.com
DocumentRoot /var/www/wordpress1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/pcnerd19.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/pcnerd19.com/privkey.pem
</VirtualHost>
<Directory /var/www/wordpress1>
AllowOverride All
</Directory>
<IfModule>
efferri.ga:
<VirtualHost *:83>
ServerName efferri.ga
ServerAlias www.efferri.ga
ServerAdmin ruby#pcnerd19.com
DocumentRoot /var/www/efferri
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
It is worth mentioning I used certbot to get an ssl certificate on pcnerd19.com, and my droplet is running Debian.
From this page where someone had a similar problem, this answer might help:
When looking at your VirtualHost configuration, you’re setting a ServerAlias that defines the www. access point for each domain, but you’re missing a DNS entry on both domains that points www. to your domain. Since the DNS record isn’t set, Apache will handle it the best way it knows how, which may or may not be correct.
So what I would recommend doing is adding an A entry to each domains’ DNS records. The A entry should point to the same IP that your domain points to (i.e. the public IP of the Droplet).
So www should be an A entry that points to 46.101.19.243 in your case. Since it looks like your using DigitalOcean for your DNS, you can make these changes through their control panel and the updates should take effect pretty quickly. If they don’t, you may need to clear your browsers cache and then try to access your domains again.

virtual host not able to access file outside document root

I have setup virtual hosts for separate projects on my localhost on my macOS. Here is the httpd-vhosts.conf file:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot "/Users/Sammy/Sites"
ServerName 127.0.0.1
ServerAlias localhost
ErrorLog "/private/var/log/apache2/localhost-error_log"
CustomLog "/private/var/log/apache2/localhost-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/Sammy/Sites/workspace/abc/public_html"
ServerName abc
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/Sammy/Sites/workspace/xyz/public_html"
ServerName xyz
</VirtualHost>
I have also updated the list of hosts. The problem is, when I access my sites using this link, The sites are not able to load the css/js files & images present outside the 'public_html' directory, while it works if I open it via localhost path..
I come from an ubuntu environment, The apache directory structure is quite older on mac! can anyone help me with this..
Thanks
The sites are not able to load the css/js files & images present outside the 'public_html' directory
That is rather the point.
The DocumentRoot is the root of the website. Everything under it is part of the site.
By putting something in the DocumentRoot you are giving it a URL and making it available to be requested over HTTP.
An HTTP server isn't designed to expose every private file on your whole hard disk to the world. That would be awful.
If you want to give a file a URL, then put it in the DocumentRoot (or look at the Alias directives).

Apache settings to serve two sites in different directories

I am not very good with apache, as I've been an IIS guy for years.
I have two websites in the following directories:
/htdocs/ (site 1)
/htdocs/site2 (site 2)
Doing searches on the web does not give any understandable info on what to do other than a vhost file, etc. I've tried a few things with no luck.
What file(s) do I need to update so that I can set:
www.site1.com - /htdocs/
www.site2.com - /htdocs/site2
There are two parts to creating the solution: setting up your virtual hosts and then creating the right document roots for each host.
This can all be done in httpd.conf, but more cleanly as separate files in directory conf.d
So in site-1.conf you'd have these statements:
<VirtualHost www.site1.com:80>
ServerName www.site1.com
ServerAdmin me#myemail.com
DocumentRoot /var/www/htdocs
ErrorLog /var/log/httpd/site1.error.log
</VirtualHost>
And in site-2.conf you'd have this:
<VirtualHost www.site2.com:80>
ServerName www.site2.com
ServerAdmin me#myemail.com
DocumentRoot /var/www/htdocs/site2
ErrorLog /var/log/httpd/site2.error.log
</VirtualHost>

Why does http://localhost redirect to my default virtual host once I setup virtual hosts in Apache?

This is probably an easy question, but I want to understand better how Apache works with virtual hosts. I am setting up virtual hosts because I work on multiple websites at once and I don't want to use subdirectories. I was pretty much using the default Apache httpd.conf file with the DocumentRoot pointing to something like "/www". I uncommented the virtual hosts include and added the following:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName site1.dev
DocumentRoot /www/site1
</VirtualHost>
<VirtualHost *:80>
ServerName site2.dev
DocumentRoot /www/site2
</VirtualHost>
Now when I go to http://localhost I get the default page for site1.
I'm sure there is a reason why this makes sense, but I don't quite understand it. I would've thought that only requests that were explicitly to http://site1.test would get routed through that directive and it wouldn't just become the default. Can someone explain why it becomes the default.
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
(Should be true for 2.x also)
"If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a container and list it first in the configuration file."
answer 1 is correct
and i'd add with namevirtualhosts as the first entry
essentially matches any not-named elsewhere virtualhost
it should ONLY be used to catch unintentional mal-formed and broken traffic
ie a machene with one ip called john.domain.com running www.domain.com and www.domain2.com as valid webservers on ip www.xxx.yyy.zzz might have an optimal config like thus
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a directory readable by apache with only a robots.txt denying everything
ServerName bogus
ErrorDocument 404 "/errordocuments/unknown-name.html"
#custom 404 describing how/what they might have done wrong try pointing a browser {with a hosts file at http://bogus/ on 193.120.238.109 to see mine#
ErrorLog /var/log/httpd/unknown-error.log
CustomLog /var/log/httpd/unknown-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a possibly different directory readable by apache with only a robots.txt denying everything
ServerName www.xxx.yyy.zzz
ServerAlias john.domain.com
ErrorDocument 404 "/errordocuments/ip-name.html"
ErrorDocument 403 "/errordocuments/ip-name.html"
#custom 404 telling them as a likely hacker/bot you wish to have nothing to do with them see mine at http://193.120.238.109/
ErrorLog /var/log/httpd/ip-error.log
CustomLog /var/log/httpd/ip-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain.com
RedirectPermanent / http://www.domain.com/
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain
ServerName www.domain.com
ServerPath /domain
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
RedirectPermanent / http://www.domain2.com/
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain2
ServerName www.domain2.com
ServerPath /domain2
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
Confirming that for Apache 2.x, the first virtual host (with the same port number) will be used if a matching virtual host is not found.
http://httpd.apache.org/docs/2.2/vhosts/details.html
"If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected"
You can always add this code below, put it right below NameVirtualHost *:80 so that your default document root is served by default if no other virtual hosts found.
<VirtualHost *:80>
ServerName localhost
DocumentRoot /my/default/document/root
</VirtualHost>
Simply put this code at top in httpd-vhosts.conf
<VirtualHost localhost:80>
ServerName localhost
DocumentRoot d:/xampp/htdocs
<Directory "d:/xampp/htdocs/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
One way to do this is:
In your VirtualHosts configuration, enter the specific local site name you want to enable instead of using a wildcard:
<VirtualHost site1.dev:80> instead of <VirtualHost *:80>
Switch off NameVirtualHost *:80 which can be done by commenting it out in your vhosts.conf file
In your /etc/hosts file mention both aliases for the loopback IP:
127.0.0.1 localhost site1.dev
That's it. You should see that localhost goes to the default DocumentRoot as usual and the site1.dev goes to the site you've setup as virtual host.