DEV Community

Cover image for GitHub Pages: A Beginner's Guide to Deploying Your Website on a Custom Domain ๐Ÿช„

GitHub Pages: A Beginner's Guide to Deploying Your Website on a Custom Domain ๐Ÿช„

Hey devs, itโ€™s time to elevate your web presence with GitHub Pages! This guide will walk you through deploying your project to a custom domain in no time.


First Things First: Initializing gh-pages

Before we dive into the exciting world of GitHub Pages, make sure you've already created the gh-pages branch. Itโ€™s simpler than it sounds!


Just follow these commands:

git checkout --orphan gh-pages
git reset --hard
git commit --allow-empty -m "Initializing gh-pages branch"
git push origin gh-pages
git checkout main
Enter fullscreen mode Exit fullscreen mode

Great! Now youโ€™ve set up your GitHub Pages branch and are ready to move forward.


Hostinger & GitHub: Web Setup Made Easy! ๐Ÿ“œ


Note: This guide is tailored for using a domain purchased on Hostinger for your GitHub Pages project. If youโ€™re using a different domain service, the process might have some variations, but the core steps will be similar.


When deploying your project with GitHub Pages, the deployed page's URL usually looks like "yourname.github.io/projectname," based on your GitHub username and repository name.

However, if you use a repository named "yourusername.github.io," you can host your project directly under your custom domain without needing a project name in the URL.


Step 1: Accessing Hostinger Domain DNS Settings ๐Ÿ—๏ธ

To get started, visit your Hostinger domain DNS settings at Hostinger DNS (replace yourdomain.com with your actual domain). This is where youโ€™ll configure your DNS records.


Step 2: Removing Old Records ๐Ÿ—๏ธ

In your domain's DNS records, look for an existing CNAME record. Youโ€™ll need to delete this record to prepare for your new GitHub Pages setup. Also, remove any default A type records.


Step 3: Adding New CNAME Records

Now, youโ€™ll want to add two CNAME records:

The first will point www.yourdomain.com to .github.io.

The second will point yourdomain.com to .github.io.


CNAME Record Setup

  1. Record 1: CNAME
    • Type: CNAME
    • Name: www
    • Points to: .github.io (TTL can be left as default)

CNAME

  1. Record 2: ALIAS
    • For APEX domains (when someone types yourdomain.com), create another CNAME with:
    • Name: @
    • Points to: .github.io

ALIAS


Step 4: Adding GitHub Pages IP Addresses ๐Ÿ—๏ธ

To ensure your GitHub Pages site is accessible, youโ€™ll need to add the following IP addresses to your DNS records:

185.199.108.153
185.199.109.153
185.199.110.153
185.199.111.153
Enter fullscreen mode Exit fullscreen mode

Step 5: Connecting Your Custom Domain to GitHub Pages ๐Ÿ—๏ธ

Weโ€™re almost finished! The final step is to add your custom domain in GitHub Pages. Go to your GitHub repository, open the Settings tab, and then the Pages tab. Enter your custom domain in the Custom domain field.


Configuring Your Custom Domain...

DNS Check Successful

Custom Domain Setup Successful!


Note: If youโ€™re using a static site generator, youโ€™ll need to manually add your custom domain to the public folder. Create a file named public/CNAME (with capital letters and no extensions) containing your domain:

yourdomain.com
Enter fullscreen mode Exit fullscreen mode

The Finale: Access Your Website ๐Ÿ”ฎ

Once youโ€™ve completed these steps, your website will be live! Visitors can explore your creations by visiting https://yourdomain.com. How exciting is that?


Troubleshooting: Common Issues

If you encounter any issues with your custom domain, here are a few solutions that might help:


Solution 1: Create docs/CNAME ๐Ÿช„

In the root of your project, create a folder named docs. Inside the "docs" folder, create a file named CNAME (no additional extensions).

docs/CNAME


Solution 2: Create index.html ๐Ÿช„

As an extra measure, create an empty file named index.html in the root folder (itโ€™s fine if you already have one; just add a new one).

index.html


These solutions are particularly useful for deploying a React app to GitHub Pages, but they can also be applied to other projects.

Now go forth, beginner developers, and let your websites shine brightly on the web! Happy coding! ๐ŸŒŸโœจ


For further information, visit the GitHub documentation for using a custom domain with GitHub Pages!


Conclusion ๐Ÿ•ฏ๏ธ

Congratulations on completing this journey of hosting and deploying with Hostinger & GitHub Pages! ๐ŸŽ‰โœจ You've successfully set up your GitHub Pages site to share your creations with the world under your custom domain.


As you continue your coding adventures, remember that hosting and deploying are just the beginning of an exciting path in web development. Embrace the joy of learning, exploration, and the camaraderie of the coding community.

So, go ahead, and let your websites spread your creativity across the web. Until next time, happy coding, and may your projects flourish! ๐Ÿš€๐ŸŒโœจ


I hope this guide has been helpful in your development journey. If you have any questions or would like to connect, feel free to reach out or comment below!

Instagram Twitter Discord

ส•ใฃโ€ข แดฅ โ€ข ส”ใฃ Thanks for reading!


Love,

NESSA KODO

Top comments (0)