Categories
Resources Tutorial

CakePHP and Namecheap Hosting

You’ve created an amazing CakePHP app that runs perfectly on your local machine. All your friends think it’s quite possibly the coolest thing that’s ever existed, but the rest of the world will never know about it because you have no idea how to publish a CakePHP app to the world wide web. Well, this guide will take you straight through how to publish that amazing piece of art for cheap!

(This post contains affiliate links)

Why Namecheap?

Namecheap has been my go-to hosting, and domain buying service for years now! They have amazing 24/7 support that has helped me get out of a jam more than once. They have a huge list of domains including the recently released (September 2020 to be exact) .GAY domain extension! Not only is their selection incredibly broad, the domains are also affordable! For example, you can get a .xyz domain for only a $1! Most importantly for our purposes Namecheap’s hosting allows you to install CakePHP without jumping through any hoops. If you’re looking for high quality web hosting, and domain purchasing for cheap then I highly recommend Namecheap.

“But Chris!” I hear you say. “How do I publish my amazing band-name generator to the web so Google will finally hire me!?!1!” Well let’s look at the next section to see how to setup CakePHP on Namecheap

How to Setup CakePHP on Namecheap

What You’ll Need

In order to setup your CakePHP application on Namecheap hosting you’ll only need 2 things:

  1. A domain to host your website at.
  2. A hosting plan through Namecheap

You can actually get a free domain when you sign up for a Namecheap hosting plan!

Once you have those two things you can follow the steps below!

Installing CakePHP

Navigate to your dashboard on Namecheap where you should see a list of domains you own through Namecheap.

As you can see I own a few domains through the service including this very blog!

While we’re in the dashboard we can make sure that our domain is actually on our server by setting our nameserver to Namecheap Web Hosting DNS

After you do that click on the arrow next to the Manage button for your hosting option, and navigate to cPanel.

Your browser should open a new window to the cPanel. If you’re unfamiliar with the cPanel it is a GUI through which an end user can perform tasks involved with hosting a website like creating an email, altering DNS record, or even installing PHP frameworks!

Now go ahead and click on Softaculous Apps Installer, and search CakePHP. You should find the most current stable version of CakePHP just waiting to be installed!

Once you install the app you’ll need to actually put your code onto the server.

Getting Your CakePHP Application on the Server

There are two main ways to get your application onto the Namecheap server:

  1. Clone a repo from GitHub
  2. Upload files using FTP

1. Using GitHub

If you want to clone a repo from GitHub you’ll first have to enable ssh access by using the Manage Shell function.

Then enabling ssh access:

From there navigate to the Git Version Control button on the cPanel and press the create button.

Then you’ll have to fill in your GitHub repos information in the form.

2. Using FTP

Using FTP does not require you to enable SSH for your account.

All you’ll need to do to upload your files to your server is navigate to the FTP Accounts button, and get the information for the already existent account.

You can use your favorite FTP tool to login to your server and upload files to your hearts content!

Now that our code is on the server let’s change our document root to point at your CakePHP applications webroot.

Changing Our Website’s Document Root to CakePHP’s webroot(primary domain)

Since we shouldn’t expose our application’s internals to the public web we’ll have to change the document root! Since the domain you registered is probably the primary domain for the server you’ll have to enable ssh access in order to change the document root.

Once you enabel ssh access login to the server via ssh, and perform the following commands:

#open cpanel userdata
vim /var/cpanel/userdata/{USERNAME}/{DOMAIN.COM}
#Alter the document root from:
#documentroot: /home/USERNAME/public_html
#to:
documentroot: /home/{USERNAME}/{APP_DIR_NAME}/webroot
#Rebuild the Apache conf and restart Apache:
/scripts/rebuildhttpdconf
service httpd restart

The changes should be immediate!

If you’re having issues doing this I highly recommend contacting Namecheap’s support.

Changing Our Website’s Document Root (non-primary domain)

Changing a non-primary domain document root is much easier! All you’ll have to do is navigate to the Domains function.

Then click manage next to the domain who’s document root you want to alter.

After that you should be able to easily type in the document root you want!

Conclusion

If you want to learn more about properly deploying a CakePHP application to the web you can read about it at CakePHP’s official documentation. They also cover different ways to update your document root.

What do you think of the post? If you liked it, or have some suggestions then please comment below!

You can also check out my other tutorials like the one on how to stream video with CakePHP, or blocking disposable emails.

Also don’t forget to subscribe for new CakePHP goodness every Tuesday and Thursday 🍰😋