Multiple domain names on a single server - apache

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.

Related

Running multiple domains on same server

I have a Ubuntu server running Apache and have 3 sites under /var/www/website/abc, /var/www/website/xyz and /var/www/website/lmn. I have 3 domains (www.abc.com, www.xyz.com, www.lmn.com) mapped to same machine (mapped same ip to 3 different domains on godaddy).
So I googled around and found this link - virtual host setup and made abc.com.conf in /etc/apache2/sites-available/ and correspondingly for other sites. Enabled the sites and then restarted apache but same site(/var/www/website/abc) appears on all 3 domains. I rechecked the paths but they seem to be correct. I can't figure out what is wrong. How can I route them to their corresponding sites?
It would be helpful in the future if you share your code (in this case the apache config files) to figure out what's wrong. In any case, this is roughly how the files can look (they don't have to look like this, there are other ways it can be configured).
First check /etc/apache2/apache2.conf and make sure you see the following code:
IncludeOptional sites-enabled/*.conf
The apache2.conf file is the primary configuration file. That line above includes all of the configuration files in the site-enabled folder. If you use a Red Hat derived OS you'll notice that the configuration file structure is different (Debian derivatives like Ubuntu like to split everything up into tons of configuration files, Red Hat derivatives keep it together)
Make sure that each of the files in the sites-enabled folder includes lines that look like this.
For abc.com.conf:
<VirtualHost *:80>
ServerName www.abc.com
DocumentRoot /var/www/website/abc
</VirtualHost>
If you also want "abc.com" to point to this virtual host enter "ServerAlias abc.com" underneath the ServerName line. What you're doing here is creating a VirtualHost block for any ip address (*) on port 80 (:80). You could replace the * in the opening VirtualHost line with your external ip address if you want to make sure that the VirtualHost is only matched to a particular ip (this is only potentially needed if there are multiple external ips pointing to your webserver). The ServerName line tells apache to match this VirtualHost whenever the Host HTTP header is www.abc.com. ServerAlias can be used to specify additional Hosts to match. Remember that www.abc.com and abc.com are treated as different Hosts. The DocumentRoot line sets the directory from which files are served.
Similarly for xyz.com.conf:
<VirtualHost *:80>
ServerName www.xyz.com
DocumentRoot /var/www/website/xyz
</VirtualHost>
If you also want "xyz.com" to point to this virtual host enter "ServerAlias xyz.com" underneath the ServerName line.
And finally for lmn.com.conf:
<VirtualHost *:80>
ServerName www.lmn.com
DocumentRoot /var/www/website/lmn
</VirtualHost>
If you also want "lmn.com" to point to this virtual host enter "ServerAlias lmn.com" underneath the ServerName line.

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 I point multiple URLs to the right directory?

I have a VPS running CentOS 5. I want to point multiple domains at the same VPS (point them at the same IP), but serve up distinct websites for each domain.
So, the setup I want is like this:
site1.com ----> 127.0.0.1 ----> /var/www/html/site1.com/
site2.com ----> 127.0.0.1 ----> /var/www/html/site2.com/
I've tried setting up virtual hosts through the CentOS GUI for Apache, but they don't seem to be working. Only the first virtual host added will work (i.e. If I add site1.com first, both domains direct to that site. If I add site2.com first, both domains direct to that site.)
What might I be doing wrong? Is there an effective step-by-step tutorial for newcomers to get me started?
Update
Please remember, I have little to no experience working with CentOS and Linux ... but I'm learning.
To those who asked to see the directives I'm using, here's what CentOS added to the bottom of /etc/httpd/conf/httpd.conf when I used the built-in Apache GUI:
<VirtualHost skylarkapp.com:80>
DocumentRoot /var/www/html/skylarkapp.com
<Directory "/var/www/html/skylarkapp.com">
allow from all
Options +Indexes
</Directory>
ServerName skylarkapp.com
</VirtualHost>
<VirtualHost eamann.com:80>
DocumentRoot /var/www/html/eamann
<Directory "/var/www/html/eamann">
allow from all
Options +Indexes
</Directory>
ServerName eamann.com
</VirtualHost>
At the moment, all traffic to both skylarkapp.com and eamann.com on this system direct to the content of /var/www/html/skylarkapp.com.
Also, I'm using my Windows hosts file to manually point eamann.com to this server (204.92.23.6) because I'm in the process of migrating a live site. I want to get things working on the server before I actually move things from one box to another.
Name-based virtual hosts must be turned on explicitly with NameVirtualHost *:80. See http://httpd.apache.org/docs/2.2/vhosts/name-based.html
This line:
<VirtualHost eamann.com:80>
won't work since eamann.com won't resolve to your CentOS server's IP address (only your Windows machine resolves eamann.com to 204.92.23.6). Try making apache listed on any IP by replacing with:
<VirtualHost *:80>
You probably also want:
ServerAlias www.eamann.com
to allow visitors to www.eamann.com to see the site as well. Restart apache after making these changes:
service httpd restart

Ubuntu Server with Apache. Domains Management

Lets' asume I have 1 domain on a Ubuntu Server in the following directory:
/var/www/domain1.com/httpdocs
and that the ip address is 100.100.100.100
If I go to www.domain1.com, Apache will server the files inside the httpdocs folder.
How can I avoid that if the following file exists:
/var/www/domain1.com/privatefile.html
.. apache shows it by going to:
http://100.100.100.100/domain1.com/privatefile.html
In other words, I want to display the content in the httpdocs only, no by ip address.
In a vhost setup, Apache will use the first vhost defined as the default one to serve when a request comes in by IP. So just make a "dummy" vhost that points nowhere, and make sure it's the first one in the config file:
<VirtualHost *:80>
ServerName nothing.nowhere
ServerAdmin nobody#nothing.nowhere
DocumentRoot /var/empty
<Directory /var/empty>
Order Allow,Deny
</Directory>
</VirtualHost>

setting a default apache virtual host

Is there any better way of setting the default apache virtual host other than it just picking the first config it finds?
I have a server with many domains, of which only some are configured with httpd but the default virtual host severed up is for example is aaa.com where as really I would like it to default to mmm.com instead?
Something like parking domains without going through the hassle of setting up a config for each one - then I can serve a "content this domain has not been created yet" page?
Cheers
You can create a default virtual host and name it something like 000-default so that it loads first and is used unless another vhost matching the requested domain is found. Here's the bare-bones 000-default:
<VirtualHost *:80>
DocumentRoot /var/www
<Directory /var/www >
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Then you can setup a PHP file under /var/www to create a domain parking page (this is a very simplified example):
<?php
printf('The domain <b>%s</b> is being parked',
htmlentities($_SERVER['HTTP_HOST']));
?>
The first sites-available conf file is default (alphabetically). There should be a 000-default.conf file already, if not create it.
Edit this one to your liking and then make sure it's enabled a2ensite 000-default.conf. And apache2 is reloaded sudo service apache2 reload.
Then any request that isn't caught by your other vhosts will come here.
Use ServerAlias in a name-based VirtualHost, you will only have to add one line per each new domain.