Tuesday, April 12, 2016

Apache2 Server Subdomains

Apache2 Server configuration for multiple web applications is confusing for beginners. Since I have just started configuring apache server in my Ubuntu 14.04 system, I am going to share my experiences while installing and configuring Apache Server.

Basic Commands

Here are the basic commands need to learn:

1) Installation :
       sudo apt-get install apache2
2) Reload:
       sudo service apache2 reload
3) Restart:
       sudo service apache2 restart
4) Enable website:
       sudo a2ensite [WEBSITE_NAME]
5) Disable website:
       sudo a2dissite [WEBSITE_NAME]

Problem Scenario

So, the problem scenario is I have to install multiple web applications in the same host but with different URLs. And the web applications can be placed anywhere in the server. And I also discuss about defining sub-domains. 

For example, I have three web projects located in /home/frietec/www/redmine, /home/frietec/www/owncloud, /home/frietec/www/ldap and I want to access these projects with redmine.frietec.com, cloud.frietec.com, ldap.frietec.com.


The solution for this is not so difficult. We need to configure virtual host.  First of all, we have to define the virtual hosts in config file as below:

    DocumentRoot /home/kris/public_html
    ServerName myproject.localhost

    # Other directives here

    DocumentRoot /home/kris/public_html/myproject
    ServerName myotherproject.localhost
    # Other directives here

In this example, it is important that the defined sub-domains should point to the same local machine. So, we either place this information to a DNS server to publicly access. To test in local network, we modify the file /etc/hosts with the subdomains point to the same IP. This way removed the burgeon of remembering the ip-address of the system.

File: /etc/hosts      localhost      kpsharma   myproject.localhost   myotherproject.localhost 

Note: if we use IP address instead of domain-name, then it uses the topmost virtual host. In this case, the project "myproject.localhost" is called.

No comments:

Post a Comment