How to create subdomains on DigitalOcean LAMP

Madgeek
3 min readMar 31, 2018

--

This article is to demonstrate how to create subdomains on DigitalOcean droplet(VPS) in a few simple steps.

It also demonstrates how to configure the Apache Virtual Host configuration file. In the VPS (Virtual Private Server) world, being able to get up and running quickly (with subdomains) is priceless (especially if you’re new to it). It will save you lots of time — you can spend tweaking your code, writing tests, debugging, and sharing your works with friends and family.

Who’s this for?

This tutorial is for anyone out there, who wants to set up his/her subdomains on a VPS using a provider such as DigitalOcean quickly without lots of head-to-walls banging. The principles in this tutorial apply to whichever VPS provider you are using, as long as it's a Linux system, we’re on the same page.

And so, you want to achieve something like this with your domain:

blog.madgeek.in

Or

ecommerce.madgeek.in

If so, then you are in the right place.

I’m assuming you have a VPS or DigitalOcean Droplet configured with the NS(Name Server) records (Have demonstrated this in a video - Setup DNS from GoDaddy with DigitalOcean Host). You can read more about that here How to add domain & host your websites on DigitalOcean

Connect to your VPS or DigitalOcean Droplet using the SSH command. If you are clueless about how to - check this video - For Windows systems and for MAC follow my previous video on How to add domains and host websites on DigitalOcean droplet.

ssh {username}@{ip_address}

Replace {username} with the sudo user, you have created or for now, you can use root user credentials.

(NOTE: It’s recommended not to use root credentials but rather create a sudo user for your activity).

Step 1: Create a new A Records

Go to the networking section if you are using DigitalOcean else go to your DNS management console.

Create a new A Record with hostname being ex. alpha or ecommerce. Where redirect to is the IP address i.e. basically the IP address of the droplet.

Step 2: Create sub directory & host project files

Create a directory under /var/www/ the directory using the commands

cd /var/www/
mkdir subdomain
cd subdomain
sudo nano index.html

When you are inside the index.html file, write anything ex. hello world.

`Hello World`

Step 3: Configure Virtual Host

Now let’s configure the apche2 virtual host, which will allow our domains to refer to a project directory.

Type in the following commands

cd /etc/apache2/sites-available/
cp 000-default.conf subdomain.website.conf
nano subdomain.website.conf

Now once you are inside the config file, change the following

#ServerName
#ServerAlias
#DocumentRoot

Here, the ServerName is basically the domain that you want - say ecommerce.madgeek.in, ServerAlias is basically another name using which you want to refer this domain to any CNAME in most cases. DocumentRoot is where your project files are located (Root directory of your project)

Point DocumentRoot to the directory where your project files are located.

ServerName subdomain.website.com
ServerAlias www.subdomain.website.com

Save the file and restart the server

sudo service apache2 restart

Now try to access your subdomain

https://ecommerce.madgeek.in

Cheers!

Read the full article

--

--

Madgeek
Madgeek

Written by Madgeek

Madgeek is a software agency renowned for its expertise in developing tailored enterprise tools and e-commerce solutions.

Responses (5)