Table of Contents
Django is a Python-based framework that enables you to quickly and easily create powerful websites. This article demonstrates how to install and configure Django on a Linux shared hosting account that uses cPanel.
After completing the following procedures, you will have a functioning registered Django site on your account that:
Although we have tested and run this Django configuration on shared hosting accounts, it is not supported. You can use this configuration as a starting point for your own Django projects, but A2 Hosting cannot help you troubleshoot or debug any custom configurations.
Table of Contents
The first step is to create a Python application within cPanel that will host the Django project. To do this, follow these steps:
If you do not know how to log in to your cPanel account, please see this article.
2.In the SOFTWARE section of the cPanel home screen, click Setup Python App.
3. Under Setup new application, in the Python version list box, select 3.6.
4. In the App Directory text box, type my app.
5. n the App Domain/URI list box, select the domain you want to use and then leave the URI text box empty.
6. Click Setup. cPanel creates the application and sets up the Python environment.
7. Under Existing applications, next to Command for entering the virtual environment, copy the command. You will need this information in the following procedure.
After you create the Python application in cPanel, you are ready to do the following tasks at the command line:
The command prompt now starts with (myapp:3.6) to indicate that you are working in the map virtual environment with Python 3.6. All of the following commands in this article assume that you are working in the Python virtual environment. If you log out of your SSH session (or deactivate the virtual environment by using the deactivate command), make sure you reactivate the virtual environment before following any of the steps below.
5. cd ~ pip install django
django-admin --version
6. To create a Django project, type the following command:
7. django-admin startproject myapp ~/myapp
8. To create directories for the static project files, type the following commands:
9. mkdir -p ~/myapp/templates/static_pages
10. mkdir ~/myapp/static_files
11. mkdir ~/myapp/static_media
12. Use a text editor to open the ~/myapp/myapp/settings.py file, and then make the following changes.
Locate the ALLOWED_HOSTS line, and then modify it as follows. Replace example.com with your own domain name:
ALLOWED_HOSTS = ['example.com']
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "static_media")
13. Use a text editor to open the ~/myapp/myapp/urls.py file. Delete all of the existing text, and then copy the following text into the file:
14.from django.contrib import admin
15. from django.urls import path, include
16. from django.conf import settings
17. from django.conf.urls.static import static
18.from django.conf.urls import url
19. from django.views.generic.base import TemplateView
20. urlpatterns = [
21. path(‘admin/’, admin.site.urls),
22. url(r’^$’, TemplateView.as_view(template_name=’static_pages/index.html’), name=’home’),
23. ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
24. urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
25. Use a text editor to open the ~/myapp/passenger_wsgi.py file, and then make the following changes. Replace usernamewith your own account username:
26. import myapp.wsgi
27. SCRIPT_NAME = ‘/home/username/myapp’
28. class PassengerPathInfoFix(object):
29. “””
30. Sets PATH_INFO from REQUEST_URI because Passenger doesn’t provide it.
31. “””
32. def __init__(self, app):
33. self.app = app
34. def __call__(self, environ, start_response):
35. from urllib.parse import unquote
36. environ[‘SCRIPT_NAME’] = SCRIPT_NAME
37. request_uri = unquote(environ[‘REQUEST_URI’])
38. script_name = unquote(environ.get(‘SCRIPT_NAME’, ”))
39. offset = request_uri.startswith(script_name) and len(environ[‘SCRIPT_NAME’]) or 0
40. environ[‘PATH_INFO’] = request_uri[offset:].split(‘?’, 1)[0]
41. return self.app(environ, start_response)
42. application = myapp.wsgi.application
43. application = PassengerPathInfoFix(application)
44. Use a text editor to create a basic index.html file in the ~/myapp/templates/static_pagesdirectory. The file can be as simple as a text file that says Hello world.
45. Type the following command:
python ~/myapp/manage.py migrate
46. Set up the superuser account
python ~/myapp/manage.py createsuperuser
47. Type the following command to collect the static files:
python ~/myapp/manage.py collectstatic
If you are asked if you want to overwrite existing files, type yes and then press Enter.
48. In cPanel, restart the Python application:
If you do not know how to log in to your cPanel account, please see this article.
49. Test the Django site:
If the web site does not appear in your browser, try running the passenger_wsgi.py file manually. To do this, type the following command:
python ~/myapp/passenger_wsgi.py
There should not be any text output to the console when you run this file. If there are any errors, check the syntax in the configuration files.
Visit: Hostripples!
Due to growing digitalization, Email Communication has become the backbone of professional interactions. Yet, surprisingly, many professionals struggle to craft…
As the digital landscape continues to evolve, securing your website has never been more crucial. SSL, or Secure Sockets Layer,…
As a web designer and web developer your experience must have reached to new height, right? Further, you need to…
In today's digital landscape, timing is everything. Whether you're a social media manager, business owner, or content creator, the success…
Are you a website owner? Maintaining the website is the prime concern for any website owner. Yes, it’s equally important…