sam's page

Dec 04, 2016

Multiple subdomains using nginx and namecheap's dynamic DNS

Here I will describe how I made my website with multiple subdomains running on a Raspberry Pi 3 using nginx, pelican and namecheap's dynamic dns.

Step 1 - Install and configure nginx

I think most linux distributions will have nginx available to install. On a debian-based system such as Raspbian, you can just run:

sam@my-pi:~ $  sudo apt-get install nginx

Setting up your domain and subdomain is easy with nginx! You can add a configuration file for each domain and subdomain in the /etc/nginx/sites-available/ folder and when it's ready, you can create a symlink to it from the /etc/nginx/sites-enabled/ folder. Here is an example for mysite.com with subdomains sub1.mysite.com and sub2.mysite.com:

First, here is /etc/nginx/sites-available/mysite.com.conf

# mysite.com

server {
        listen 80;
        server_name mysite.com;
        root /path/to/mysite.com;
        index index.php index.html index.htm;
}

Then for the first subdomain we have /etc/nginx/sites-available/sub1.mysite.com.conf

# sub1.mysite.com

server {
        listen 80;
        server_name sub1.mysite.com;
        root /path/to/sub1.mysite.com;
        index index.php index.html index.htm;
}

Finally, for the second subdomain we'll make /etc/nginx/sites-available/sub2.mysite.com.conf

# sub2.mysite.com

server {
        listen 80;
        server_name sub2.mysite.com;
        root /path/to/sub2.mysite.com;
        index index.php index.html index.htm;
}

For the root path, just make sure that it's somewhere which the nginx user (usually www-data, but can be changed in /etc/nginx/nginx.conf file) has read and write access.

Now when you're ready to use them, you can symlink from the sites-enabled directory:

sam@my-pi:~ $  cd /etc/nginx/sites-enabled
sam@my-pi:/etc/nginx/sites-enabled $  ln -s ../sites-available/mysite.com.conf .
sam@my-pi:/etc/nginx/sites-enabled $  ln -s ../sites-available/sub1.mysite.com.conf .
sam@my-pi:/etc/nginx/sites-enabled $  ln -s ../sites-available/sub2.mysite.com.conf .

That's it for the nginx part of this tutorial! Easy, peasy, lemon squeezy!

Step 2 - Set up Namecheap's dynamic dns service

I have to say that in the short time I've been using Namecheap, I really like their service and prices! (And if they want to give me a discount for writing this, that would make them even better!)

For a short time, I was using pagekite to make my site avaiable on the web. But this was before I learned about Namecheap's dynamic dns settings! This was really easy to set up on my Raspberry Pi using the ddclient and simply turning on the service in Namecheap and adding an A record. It might be easy to do through godaddy or some other service, but if you're using Namecheap, then try this!

Login to your namecheap dashboard and next to the domain you want to modify, click on "Manage". When you're there, click on the "Advanced DNS" tab. Now scroll down until you see "Dynamic DNS" and click the toggle to turn the service on. Below this toggle, you should now see a Dynamic DNS Password which looks like a long string of numbers and letters. You're going to need this to set up the config file for ddclient.

Next, below where you just toggled this service on, there is a place to add an "A + Dynamic DNS Record". For mydomain.com, you'll want to enter @ for the host and your pi's current IPv4 address for the value. For sub1.mydomain.com, you'll enter sub1 for the host and similar for sub2. They should all have your pi's current IPv4 address for the value. The ddclient you will run will use the Dynamic DNS Password to keep your current IP address updated with Namecheap.

If you're running raspbian, you can install ddclient by simply running this command:

sam@my-pi:~ $ sudo apt-get install ddclient

During the install, it will asks you some questions. For one of them, it will ask you for a dynamic dns provider. The one used by namecheap wasn't an option when I installed, but don't worry about that because we'll change the config file next to fix this.

After it's installed, you'll want to edit /etc/ddclient.conf to look like this:

use=web, web=dynamicdns.park-your-domain.com/getip
protocol=namecheap 
server=dynamicdns.park-your-domain.com 
login=mysite.com
password=bcd90a21f335ac84ef6c3f077a5b82e8
@, sub1, sub2

The first 3 lines above need to be exactly like I have written them. For login, you will put your domain name in place of "mysite.com". The password is the Dynamic DNS Password from your Namecheap dashboard which I mentioned above. The last line has an @ for your domain and the name of the subdomains for each subdomain.

Now it might be a good idea to restart ddclient:

sam@my-pi:~ $ sudo service ddclient restart

Now(or soon), requests to your domain or subdomain should go to the correct place! So, how about putting something there for people to see?

Step 3 - Install Pelican

So, you might have chosen to do this as the first step, but I'm putting it at the bottom in case some people wanted to create their site some other way. First, you can install pelican and markdown as follows:

sam@my-pi:~ $ sudo pip install pelican markdown

Now, I'm not going to go into detail of how to set up a pelican site, but you can get detailed instructions here. Just be sure that your project folders match up with those paths you set up in the nginx sites-available configurations.

Step 4 - Profit!

Notes:

You will probably need to enable port forwarding on port 80 to your pi's local IP address. See your router's manual for how to do this.

I might add or edit this later. Let me know if you see any mistakes or if there's something I could explain better.

Comments