how to create a subdomain in a dedicated apache2 server - linux

I want to create a subdomain I followed this steps :
cd /etc/apache2/sites-available
nano test.mydomaine.com
<VirtualHost *:80>
ServerAdmin mymail#gmail.com
ServerName test.mydomaine.com
ServerAlias www.test.mydomaine.com
DocumentRoot /var/www/testfolder/
<Directory "/var/www/testfolder">
AuthType Basic
AuthName "test"
AuthUserFile /var/www/testfolder/passwords
Require valid-user test
</Directory>
</VirtualHost>
exit and save
a2ensite test.mydomaine.com
/etc/init.d/apache2 reload
but It's not working am I missing something ?

In order for this to work, you additionally have to configure DNS resolution for the new hostname test.mydomain.com. If you are just playing with this locally and do not want to create a "real" DNS entry, you have to edit the file /etc/hosts so that your computer can resolve the hostname to an IP address. If you are running the web server on the same machine as the browser, you will want to map test.mydomain.com to 127.0.0.1. If the browser is on a different machine on your local network, you'll need to determine the server's IP address and then on the browser machine, edit /etc/hosts to add the mapping.
On Windows, the file is called C:\windows\system32\drivers\etc\hosts
A sample entry would be (for the browser running on the same machine as the server):
127.0.0.1 test.mydomain.com
If the server is at, say 192.168.0.5, the entry would be
192.168.0.5 test.mydomain.com
EDIT: If the server has a real routable IP address, then if you want the test.mydomain.com address to resolve on the global Internet you will have to get your service provider to add it to DNS. For testing purposes, you can still use /etc/hosts as described above. Just substitute the server's real IP instead of 127.0.0.1. Do this on the system where you are running the browser.

Related

Automatically create a sub-domain for laravel application using wild card dns

I am using laravel5.5 and I'm implementing the wild card dns giving each user their own sub-domain.
In my implementation, I am working on a laptop with windows 10 and a laragon server.
On laragon I setup like this
<VirtualHost *:80>
DocumentRoot "C:/laragon/www/tindahan/public/"
ServerName tindahan.local
ServerAlias *.tindahan.local
<Directory "C:/laragon/www/tindahan/public/">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
and on the host file
127.0.0.1 tindahan.local
127.0.0.1 fil.tindahan.local
127.0.0.1 liz.tindahan.local
On the route
Route::get('/', function () {
$url = parse_url(url()->current());
$domain = explode('.', $url['host']);
$subdomain = $domain[0];
dd($subdomain);
});
So when visiting the fil.tindahan.local or liz.tindahan.local, I got the result
fil and Liz
but if I visit joseph.tindahan.local I got this error
This site can’t be reached
I need to create another sub-domain on the host to make it work.
The question is, how can do it automatically?
when I enter any names as a sub-domain it should be automatically created so that I never do it manually?
As far as I know, the error will resolved in network layer, not from application layer (in this case laravel).
I think you need your own local DNS-Server.
I've used dnsmasq in Linux (ubuntu) and it worked fine.
steps:
apt install dnsmasq
open /etc/dnsmasq.conf with an editor.
add address=/your_domain.loc/127.0.0.1 (it's just an example so you'll need change domain and ip probably).
run sudo systemctl restart dnsmasq to restart the service.
Well, you can test any url from your_domain.loc in your browser and you will not get that error ;)
goodluck

Unable to create URL with wampserver

I am trying to create a URL for a site hosted through wampserver, but no matter what I do I am unable to get the URL to work. The site is online because I am able to connect through the servers IP address, though.
(I should also mention that this site is only available on an intranet)
hosts file:
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 localhost
127.0.0.1 www.socialclub.com #also tried public/private IP, still only works locally
vhosts.conf:
# Virtual Hosts
#
# Required modules: mod_log_config
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerName localhost
DocumentRoot "E:\Data\Users Apps\wamp\www\socialclub"
</VirtualHost>
<Directory "E:\Data\Users Apps\wamp\www\socialclub">
AllowOverride All
Order Allow,Deny
Allow from all
Options Indexes FollowSymLinks Includes ExecCGI
</Directory>
<VirtualHost *:80>
DocumentRoot "E:\Data\Users Apps\wamp\www\socialclub"
ServerName www.socialclub.com
</VirtualHost>
Every guide I've looked at says that this should work, but it only works locally. What do I need to do for the URL to work from other computers?
Ok I think the problem is your are not understanding what the HOSTS file is used for and what its scope is.
The HOSTS file only effects the single PC that it lives on. It is used to seed the windows DNS cache at boot time. So whatever you put in this file will have no effect on any other PC in your intranet.
There are a couple of solutions :
Lets assume your PC running WAMPServer has the ip address 192.168.1.10:
You could go to each PC in your intranet and make this change to the
HOSTS file on each PC
192.168.1.10 socialclub.com
people normally think this is too much hassle especially if they have more than 5-6 PC's to mod
You could install a local DNS Server, or make use of an existing
local DNS Server. Then as long as all the PC's in your intranet are
using that DNS Server you add the domain name to that DNS Server.
people normally think this is a good idea, but it can be quite complicated to get this right and not loose access to the real DNS servers out there on the web
A couple of changes I would suggest to your httpd-vhost.conf file
First leave localhost pointing to the original wampserver homepage, but only allow access from the PC running WAMPServer. The tools on the homepage can be a very useful for debug/diagnostics/etc, but only allow access to locahost from the PC running WAMPServer.
Second put the <Directory></Directory> block inside the Virtual Host definition. This allows you to make each virtual hosts security specific to that virtual host.
# Should be the first VHOST definition so that it is the default virtual host
# Also access rights should remain restricted to the local PC and the local network
# So that any random ip address attack will recieve an error code and not gain access
<VirtualHost *:80>
DocumentRoot "c:/wamp/www"
ServerName localhost
<Directory "c:/wamp/www">
AllowOverride All
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "E:\Data\Users Apps\wamp\www\socialclub"
ServerName www.socialclub.com
<Directory "E:\Data\Users Apps\wamp\www\socialclub">
AllowOverride All
Options Indexes FollowSymLinks Includes ExecCGI
# assuming your subnet equates to this range
# and you are using Apache 2.4.x
# its not necessary to allow access from all in an intranet
# in fact it might be dangerous
Require ip 192.168.1
</Directory>
</VirtualHost>

virtualhost without real domain name

I've this configuration for my virtual host and runs with real domains. For testing purpose I need create same virtual host with non real domain (i.e. domain10). Considerated that the browser check the name of domain of virtual host, I think using something like 127.0.0.1. But I've no idea to realize this.
<VirtualHost *:80>
ServerAdmin info#domain10.org
ServerName domain10.org
ServerAlias domain10.org
DirectoryIndex index.html index.htm index.php
DocumentRoot /home/domain10.org/
<Directory /home/domain10.org/>
Options -Includes -Indexes -FollowSymLinks -ExecCGI +MultiViews
AllowOverride none
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
You can use the /etc/hosts file for this purpose. There should already be a line in it beginning with 127.0.0.1. Just add domain10.org to the list of hosts that resolves to this address.
For example, if the existing line is:
127.0.0.1 localhost localhost.localdomain
Just extend it to:
127.0.0.1 localhost localhost.localdomain domain10.org
Then (on this machine only) domain10.org will resolve to 127.0.0.1. If you are testing on the same machine, you can enter http://domain10.org/ in your browser to reach your local web server.
If you need to reach this host over the Internet, then a different approach is needed. Look for a line for the external IP address of the server in its /etc/hosts file and modify it similar to the above instructions. If such a line does not exist, add one.
On the client machine, do the same (using /etc/hosts for a Linux box or %WINDIR%\system32\drivers\etc\hosts for a Windows box). You should then be able to reach the server from the client using this name.

Apache virtual hosts - server ip directing to virtual host instead of /var/www/html

I have set up a linux box mainly for testing and I have got to a stage were apache, mysql and php are running. I followed tutorials on how to set up virtual hosts so I can point domains to it but something strange is happening.
This is my httpd-vhosts.conf file that I have included in my httpd.conf file:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName thedomain.co.uk
DocumentRoot /var/www/thedomain
</VirtualHost>
When I go to thedomain.co.uk it is pointing to the correct place as you'd expect. But when I go to my servers ip, internal or external it is going to the same directory as the virtual host. Could you guys think of any reason that is? I would expect it to go to /var/www/html by default? Oh and I'm running CentOS 6.3
Thanks in advance for any answers!
You have used wildcard to point everything at DocumentRoot /var/www/thedomain
You need to create second listing :
<VirtualHost myotherdomain.co.uk:80>
ServerName myotherdomain.co.uk
DocumentRoot /var/www/myotherdomain
</VirtualHost>
where your other domain is whatver ip and the document root, points to your choise.
The first vhost listing is also used as the default ... so if you were to use localhost it would resolve to whatever is first in list.

Apache subdomains on ubuntu server from laptop

I'm on my laptop and I want to create subdomains for the server on my network. On my laptop, I modified my hosts file so that I can access my server with the adress myserver
If I access myserver, I get the default apache server html page saying "it works".
On my server, the default document root is /var/www/
I want to create a subdomain for a user such that I would type user.myserver and access files at /home/user/Websites/
Now, I modified httpd.conf on my server to add:
<VirtualHost *>
ServerName *
DocumentRoot "/var/www/"
</VirtualHost>
<VirtualHost *>
ServerName user.localhost
DocumentRoot "/home/user/Websites/"
</VirtualHost>
Also, my hosts file on my server is modified to:
127.0.0.1 user.localhost
if I put user.myserver on my laptop, my browser says it can't find the server.
What am I missing ?
You don't need to modify hosts file on your server, it's only for server resolving, apache will just listen to what Host header it will receive.
You need to modify hosts file on your laptop to have user.myserver point to the IP of your server.

Resources