Wednesday, April 29, 2020

Setting up Django Project and App on NameCheap Shared Hosting using cPanel

It isn't news anymore that many shared host accounts best know for hosting PHP web frameworks now support python web frameworks.

In this post, I will show you how to deploy python django web framework on "NameCheap Shared Hosting" using cPanel.

Django Project is a collection of Apps. A project most have at least one app.

Note: you can deploy django on any cpanel account that:-
1) Supports python apps and 
2) Provides access to 'Terminal/Command-Line'


Step 1: 
I am going to use an existing namecheap account (domain and hosting). If you don’t have account yet, signup for one.

Step 2: 
Login to you cpanel and create a 'subdomain'. Mine is at: https://django1.umaryusuf.com as seen below





At this point, when you load the URL it should display this…



Step 3: Setup Python App
Click on the python icon that says “Setup Python App” (it should be under ‘Software’ tab as seen below).

Click create button that says “Create Application”


Fill the form as seen below… and click on ‘Create’ button.



You should see a screen that look like this below, with a command to enter the virtual environment. Copy the path (source /home/xxx/virtualenv/django1/3.7/bin/activate && cd /home/xxx/django1), you will use it in the next step.


If you checked your “File Manager”, you should see these two folders created from the names you entered above.





You will find manage.py and passenger_wsgi.py files in the “Application root” folder.


At this point, when you load the URL it should display this… (different from what you saw in step 2) that is fine. It confirms that our python app was applied and we just need to install Django.


Step 4: pip install django
Open the 'Terminal/Command-Line' and install Django. Note that you could use SSH instead that is if your cpanel hosting provider supports it.
If you don’t have SSH, don’t worry let’s use the online 'Terminal/Command-Line' as seen below…




When the terminal is opened, right click and paste the command path you copied from the step above. This should navigate your terminal to python app directory.
Now, install django by running: pip install django==2.1



Let’s check the version of django installed, enter: django-admin --version



Note:

  • 1) At the moment Django version 3 requires PostgreSQL database to work on namecheap shared hosting, so use django==2.1
  • 2) You can install other modules for your project from here. Example: pip install pandas



Step 5: Create a new project
Yes, even though we have installed Django we still need to create django project and apps for our URL to display what we want. So, let’s start by creating a django project.

Run this command: django-admin startproject myproject ~/django1
Replace 'myproject' with your application name and 'django1' with whatever name you entered in 'step 3' under "Application root".



Step 6: 
Now using ‘File Manager’ in CPanel (or FTP), open the python app folder and edit the file: passenger_wsgi.py



Delete everything in the file and add this line...
from myproject.wsgi import application



Remember to replace "myproject" with your django project name. Click on 'Save Changes' to save the changes to the file.


Step 7: 
Next open the django project folder and edit “settings.py”.
Change this line: ALLOWED_HOSTS = [] to ALLOWED_HOSTS = ['django1.umaryusuf.com']
Replace my sub domain with whatever yours is.





Step 8: 
Go to you python app from ‘step 3’ and restart the app.




Step 9: 
Now your URL should load the default django welcome page.



From this point, create your apps (python manage.py startapp my_app_name), do migrations and configure as usual.



Related links

No comments:

Post a Comment