In this article I explain how to add subdomains to your Apache2 Web Server specifically on a Raspberry Pi. A good friend of mine asked me sometime back how this can be done so I figured I would write this article for everyone. Just an FYI this article can be ported on any hardware configuration regardless of a Raspberry Pi or not. A last mention that entry was created as is. I will not hold any liability should any misconfigurations are made.
Prerequisites:
- You have a domain.
- You have pointed DNS of your subdomain (alias.domain.com) to you linux server.
- You have installed apache2
- You have configured your domain but now it’s time to add subdomains.
- You know how to edit files on linux.
- You know how to create files on linux.
- You know how to ask questions if you get stuck.
Getting started:
First off I am going to assume that you already have one of your domains setup via apache2 and you have it configured properly. So before we dive in I am going to ask that you make a backup of the following file:
- 000-default.conf
This file is located in /etc/apache2/sites-available/ to create a backup you can simply use the copy command as so.
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/000-default.conf.bk
We have created a backup of the 000-default.conf just in case we need to revert back for any reason.
Navigating and editing:
Ok, now that we have our backup it’s time to get to work. Below is a list of steps on how to get your subdomain configured. If for whatever reason I miss a step please do not hesitate to call me out on it so I can make the necessary adjustments to this article.
1.) First we need to navigate to apache2’s sites-available directory to do this run the following command in your terminal:
cd /etc/apache2/sites-available/
2.) Next like we did when we created a backup of our sites configuration file we need to create a new one (I like to do this) and call it our site. In this example I will configure music.davidpolanco.com by creating a configuration file for it. Proceed by running the copy command below and changing music.davidpolanco.com to your alias.domain.com
cp 000-default.conf music.davidpolanco.com.conf
3.) Excellent, we are off to a good start. Now we will proceed with editing the music.davidpolanco.com.conf or your alias.domain.com. Now you will need to proceed with editing it with something like vim, vi, or nano. (I won’t judge you on your editor) In my case I will use VIM however if you are new to linux I would consider using nano.
vim music.davidpolanco.conf
4.) Now we will proceed with editing our default configuration. Begin by running the following command you can use one or the other:
vim music.davidpolanco.com
Or if you are new to linux run:
nano music.davidpolanco.com
Do not forget to change music.davidpolanco.com to your domain alias! 🙂 Once you are done with this your configuration file should look like the one below. If it doesn’t don’t worry for the most part the configuration is the same. Also we will begin cleaning the default configuration up in step 5:
<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
5.) Clean up time if you notice the above configuration there sure are a lot of comments with all those # signs. We will proceed with clearing them up so now yours should look like mine below:
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Much better right? I would agree!
Editing the configuration file:
6.) Now that we have things cleaned up it’s to make some additions. Below is mine, but I would ask that you use as an example configuration below mine.
<VirtualHost *:80> ServerName music.davidpolanco.com ServerAlias music.davidpolanco.com ServerAdmin webmaster@localhost DocumentRoot /var/www/media/music/ DirectoryIndex application.php index.php index.html index.htm <Directory /var/www/media/music/music.davidpolanco.com/> Require all granted Options Indexes FollowSymLinks MultiViews AllowOverride all Order allow,deny allow from all Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /log/ "/var/log/" <Directory "/var/log/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from all Require all granted </Directory> </VirtualHost>
I would like to make you aware that in DocumentRoot and Directory I am using the path:
/var/www/
And not:
/var/www/html
So you will NEED to edit this to /var/www/html again so there is no confusion copy and paste this:
USE THIS CONFIGURATION: (copy and paste is welcomed)
<VirtualHost *:80> ServerName alias.domain.com ServerAlias alias.domain.com ServerAdmin webmaster@localhost DocumentRoot /var/www/html/hosting/alias.domain.com DirectoryIndex index.php index.html index.htm <Directory /var/www/html/hosting/alias.domain.com/> Require all granted Options Indexes FollowSymLinks MultiViews AllowOverride all Order allow,deny allow from all Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /log/ "/var/log/" <Directory "/var/log/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from all Require all granted </Directory> </VirtualHost>
Now simply replace alias.domain.com to your own domain in the configuration above and save this file.
7.) Now we need to symlink our work for sites-enabled to the sites-available directory which is located one directory above. Begin by navigating to the sites enabled directory:
cd /etc/apache2/sites-enabled
Now we will run following command (this will create a symbolic link aka short cut from the sites-available configuration file and folder to sites enabled)
ln -s ../sites-available/alias.domain.com.conf alias.domain.com.conf
Creating our subdomains homepage:
8.) Now we will proceed with creating a directory where your alias.domain.com will live. In my example for you and as shown in the configuration above we will map our alias.domain.com to a directory in /var/www/html in a new directory called hosting. So we will need to create a directory in /var/www/html to do so run the following two commands. One will create a hosting directory and the other will create the alias.domain.com directory. (again be sure to change alias.domain.com)
mkdir /var/www/html/hosting/
Next run:
mkdir /var/www/html/hosting/alias.domain.com
9.) Now that we have our two directories created with the commands above we need to create an index file inside of it. An index file is simply a file that a webpage recognizes as the beginning page in that particular directory. To do this run the following command: (do not forget to modify alias.domain.com to your subdomain:
touch /var/www/html/hosting/alias.domain.com/index.html && echo "hello world" > index.html
10.) Now it’s time to set permissions so that our newly created directory and index file is viewable. To do this simply run the command and don’t forget to make the necessary changes to alias.domain.com to your subdomain:
sudo chown www-data:www:data /var/www/html/hosting/alias.domain.com && chmod -R 755 /var/www/html/hosting/alias.domain.com
Wrapping things up:
10.) Now that we have our index.html file created we need to proceed with telling apache2 to notice our new subdomain. You will now run the following command: (again don’t forget to change the alias.domain.com to your subdomain.
a2ensite alias.domain.com
11.) Now then nothing is going to work until we restart Apache2. There are a few ways to do this but I’m old school so run the following:
/etc/init.d/apache2 restart
It works! “hello world”
Now proceed with viewing your newly configured sub-domain. Go to your browser and go to alias.domain.com and you should view a message that says “hello world”. If you see this you should be all set! Now simply edit the index.html located in /var/www/html/hosting/alias.domain.com/index.html if you can to change it to something else.
Conclusion:
I do hope this helped you. Please leave a comment below if you have any questions and I will be happy to reply and help you out.
Leave A Comment