Question

How do I add sub domain and make it point to a certain directory?

I want to add subdomain dev.mydomain.com that point to /var/www/html/dev.

I currently have a main domain and my server is Debian 8.2 x64 running on LAMP. My apache configuration /etc/apache2/sites-available/000-default.conf which I think is correct file to check:

<VirtualHost *:80>
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        <Directory /var/www/html>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride FileInfo
                Order allow,deny
                allow from all
        </Directory>
        #LogLevel info ssl:warn

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

</VirtualHost>

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

It looks barehand because I removed almost all comments. Not sure where did that vim line come from. My site works, my domain points to /var/www/html. (Off-topic, i would like it to point to /var/www/ but if this is default, then it must be correct and common way)

Anyway I tried looking up how to add sub domain, found this reply on relevant thread but I don’t know if it’s okay to uncomment ServerName and set it as dev.mydomain.com and change directory from var/www/html to var/www/html/dev because that’s what everyone is saying. It just doesn’t make sense to me. If I do that, then my main domain and main directory configuration will probably cease to function (my guess).

I also do not know what to type in droplet cpanel in domain settings to make this work. Advice on that will be very helpful.

Can you advise me how to add a subdomain dev.mydomain.com and make it point to var/www/html/dev/ folder?

Subscribe
Share

Could I ask which instruction should I follow in this page to add subdomain dev.mydomainname.com in DO cpanel and add dev.mydomainname.com in certain file to make it working?

I don’t even have ServerName setup, as mentioned in the thread, I’m worried if I setup ServerName with dev.mydomainname.com in 000-default.conf (I dont even know if it’s right file) and it will break something.

Even though you’re on a Debian distro, this tutorial on Apache virtual hosts on ubuntu should still be relevant. The red colored font shows which changes to make on your config file that you copied. BTW, the vim line is usually towards the top of the file for the vim editor to honor the directives. P.S. - The tutorial uses two same level directories, and not a subdirectory of another virtual host.


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

@dmxt

  1. cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/dev.mydomain.com
  2. edit the file /etc/apache2/sites-available/dev.mydomain.com and make the necessary changes to make it look like the following:
<VirtualHost *:80>
        ServerAdmin webmaster@dev.mydomain.com
        ServerName dev.mydomain.com
        DocumentRoot /var/www/dev

        <Directory /var/www/dev/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

        <files xmlrpc.php>
            order allow,deny
            deny from all
        </files>

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

Note: As mentioned in my previous comment, use same level directory instead of subdirectory, i.e, /var/www/dev and /var/www/html.
3) a2ensite dev.mydomain.com 4) service apache2 reload 5) create the directory /var/www/dev and add a sample index.html file with appropriate ownership and permissions for use with apache. 6) Test on the server using wget or curl for both dev.mydomain.com and mydomain.com

Hope that works for you.

@dmxt

  1. cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/dev.mydomain.com
  2. edit the file /etc/apache2/sites-available/dev.mydomain.com and make the necessary changes to make it look like the following:
<VirtualHost *:80>
        ServerAdmin webmaster@dev.mydomain.com
        ServerName dev.mydomain.com
        DocumentRoot /var/www/dev

        <Directory /var/www/dev/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

        <files xmlrpc.php>
            order allow,deny
            deny from all
        </files>

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

Note: As mentioned in my previous comment, use same level directory instead of subdirectory, i.e, /var/www/dev and /var/www/html.
3) a2ensite dev.mydomain.com 4) service apache2 reload 5) create the directory /var/www/dev and add a sample index.html file with appropriate ownership and permissions for use with apache. 6) Test on the server using wget or curl for both dev.mydomain.com and mydomain.com

Hope that works for you.

@dmxt

  1. cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/dev.mydomain.com
  2. edit the file /etc/apache2/sites-available/dev.mydomain.com and make the necessary changes to make it look like the following:
<VirtualHost *:80>
        ServerAdmin webmaster@dev.mydomain.com
        ServerName dev.mydomain.com
        DocumentRoot /var/www/dev

        <Directory /var/www/dev/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

        <files xmlrpc.php>
            order allow,deny
            deny from all
        </files>

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

Note: As mentioned in my previous comment, use same level directory instead of subdirectory, i.e, /var/www/dev and /var/www/html.
3) a2ensite dev.mydomain.com 4) service apache2 reload 5) create the directory /var/www/dev and add a sample index.html file with appropriate ownership and permissions for use with apache. 6) Test on the server using wget or curl for both dev.mydomain.com and mydomain.com

Hope that works for you.