Setting Up a Subdomain with Apache on Linux

Setting up a subdomain with Apache on Linux

You are telling Apache what IP and port you want to answer it on inside of the <VirtualHost> tag so here * means any IP, but accept requests for this site on port 80. Next you need to tell Apache where the document root is. ~/ means your default home directory, so if your DocumentRoot just happens to be the default home variable then it would work with your existing notation (depending on which user you're running the server as). Then you would declare the server name.

Each domain name you're create a host for needs its own Virtual Host Directive unless you're using aliases.

<VirtualHost *:80>
DocumentRoot /home/sam/public_html
ServerName myproject.localhost

# Other directives here

</VirtualHost>

<VirtualHost *:80>
DocumentRoot /home/sam/public_html/myproject
ServerName myotherproject.localhost

# Other directives here

</VirtualHost>

About Hosts
In addition to this, any special name that you create for a host needs to go into a hosts file or in the DNS server as well. This way any web browser that is looking for your server can find it without having to type in the IP. Since you'll likely have multiple hosts on the same IP with your setup if you were to try and access the server with the IP only, you would only get the first host to respond on the IP (usually the top in the vhosts list).

How properly set a website and domain (with subdomains) with apache on ubuntu?

Create a virtual host in Apache by creating a file at /etc/apache2/sites-available/subdomain.website.com.conf

In that file, add the following

<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName subdomain.website.com
DocumentRoot /var/www/subdomain.website.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Now place your subdomain.website.com files at

/var/www/subdomain.website.com/public_html

Then enable the new virtual host by sudo a2ensite subdomain.website.com

After placing the files, if you get a 403 forbidden error, check for permissions of the DocumentRoot folder.

Refer: https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts

How to configure subdomains for Apache2 on Ubuntu?

As Mark B already answered correctly the problem was a misconfiguration in /etc/hosts. The correct configuration is:

# /etc/hosts
127.0.0.1 test.localhost

Apache make subdomain and change main domain port

You can configure your Apache to proxy port 80 -> 3000.

<VirtualHost *:80> 
ServerName api.domain.com
DocumentRoot "/your/laravel/application/path/public"
</VirtualHost>

<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName domain.com
ServerAlias domain.com
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>

Once you're done with configuring Apache, you'll have to enable the required Apache modules and restart.

sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart

[Derived from this answer.]

As for your Laravel application, I'm going to assume you've installed PHP and loaded the Apache PHP module.

how to create a subdomain in a dedicated apache2 server

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.

How to setup sub-domains like blogspot

You can make a CNAME entry/ A Record in your DNS settings, for each subdomain

A CNAME record is a record in your
Domain Management Settings that allows
you to control a subdomain of your
domain.

To automate it along with registration, you can write a script which is executed for each user, when s/he registers.

You can refer to this link, as well, for a step-by-step process for Apache:

How to setup subdomains in apache

(since you mentioned Linux, I assume it must be APache. Please mention if it is otherwise)

Alternate Solution

You can also refer to the wildcard solution, given by Alnitak, in the same thread. I find his is an easier way. :)

creating subdomains with apache on debian; creating hgweb subdomain as second step

Well, good news - I fixed my issues.

First step was removing libapache-mod-wsgi-py3 and installing libapache-mod-wsgi (during my tries I re-installed wsgi-py3-package several times, seems like my last re-installation fixed that somehow...)

apt-get remove libapache2-mod-wsgi-py3
apt-get install libapach2-mod-wsgi-py3
apt-get autoremove

After that, restarting apache got me the following in my error.log:

mod_wsgi (pid=16349): Target WSGI script '/var/www/hgrepos/cgi-bin/hgweb.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=16349): Exception occurred processing WSGI script '/var/www/hgrepos/cgi-bin/hgweb.wsgi'.
Traceback (most recent call last):
File "/var/www/hgrepos/cgi-bin/hgweb.wsgi", line 20, in <module>
application = hgweb("/var/www/hgrepos/cgi-bin/hgweb.config")
File "/usr/lib/pymodules/python2.6/mercurial/hgweb/hgweb_mod.py", line 32, in __init__
self.repo = hg.repository(u, repo)
File "/usr/lib/pymodules/python2.6/mercurial/hg.py", line 94, in repository
repo = _lookup(path).instance(ui, path, create)
File "/usr/lib/pymodules/python2.6/mercurial/bundlerepo.py", line 305, in instance
return bundlerepository(ui, repopath, bundlename)
File "/usr/lib/pymodules/python2.6/mercurial/bundlerepo.py", line 177, in __init__
raise util.Abort(_("%s: not a Mercurial bundle file") % bundlename)
Abort: /var/www/hgrepos/cgi-bin/hgweb.config: not a Mercurial bundle file

Following this, I set up my hgweb.wsgi-file like this:

#!/usr/bin/env python
#

# Uncomment and adjust if Mercurial is not installed system-wide:
#import sys; sys.path.insert(0, "/usr/lib/pymodules/python2.6")

# Uncomment to send python tracebacks to the browser if an error occurs:
import cgitb; cgitb.enable()

from mercurial import demandimport; demandimport.enable()
from mercurial.hgweb import hgweb, wsgicgi

# Path to repo or hgweb config to serve (see 'hg help hgweb')
config = "/var/www/hgrepos/cgi-bin/hgweb.config"
application = hgweb(config)

Restarting Apache

/etc/init.d/apache2 restart

and now everything works for me.

A big THANKS again to krtek who really helped out ending my pain! Made my day :D



Related Topics



Leave a reply



Submit