Configuring virtual hosts on apache2 - apache2

I'm switching from Dreamhost to Rackspace Cloud hosting, since the formers ability to handle a rails app left something to be desired. I've got a CentOS server all set up and I've got my rails app deployed and the domains resolve and everything is great. Now I want to set it up so if I go directly to the IP address I'll get the the normal apache directory instead of the rails app. I thought the virtual host could manage this, but now both of them go to rails page or the apache page, as the case may be
<VirtualHost *:80>
ServerName 123.456.789.101
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerName mywebsite.com
ServerAlias www.mywebsite.com
DocumentRoot /rails/myapp/current/public
<Directory /rails/myapp/current/public>
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>
I'm not quite sure how to proceed, or if this is just impossible to begin with. Thoughts?

Apache takes the very first virtualhost to be it's primary host for the provided IP, (or all IP's in the case of *). Therefore you should be able to just setup your main virtual host record, make sure its the first vhost record that get's loaded and it will load that virtualhost whenever an unbound IP or unbound address is encountered. Then specify a second virtual host with the specific domains/subdomains you want to point to your rails app.
Please note that if you split your virtual host records across multiple files (i.e. sites-available/sites-enabled folders). Apache loads them in alphabetical order, often on debian based systems you will see the 000-default or similer file. This one uses numbers at the start to ensure its always the first one loaded and takes on the role of primary vhost

Related

What is the correct way to set up a virtual host running on Apache?

I configured apache on ubuntu 16.04 and I created a virtual host with these instructions:
<Directory /var/www/html/example.com/public_html>
Require all granted
</Directory>
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/example.com/public_html
ErrorLog /var/www/html/example.com/logs/error.log
CustomLog /var/www/html/example.com/logs/access.log combined
</VirtualHost>
when I try to open the page www.example.com on my browser I see this message:
This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.
Should I use another name instead of "example" to solve the problem?
Ok any name? For example "myname.com"
For clarity, I only need this for local tests
www.example.com is actual website that somebody else owns, has it registered in DNS and has some content on it. When you open the address in browser, your computer first asks DNS server (presumably one setup by your ISP). DNS then responds, that www.example.com is ran on 93.184.216.34 - which is probably not your machine. This is where the message is from.
Now, what is the meaning of ServerName and ServerAlias then? It is actualy a directive for your webserver, running on your machine, to know which packets are meant for given virtual host. It does not affect how your browser will transalate them.
For the sake of local testing (both server and browser are on same OS), all you need to do is to create virtualhost for a 'localhost'. This is a reserved address for the machine currently being used. Simply put:
ServerName localhost
ServerAlias localhost
into your virtualhost and you will be able to access the webserver by entering 'localhost' into your browser.
In case you would need more than one host locally, you can override the dns by editing the host file https://en.wikipedia.org/wiki/Hosts_(file) - for example, in windows by adding hosts records like:
127.0.0.1 app.local
127.0.0.1 db.local
and then creating two different virtual hosts, one with alias app.local, second with db.local. Then, the OS will then route requests from your browser correctly to your machine per hosts file. Your websever will then sort your request according to the header to proper virtualhost.
You might want to check out some general information on how dns and internet protocol works:
https://en.wikipedia.org/wiki/Internet_protocol_suite
https://en.wikipedia.org/wiki/Internet_Protocol
https://en.wikipedia.org/wiki/Domain_Name_System

Apache2/Tomcat8 virtual host JSF application returning incorrect urls (the app name is duplicated)

I have a website (http://www.goodfoodwaiheke.org) configured as a virtual host on Apache2. The virtual hosting configuration redirects traffic from apache2 to tomcat8 via these conf parameters:
<VirtualHost *:80>
ServerName www.goodfoodwaiheke.org
ServerAlias goodfoodwaiheke.org
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/coop/
ProxyPassReverse / http://localhost:8080/coop/
</VirtualHost>
On tomcat, the /coop/ part of the address invokes the JSF application in coop.war. This seems to work. The correct welcome page is served except for one problem. The url for static resources are being served incorrectly. For example, the url of the background image on the welcome page, which is actually in http://www.goodfoodwaiheke.org/coop/resources/images/GoodFoodWaiheke4.png is served to the browser as http://www.goodfoodwaiheke.org/coop/coop/resources/images/GoodFoodWaiheke4.png (i.e. the coop app name is duplicated in the url). This is causing static resources to not work. Although I can sort of understand why this may be happening I am unsure of how to fix it. I know I could offload the static resources onto the Apache2 server but for various reasons I would like the war file to be self-contained and for tomcat to be responsible for serving the whole site.
I'd be very grateful for any suggestions as to what I need to change.

virtual hostname instead of i.p. address in mediawiki

I am trying to replace my i.p. address from my mediawiki with a virtual hostname. My system information are
O.S.: Opensuse 13.1
Mediawiki: 1.26.1
apache2 and mysql
I have edited the /etc/apache2/vhosts.d/mydomain.conf file like this
<VirtualHost 123.456.78.90>
ServerAdmin webmaster#localhost
ServerName www.mylabwiki.org
DocumentRoot /srv/www/htdocs/mylabwiki
ErrorLog /var/log/apache2/myabwiki-error_log
CustomLog /var/log/apache2/mylabwiki-access_log combined
HostnameLookups Off
UseCanonicalName Off
ServerSignature On
<Directory "/srv/www/htdocs/cgi-bin">
AllowOverride None
Options +ExecCGI -Includes
Require all granted
</Directory>
<Directory "/srv/www/htdocs/mylabwiki">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
I have edited the /etc/apache2/listen.conf file like this
NameVirtualHost 123.456.78.90
Then I have executed
vi /etc/hosts
123.456.78.90 www.mylabwiki.org
Then I restarted the apache2
service apache2 restart
My problem is, it works only locally. If I use the browser from the host computer, the domain 'www.mylabwiki.org' works properly.But, if I type from a remote computer www.mylabwiki.org and try to connect, it says that the url doesn't exist. However, if I type http://123.456.78.90/ then works perfect from the remote computer. Can anyone tell me where did I miss something! I can feel that I am just one step away, but unable to figure it out.
Your issue is that www.mylabwiki.org is known only on your localhost, because you edited the /etc/hosts to have a local DNS resolution. Your remote computer do not get that information to know where to go.
There is 3 ways to solve the issue:
you edit the /etc/hosts on the remote computer as well, but that means it will work on your localhost and remote computer, but not on nay other. So a real solution.
you have a DNS local server, and you edit the server to resolve the name to the IP address. That will work for all computers on your network, not the outside world.
you record this information in a domain name provide like name.com (or any other), so anyone in the world would know how to resolve the domain to the IP address. For sure it means the IP/computer can be accessed from outside your network.

Connect to multiple local sites on mobile

I have multiple local sites which I develop on my local machine.
I'd like to be able to access them on a mobile device while they are still local on my machine.
Using XAMPP these are example vhosts configs:
<VirtualHost *:8080>
DocumentRoot "C:/design and photos/agency1/website/public_html"
ServerName agency1dev.com
<Directory "C:/design and photos/agency1/website/public_html">
Options All
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot "C:/design and photos/captives/website/public_html"
ServerName capdev.com
<Directory "C:/design and photos/captives/website/public_html">
Options All
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Windows hosts file
127.0.0.1 agency1dev.com
127.0.0.1 capdev.com
I understand I can use my machines IP address to reach e.g.:
http://196.168.2.3:8080
But this goes to the XAMPP dir, so, how do I access e.g. agency1dev.com?
Is this possible?
You can try using Finch, https://meetfinch.com
It's very easy to use (recently released a GUI for it) and just 'works' without having to do anything.
Full disclosure: I'm involved in the project.
The solution I found was using NGROK
Very easy to implement:
In cmd or some shell
Go to: C:\path\to\ngrok
Type:
ngrok http -host-header=myapp.dev 8080
Change myapp.dev to suit what you want to look at - and change port (8080) accordingly
Then look at output and it will give you a http and https url to navigate to on your device
Simple!

Multiple Domains on a VPS with Apache

I am planning on getting a VPS soon and have two sites I want to host. I have my local vhosts setup for my development environment using host file to port it correctly.
My question is hopefully simple: When setting up with two separate domains that point to the one VPS server is Apache smart enough, like on the local environment, to automatically filter any requests for domain.com to the correct VHOST like it does locally? I would just like to be sure :)
Thanks!
Example of what I am asking:
Say Domain1.com and Domain2.com are both on my VPS.
When someone requests www.Domain1.com apache sees this and passes it through to the domain1.com vhost file.
When someone requests www.Domain2.com apache sees this and passes it through to the domain2.com vhost file.
Repasting my answer from Hosting two domains using only one VPS? as here it is even more relevant.
As complete beginner, I have been trying to host multiple domains on one Apache VPS. Tutorials had too much information that lead me to confusion.
Below I describe, for complete beginners, how to host multiple domains on one VPS server with Ubuntu and Apache.
IMPORTANT! You need to use root account to execute most operations.
IMPORTANT! If you have been trying to make some changes to apache configuration before, undo them.
Creating VirtualHosts
Create folders for your domains on server.
For example:
/home/apache/domain1
/home/apache/domain2
Put index.html file in each folder with any text.
This is domain1
This is domain2
Go to /etc/apache2/sites-available folder.
Create file domain1.conf
sudo nano domain1.conf
<VirtualHost *:80>
DocumentRoot /home/apache/domain1
ServerName domain1.com
ServerAlias www.domain1.com
</VirtualHost>
Create file domain2.conf
sudo nano domain2.conf
<VirtualHost *:80>
DocumentRoot /home/apache/domain2
ServerName domain2.com
ServerAlias www.domain2.com
</VirtualHost>
You can create subdomains same way.
sudo nano blog.conf
<VirtualHost *:80>
DocumentRoot /home/apache/blog
ServerName blog.domain.com
ServerAlias www.blog.domain.com
</VirtualHost>
Enable created sites
sudo a2ensite domain1.conf
sudo a2ensite domain2.conf
Restart apache
sudo service apache2 reload
Redirecting domain to server
Created VirtualHosts will work only if you redirect your domain name to server IP. Domains are just names that can be translated to IP numbers.
Local computer
To test your configuration on local machine, you need to edit hosts file.
sudo nano /etc/hosts
It should look like this.
127.0.0.1 localhost domain1.com domain2.com
Hosts file tells your computer that domain needs to be redirected to local machine.
IMPORTANT! If you create entry in hosts file for existing domain, for example
127.0.0.1 stackoverflow.com
you will loose access to this website.
Server
In order to redirect domain to you web server, you need to create or modify "A"-type DNS record for given domain to IP address of your server. You can do it by panel control provided by your domain registrar.
If you do not know IP address of your server, log in to that server and type in command line:
ifconfig
The simple answer is 'yes', Apache is that clever. If you are used to using a local vhost file in conjunction with your hosts file to simulate local domains, the exact same technique can be applied on a VPS. The part you are doing with the hosts file is essentially creating a local name server, other than that Apache doesn't know the difference. Simply set the ServerName directive for each named vhost and you should find it working the same as it does locally.

Resources