How to use GitHub Pages to host your website, even with multiple repos
Tags: github, tutorial, webdev
Published: 2021-08-12 02:53:54

Have you ever tried to host a website and you're blocked on where, or how to post it? There's lots of hosting sites these days, and GitHub is one of them.

[GitHub Pages](https://pages.github.com/) is a feature that allows you to host your website direct from your repo. Let's see how to do this.

## Step 1. Make sure your code is on GitHub

Since GitHub Pages runs from a *.html file in your GitHub repo, you need to make sure your code is actually on GitHub.

If you haven't uploaded your code yet, follow these steps:

1. Go to your profile: github.com/YOUR_USERNAME
2. Click the "Repositories" tab
![Repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2y0ovyag0lavppq9hawv.JPG)
3. Click "New"
![New](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2kfx3dfcvb8eed20qdg.JPG)
4. Give your repository (repo) a name 
![Fill out form](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l78ygsou6o7xjz8oapt7.JPG)
5. Click private if you don't want people to see your code. Even if your repo is private, the website that goes live will be seen by everyone
![Public or private](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1xrfzsrqhh6k6dvzc4j.JPG)
6. Add any of the options you like
![Tick boxes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rd59td0zhpuahx5w251z.JPG)
7. Click "Create repository"
![Create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4yk1w1n3c8o8j8kbetx.JPG)

Now you should have a repo, and you can upload your *.html file by clicking on the "Code" tab and then clicking "Add file".

## Step 2. Enable GitHub Pages

Now that your *.html file is in your GitHub repo, you can enable GitHub Pages. Make sure you are in the repo with your *.html file and then follow these steps:

1. Go to the "Settings" tab (note this is the settings tab that is shown next to the "Insights" tab on your repo. This <u>is not</u> your personal GitHub settings
![Settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngmx6kjk3yabl0pg0y0f.JPG)
2. Scroll down until you find "Pages" on the left
![Pages](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8q1ausofz2fsmqll2bt2.JPG)
3. Click "Pages"
4. Click the "Source" dropdown and choose which branch to draw your *.html file from
![source](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0oy0lqxze3rye3icu19w.JPG)
5. Click "save"

Now your website is live! It should be available at:
`YOUR_USERNAME.github.io`

If you ever want to "unpublish" your GitHub Page, click the "Source" dropdown in Step 4 above and choose "none". Now your page is unpublished.

## Step 3. Customising your website

Now that your website (or GitHub Page) is live, you can make some customisations. Also read on if you have more than one repo you want as a GitHub Page. Ie. you can't have the same website address for two sites.

### Website themes

After you've completed Step 2. above, you'll have more options under the "Pages" settings. You can click "Choose a theme" to add a fancy look (CSS) to your website.

![Choose theme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8n7zl6cg8ox77fc3aaj.JPG)

This will take you to a site where you can choose a template to pretty up your website. If you only have a *.html file, a template will make your site look amazing. Otherwise you can write some CSS for your website. We won't go into that here.

### Custom domain

Your website is available at the URL GitHub provided, but you can also choose a custom domain. This is a domain you own, and must be purchased through a domain name provider, such as [Namecheap](https://www.namecheap.com/).

Once you have purchased a domain, add your domain name under the "Pages" settings, for example `https://YOUR_NAME.info`. This will direct the GitHub Pages link to this custom domain and your website will appear at your nominated address.

![custom domain](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fim28stxf3l1lcc3l3tl.JPG)

In order for this to work properly, you'll need to setup a few things on your domain name provider's end. For information on how to set those up [read the GitHub Docs](https://docs.github.com/en/pages/configuring-a-custom-domain-for-your-github-pages-site).

The TL;DR is you'll need to add these A records:
```
185.199.108.153
185.199.109.153
185.199.110.153
185.199.111.153
```

And you'll need to add a CNAME record with a value that is the URL of your original GitHub Page, for example:

![CNAME record](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oll2bt43hwlbooqq9p0v.JPG)

This is how mine looks in [Namecheap](https://www.namecheap.com/). Yours' might look slightly different depending on your domain name provider. 

If you add a custom domain to your website, you'll have the option to turn off "Enforce HTTPS". Usually, you'll want to make sure this is ticked to ensure your website is secure. Only untick this if you're using a different method to secure your website.

![Enforce](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v8gvtbdnu3bopp87wgb7.JPG)
 
### What if you have multiple repos?

GitHub Pages are published at the URL: `YOUR_USERNAME.github.io`. So if this link takes your username, what happens if you have multiple repos under your GitHub username? Won't these conflict? Well, GitHub solves this by publishing subsequent repos at the URL:

`YOUR_USERNAME.github.io/YOUR_REPO_NAME`

All you need to do is ensure the CNAME for each custom domain is pointing towards `YOUR_USERNAME.github.io` and you have the A records added above. GitHub will do the rest for you. After you add a custom domain, you might need to wait a bit until your custom URL will work. Just be patient while the DNS is flushed through. I may have learned this the hard way!

## Enjoy your shiny new website :sparkles:

Once your domain name is properly routed through, your new website will be available at your nominated domain name, or the default `github.io` address.

This is one of the easiest ways to host a website. Read more about GitHub Pages on the  [GitHub Pages](https://pages.github.com/) site. Let me know if you want to see more information on this topic, or what other tutorials you'd like to see.