How to configure custom hostname to IP resolutions in my system for web development - development-environment

Preface
A web app can potentially
serve different pages,
depending on the
hostname
that is requested by the browser,
even if all hostnames are resolved
to the same
IP address.
Example
For example, at
https://app.example.com,
which resolves to
1.2.3.4,
users will find the user interface
and at
https://admin.example.com,
which also resolves to
1.2.3.4,
awaits a dashboard
through which
only the app's owner can
administrate users and data
in the app.
What We Need
In short,
we need to enter,
for example,
http://admin.app:8000/
in our browsers
and have that admin.app resolve to 127.0.0.1.
The Question
How can I configure
custom hostname to IP address resolutions
in my development environment?

(Ubuntu and Derivatives) Configure NetworkManager's dnsmasq
Ubuntu Desktop's default networking configuration is
composed of NetworkManager and its slave dnsmasq.
The slave dnsmasq listens at 127.0.1.1 and /etc/resolv.conf lists it as the only nameserver.
This has
some benefits.
What it means for this purpose is that we have a fully configurable DNS server, comfortably configured by default.
We can create
/etc/NetworkManager/dnsmasq.d/hosts.conf
and put in it whatever address statements we'd like:
address=/admin.app/127.0.0.1
We can even use wildcards!
address=/.app/127.0.0.1
See the
dnsmasq documentation
for details
(look for --address).
Since dnsmasq is started by the network-manager service,
then I would assume that the following would restart it
so that new configuration would take effect:
$ service network-manager restart
But its init-script does not control slave dnsmasq.
Therefore the dnsmasq process must be killed and then
the above command would have it start again.
And that is it!

(Linux) User Specific HOSTALIASES File
Very limited
This would have been my preferred answer
because
it refrains from
altering system configuration.
But:
It does not support wildcards
It does not support hostname to IP address resolution
It does not support freely configurable subdomains
It will not work if you have a local DNS server,
which is the case in modern Ubuntu.
What is It
It is a user specific host aliases file.
Notice that the format is not the same as the hosts file.
In short, you create a file
which contains host aliases.
For example
foo localhost
bar localhost
and place it at ~/.hosts.
Then you set an environment variable
HOSTALIASES
with the path to the aliases file.
So, for this example
$ export HOSTALIASES=~/.hosts

If Testing In a Virtual Machine
In a virtual machine
127.0.0.1 and localhost
will not reach the host,
but the guest.
In VirtualBox, for example,
by default, the host can be reached
at 10.0.2.2.
So, the guest VM's hosts file can look like
10.0.2.2 host
10.0.2.2 app.host
10.0.2.2 admin.host
Proxy DNS Nameserver Inside a Virtual Machine
If you're setting up
a proxy DNS nameserver
inside a virtual machine
(perhaps for wildcard support in Windows)
the upstream nameserver
is usually provided by the host.
In VirtualBox, it is 10.0.2.3.

(Windows) Configuring Acrylic DNS Server
Acrylic DNS Proxy is easy to install and configure.
It can help us get hostnames with aliases quickly in Windows.
And it is open source.
Install it.
Open the hosts file (via the start menu entry).
Put in some entries, like 1.2.3.4 >app.
Clear its cache and restart it (via the start menu entry).
Set your DNS server to 127.0.0.1.

(Windows, Linux, OSX) System Wide Hosts File
Simply edit the
hosts file.
Its location
depends on the OS.
For example:
127.0.0.1 app.localhost
127.0.0.1 admin.localhost
On Windows you can use
this nifty open source GUI
for editing the hosts file:
Hosts File Editor.
Wildcards
The hosts file does not support wildcards!
Ubuntu Desktop
Since Ubuntu 12.04,
Ubuntu desktop comes with
a local DNS server,
which might not respect
the hosts file (/etc/hosts).
So, for Ubuntu desktop, this answer
is best.

(GNU/Linux)
Since all the major distributions are migrating (or already did) to systemd stack the proper place to implement wildcard support would be systemd-resolved: see https://github.com/systemd/systemd/issues/766 for details.
That would be the place to set custom overrides for DNS as well.
As for windows - its VM should just get DNS from host machine: it's to risky to run it on bare metal anyway.

Related

Remote access of network computer

I am not sure how or if this can be done. I have a home network and would like to see a computer,not the server, via a remote location. I have Apache on my server. Example: the network computers I would like to see ip 152.254.1.33. Is there a way to add this ip to Apache root directory? I have tried to add a shortcut with in the root directory and it only works on the home network, will not via remote connection.
I need some clarification here on what you are trying to acomplish, are you trying to access the Apache website outside of the local network?
If that is the case, Apache is automatically set to listen on all network interfaces, you can check this in your virtual host configuration in the sites-enabled directory of your apache installation.
You should see something like in the 000-default.conf
You can test if apache is serving pages up correctly using the command
curl 127.0.0.1
You should see the HTML of the page being served.
If this is the case, then it's likely the firewall on your machine/router or your ISP is blocking the required ports. You can allow Apache through the firewall on Ubuntu using sudo ufw allow Apache Full
If you give me some more info in comments we can probably work this out.

Ubuntu Server with Apache domain management

I'm currently trying to set my Apache configuration on ubuntu server. I have one IP(ex: 34.228.1xx.xx) and one domain(ex: mydomain.com).
I succeed to connect "Apache2 Ubuntu Default page" using IP 34.228.1xx.xx
Then I added 34.228.1xx.xx mydomain.com in hosts file(/etc/hosts) and restarted apache using this command sudo service apache2 restart
But I can't see "Apache2 Ubuntu Default page" using domain mydomain.com, but I still can connect it using IP 34.228.1xx.xx
This is my first web hosting I know my question is messed.
Thanks you in advance.
0.0.0.0 is not a port, it's an IP. And it is a special one which shouldn't go to /etc/hosts. Remove it.
To access Apache on the local machine (on the same on which Apache is running), use "127.0.0.1" or "localhost" as host name in your browser.
On all other machines, use either the machine's IP or its DNS name.
Of course, you have to ensure that the DNS name is pointing to the right IP. This should be a public IP (not something like 192.168.x.x or 10.x.x.x etc.) If you haven't done so already, you must register this name with your DNS hosting provider as a DNS "A" record.
Additional points to consider:
Firewall on Apache machine must be open for incoming traffic on ports tcp/80 and/or tcp/443
If your server is behind NAT, there must be a "forwarding" rule on the NAT machine

Apache localhost already used?! Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName"

I have a problem, I had apache Solr installed and it uses localhost for access on webserver...
now I have installed Apache and startet httpd.exe and I get the warning / error:
Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName"
when I stop solr, I can start and use localhost for apache httpd...
but I want to have the possibility to use both webservices, how can I configure Apache to do that? and get access to service not with localhost, but with another domain name or some configuration in httpd.conf
I tried to change the line "ServerName www.example.com:80" in httpd.conf file but no effect,
sorry but I'm pretty new to webservers and Apache, how should I configure that?
You have several things mixed here:
Installing Solr should in no way be related to other questions, unless it is configured to run on port 80. If that is the case, you simply can not run two services on the same port so you have to pick, or just move Solr to 8080 or whatever.
Could not reliably determine... is just a friendly warning and will not prevent any functionality, and it should disappear when you add ServerName www.example.com:80 in httpd.conf
With default settings, Apache will respond to any http request that comes to port 80, so you don't have to configure anything there (and if you want to modify that, use VirtualHost). You can achieve reaching your webserver by other hostnames by editing hosts file on your machine. If you want others to be able to do that, you have to configure DNS (which is separate issue)

Configure virtual host on Apache

I have a VPS with a single IP address and with Virtualmin/Webmin installed. I have created a virtual host *example.com *that will answer to www.example.com (the domain already points to the IP address of the VPS). However, when I access www.example.com I am not seeing the website I have uploaded for example.com but rather the default server:
It works!
This is the default web page for this server.
The web server software is running but no content has been added, yet.
What should I do? Do I have to configure DNS or something?
Yes, you will need to configure DNS for your www.* site if it has not been done already. This www.* alias may have been created automatically when you set up the domain in DNS, but perhaps you will need to specify it explicitly.
Seeing the message "It works!" is a common issue. Apache has a default virtual host which you may want to disable. Exactly how to do that depends on the platform of your web server (it's a good idea to mention the platform and version when asking questions).
For example, if you have shell access to a Debian or Ubuntu machine, you can use the command:
sudo a2dissite default
In virtualmin there may also be an option for this, see for example http://www.virtualmin.com/node/15854
Once you have DNS set up and the www.* domain responding correctly to pings with IP of your server, you will also need to make sure that the virtual host definition for your site includes the www.* alias.

How do I point a domain back to my own machine? (only for my own browser)

I want to set up a machine for testing a website before launching it. Some of the tests require logic in parsing the current host.
I want to be able to test it in my browser but i can only access it through the localhost.
I want to know how to make my browser resolve my domain to 127.0.0.1:80. (Also I don't want my DNS record to point to my test machine, this should be a local test only).
Also, is there a way to make all sub-domains of my domain access 127.0.0.1:80 as well?
Extra Info --
OS: Windows 7
Browser: Chrome
Web Server: Tomcat 7
Thank You.
EDIT ---
The solution was to add a few lines to the host file:
127.0.0.1 example.com
etc.
You're looking for the hosts file. It's more than likely what points localhost to 127.0.0.1 on your computer.
You can find it at C:\Windows\System32\drivers\etc\hosts on Windows. Or /etc/hosts on most Linux systems, if you're interested.