How to serve sites from http://ip/folder? - apache

Please help.
I'm trying to setup 3 test sites, so each could be reached from my server's ip like:
my-server-ip/site1
my-server-ip/site2
my-server-ip/site3
When I use ServerName as ip it works fine:
<VirtualHost 127.0.0.1:8080>
ServerName x.x.x.x
DocumentRoot "/home/myfolder/public_html"
<Directory "/home/myfolder/public_html">
AllowOverride All
</Directory>
</VirtualHost>
but using ServerName x.x.x.x/site1 is not working ofcourse.
I tryed to use Alias /site1 /path and it works for first virtual host but when i add 2 more virtual hosts to vhosts.conf only first works.
Another trick that i tryed is to add:
Alias /site1 /home/folder1/public_html
Alias /site2 /home/folder2/public_html
Alias /site3 /home/folder3/public_html
into /usr/local/apache/conf.d/domain-redirects.conf
and add options into /usr/local/apache/conf/httpd.conf
And this does work but not correctly.
I know I could use some domain name and setup a buch of subdomains for other sites..but using ip is better.
Is there a way to set things up this way?

You need to deposit your sites into the directory that the server accesses. The default that apache gives is /var/www. So if your file is at /var/www/test.html you can access it with 127.0.0.1:8080/test.html .

Thanks! Your idea led to solution. Just in case someone needs the same setup, here is what i've done (running on Centos 6 + Apache+Varnish).
Create and upload folders/files just like you do for normal domains.
By default http://ip -> my apache looks into a folder:
/usr/local/apache/htdocs
I could create a folder here and upload a site here but rather I've created a symlink to my testsite:
ln -s /home/user/public_html/test_site_folder testsite1
now my testsite is available at http://ip/testsite1
almost...to make it work for user I added in httpd.conf this statement (AllowOverride) in order to be able to use mod_rewrite and others in .htaccess located in root folder of testsite1
*** added
<Directory "/usr/local/apache/htdocs">
suPHP_UserGroup user user
AllowOverride All ***
Order allow,deny ***
Allow from all ***

Related

How to access Apache website by public-ip with server name?

I've installed vanilla at Ubuntu server with public-ip by the steps at https://www.vultr.com/docs/how-to-install-vanilla-forum-on-ubuntu-16-04
Then config /etc/apache2/sites-available/forum.example.com.conf as below
<VirtualHost *:80>
ServerName forum.example.com
DocumentRoot /var/www/vanilla
<Directory /var/www/vanilla>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Then I can access http://public-ip at browser, but the result is Apache2 Ubuntu Default Page.
How can I access the installed vanilla at the browser? Should I make any change to the forum.example.com.conf?
If the request doesn't match a virtual host, Apache will serve files from the top level (not inside a <VirtualHost> block) DocumentRoot config value.
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot /var/www/html
I don't know Ubuntu well but I'd guess this is in /etc/apache2/apache2.conf. You can change this to /var/www/vanilla to serve your forum instead of the default page.
Alternatively you could rename / delete the existing /var/www/html and make it a symlink to /var/www/vanilla: then Apache would pick up the forum files without any config change.
Or if you just want to set up access for yourself then you can add the DNS name to your hosts file (/etc/hosts on Linux, \Windows\System32\drivers\etc\hosts on Windows)
forum.example.com AAA.BBB.CCC.DDD
and then you can use forum.example.com in your browser as normal, which will send the 'Host' header so Apache will match the virtual host, even though this isn't configured as public DNS.

Prevent access to files through ip address - apache 2.4

I have asked a similar question before
Restrict access to directories through ip address
at that time the problem was solved for apache 2.2. Recently I re-installed the OS (to Debian 8) and it comes with apache 2.4.
I want to restrict access to files - when the request comes "by" IP. Mainly if in the browser I try to open http://192.168.252.178/test/image.jpg it should show error - 403 forbidden. Directory test is in www directory of apache. However I should be able to access that image if I type http://www.example.com/image.jpg - considering that example.com points to that test directory.
With apache version 2.2 I would simply put this lines in my default site config file - and the problem was solved
<Files ~ ".+">
Order allow,deny
Deny from all
</Files>
Now, trying the same thing does not work: I am getting 403 forbidden even if I try to open any site by the domain name.
Considering the changes in 2.4 I also tried this, but again getting the the same 403 forbidden when trying to open some site.
<Files ~ ".+">
Require all denied
</Files>
My goal is to prevent any kind of access to directories and files - if they are being accessed through ip address. I have also this lines in my default site's config to prevent the directory access and this works fine.
<Directory /home/username/www>
Options -Indexes
AllowOverride All
Require all granted
</Directory>
So, the question is - how to prevent file access through IP address. Also I need to achieve this by apache config, by htaccess is not a solution for me. And I need to achieve this for all the directories/files inside www recursively, so specifying the exact file names and/or directories is not a solution either.
Thanks
When you use name based virtual hosts, the main server goes away. Apache will choose which virtual host to use according to IP address (you may have more than one) and port first, and only after this first selection it will search for a corresponding ServerName or ServerAlias in this subset of candidates, in the order in which the virtual hosts appear in the configuration.
If no virtual host is found, then the first VHost in this subset (also in order of configuration) will be choosen. More.
I mention this because it will be important you have only one type of VirtualHost directive:
<VirutalHost *:80>
or
<VirtualHost 123.45.67.89:80>
I'll use the wildcard in the example.
You need a directory like /var/www/catchall with a file index.html or similar, as you prefer.
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
# It will be used as the catchall.
ServerName 123.45.67.89
# Giving this DocRoot will avoid any request based on IP or any other
# wrong request to get to the other users directories.
DocumentRoot "/var/www/catchall"
<Directory /var/www/catchall>
...
</Directory>
</VirtualHost>
# Now you can add as usuall the configuration for any other VHost you need.
<VirtualHost *:80>
ServerName site1.com
ServerAlias www.site2.com
DocumentRoot "/home/username1/www"
<Directory /home/username1/www>
...
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName site2.com
ServerAlias www.site2.com
DocumentRoot "/home/username2/www"
<Directory /home/username2/www>
...
</Directory>
</VirtualHost>
Debian specific :
For Debian, you ideally put one VHost configuration per file, and put the file in the /etc/apache2/sites-available directory.
Name the files as you like, only the file containing the catchall vhost should be named something like 000-catchall, because they will be read in alphabetic order from the /etc/apache2/sites-enabled directory.
Then you disable Debian's usual default site :
a2dissite 000-default
and you enable the new catchall site and the other VHosts if needed :
a2ensite 000-catchall
An ls /etc/apache2/sites-enabled command should show the catchall as the first of list, if not change its file name so that it will always be the first. Restart Apache: service apache2 restart
Of course you could do all this changes in the original default VHost config file, but I usually prefer keep an original model.

How to establish a virtual host properly using WampServer?

I have a problem in creating my virtual using WampServer. In the office I can create and run a virtualhost properly. But when I tries to create in my laptop. I t redirects to the localhost page of my wampserver. It displays the default page of wampserver. Here's what I did.
First I move my project in this directory: - C:\Users\jerielle0214\Documents\Chelle\hallohalloalliance
Second is I add the virtual host in apache (C:\wamp\bin\apache\apache2.2.22\conf\extra\httpd-vhosts.conf)
<VirtualHost *:80>
ServerName chelle.hallohalloalliance.com
ServerAlias chelle.hallohalloalliance.com
DocumentRoot "C:/Users/jerielle0214/Documents/Chelle/hallohalloalliance/"
<Directory "C:/Users/jerielle0214/Documents/Chelle/hallohalloalliance/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
And I include my virtualhost in the Windows hosts file. I add this in the bottom of the hosts file
127.0.0.1 chelle.hallohalloalliance.com
Last I restarted the wampserver and I access my page using chelle.hallohalloalliance.com but it only redirect my page to default wamp page. I don't know where's the error. Please help me guys thanks.
Apache will fallback to the first host it was correctly told about when there is a problem with your VHOST definitions.
Did you uncomment this line in the httpd.conf file so Apache includes your vhost definition?
#Include conf/extra/httpd-vhosts.conf
just remove the #.
Also when you change the hosts file you need to reboot or refresh the DNS Cache for the changes to be visible to windows. To refresh the cache do this from a command window ( run as Administrator )
net stop "DNS Client"
net start "DNS Client"

Accessing to a file and folder in Apache from Localhsot

I want to create a virtual folder with a file in it in Apache, and to see that file from my browser : how can i fix it ?
the error i get when i try my local host in browser:
You don't have permission to access / on this server.
I tried:
1) created a folder in etc/apache2/sites-available
2) checking the default Apache settings which is not blocking my IP which is 192.168.1.5:
Assuming that you are trying to serve /yourfolder. Change the permission of the folder to give access right to others (755 for example) and add following to site conf you have created.
<VirtualHost *:80>
ServerName example.com:80
DocumentRoot /yourfolder
# Change according to your folder path
<Directory /yourfolder>
Order Allow,Deny
Allow From All
Options -Indexes
</Directory>
</VirtualHost>

Multiple domain names on a single server

Sorry my English
I have Ubuntu 12.04 openVZ VPS, with Apache and Passenger installed, to run Ruby site. That site have several faces which available (not yet) from different domains. In /etc/apache2/sites-available I created file site1.com which contains this:
<VirtualHost *:80>
ServerName site1.com
DocumentRoot /home/happy_buddha/Sites/rubysite/public
<Directory /home/happy_buddha/Sites/rubysite/public>
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>
Previously I created A record on site1.com domain which contains server's ip.
Now if I going to site1.com browser's window contains this:
What I'm doing wrong?
you can not give virtual host name same as other's.
Like you said " I created A record on site1.com domain which contains server's ip." then you have to change your virtual host's name. Try with different name. for eg site2.com or site1.local
Make sure that you have NameVirutalHost *:80 set in your apache config - this will probably be in /etc/apache2/ports.conf on your Ubuntu server.
It sounds like the new site you created is acting as the default virtual host which is why you can't see the site you had before.
That said, if you actually visit site1.com, then I might expect to see the result you get, I don't know where the config is done for interpreting the ruby code but this might need to be part of the VirtualHost too.