3

I am looking to create links on my website like support.mydomain.com or management.mydomain.com rather than mydomain.com/support etc. I have heard you can do it with apache virtual hosts but I am not sure how I would set it up.

I am on apache 2.4 and ubuntu 16.04

  • 1
    You need to register the names support.mydomain.com, management.mydomain.com as aliases for mydomain.com with you friendly DNS service provider, then you need to read the fine documentation at Apache.org, especially the Apache Virtual Host Documentation. The implementation of Apache HTTP Server on Debian-derived system has some particularities, and you must also read the supplied file /usr/share/doc/apache2/README.Debian.gz. – AlexP Feb 13 '17 at 15:12
  • @AlexP thanks for the help, what section in particular should I look at in the Apache Virtual Host Documentation – Harry Cameron Feb 13 '17 at 15:39
  • Be sure that you fully understand how virtual hosts work; don't just skim, read it all, then create a toy configuration on a spare server or a virtual machine. Please note that the official documentation is written for a traditional setup, where all the configuration directives are in one file named httpd.conf. On Ubuntu, as on all Debian-derived systems, the configuration of Apache is split into multiple files; read the HTTPD - Apache2 Web Server chapter of the Ubuntu Server Guide. – AlexP Feb 13 '17 at 16:49
4

The Apache2 distribution comes with a very easy to use modular configuration.

Copy your 000-default.conf in /etc/apache2/sites-available to a file by then name you will call your virtual site. Use the configuraton file 000-default.conf as a template.

In this case you have indicated mydomain.com. You also indicated management.mydomain.com. You can use both to refer to access the same site. In these steps I'll use mydomain.com.

You can use any name for the configuration file. Using a name that includes the name you'll for your virtual host will make it easier to manage your sites if you make a number of them.

Run these steps. I numbered them to make it easy for you to point out which step, if any, you get stuck or don't understand. The $ is there to represent the terminal prompt where you type your command. The command is the text you see after the $ prompt.

1. $ mkdir -p /home/web/mysite/www
2. $ mkdir -p /home/web/mysite/log
3. $ cd /etc/apache2/sites-available
4. $ sudo cp 000-default.conf mydomain.conf
5. $ gksudo gedit mydomain.conf

Change from:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet


Change to:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerName mydomain.com
    ServerAlias www.mydomain.com
    ServerAlias management.mydomain.com

    ServerAdmin webmaster@localhost

    DocumentRoot /home/web/www
    <Directory /home/web/ubunzeus/www>
        Options +FollowSymLinks +ExecCGI +Includes
        # AllowOverride All
        # New directive needed in Apache 2.4.3: 
        Require all granted
    </Directory>
    
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog /home/web/mydomain/log/error.log
    CustomLog /home/web/mydomain/log/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Continue the following commands to activate the changes:

6. $ sudo a2ensite mydomain.conf
7. $ sudo service apache2 restart

With those steps, you can now access your new virtual host by typing the name you have given it into the web browser.

Place your webcontent (your html files) into the directory you created for your virtual host. In this example it's: /home/web/www. Your web directory can be placed anywhere on your system. You just need to configure your virtual host config file with the information.

Anytime you make a change to your Apache2 configuration files be sure to reload it to make the changes become effective.

$ sudo service apache2 restart

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.