virtual host not able to access file outside document root - apache

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).

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>

How do I make my new website public?

I have registered a new domain and set nameservers, A-records. I have also added conf files in my sites-enabled and sites-available directories under apache2 directory.
What would be the next step to lead incoming traffic to my appropriate site?
<VirtualHost *:80>
ServerAdmin admin#x.com
ServerName x.com
ServerAlias www.x.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
You seem to be missing the document root entry in your virtual hosts file. That is your next step to redirect traffic to your site.
Place this under server alias
DocumentRoot /var/www/x.com/public_html
Then you put your website in the folder /var/www/x.com/public_html. Make sure you modify the access rights so anyone can view the public_html contents
Check out this link for further details.
https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts

Unsure of what to add to hosts file - Apache Subdomain

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

Apache document root and VirtualHost

I'm having some problems with setting up a VirtualHost with Apache on CentOS. I'll try my best to explain what happens.
In our httpd.conf file we had to change the Document root, our base is /var/www/mydomain.com but our files is on /var/www/mydomain.com/store2 and when users goto mydomain.com we want them to come directly to mydomain.com/store2
Main config:
DocumentRoot "/var/www/mydomain.com/store2/"
Directory "/var/www/mydomain.com/store2"
So, I've added an config file inside conf.d named subdomains.conf and this is the information inside
<VirtualHost *:80>
ServerAdmin mail#me.com
DocumentRoot /var/www/mydomain.com/subdomain
ServerName subdomain.mydomain.com
ErrorLog logs/subdomain.mydomain.com
CustomLog logs/subdomain.mydomain.com common
</VirtualHost>
When i restart the httpd service and goto mydomain.com I'm transferred to the virtual host root (/var/www/mydomain.com/subdomain) this also happens when i go to subdomain.mydomain.com. Is there something that I'm missing here? Could there be something with the DNS also? I see that mydomain.com and subdomain.mydomain.com points to the same IP-address.

Apache VirtualHost won't work (Wamp/Win7)

I own two domains which I want to point to my local server here at home:
www.first.com
www.second.com
They both point to my own server here at home (Yes I've already managed the A-record config at my webhotel where I registered the domains).
I've done the following:
In httpd.conf, I've uncommented the line Include conf/extra/httpd-vhosts.conf
In httpd-vhosts.conf I have the following code:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName first.com
ServerAlias www.first.com
DocumentRoot "C:/wamp/www/public/first"
</VirtualHost>
<VirtualHost *:80>
ServerName second.com
ServerAlias www.second.com
DocumentRoot "C:/wamp/www/public/second"
</VirtualHost>
In the httpd.conf file, I've set document-root to C:\wamp\www\public, and this is the step I'm most uncertain of. What should the document root be in the httpd.conf file when the httpd-vhosts.conf file declares multiple document roots? I've tried to set the document root of httpd.conf to both C:/wamp/www/public and only C:/wamp/www/
What am I missing here? The pages won't load at all.
DocumentRoot for "main" server haven't sense as soon as you define Virthosts - known virthosts will be served from their definition, unknown - by first virthost in list
Create separate log-files per virthost and yes, check logs after access attept
The pages won't load at all
It's bad error-description. Which error-code you get? Do you have DirectoryIndex and index-file in virthost root? What about Allow/Disallow directives for Directory locations?