1. Login into your cPanel account and enter Terminal in the Advanced section.
2. Activate the virtual environment, using the command from earlier created Python project.
The command prompt now starts with (myapp:3.8) to indicate that you are working in the myapp virtual environment with Python version 3.8. 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.
3. To install Django, type the following commands:
cd ~ pip install django==2.1.8
4. To create a Django project, type the following command:
django-admin startproject myapp ~/myapp
5. To create directories for the static project files, type the following commands:
mkdir -p ~/myapp/templates/static_pages mkdir ~/myapp/static_files mkdir ~/myapp/static_media
6. Enter ~/myapp/myapp/settings.py file with vi or another text editor, and then make the following changes:
- Find the ALLOWED_HOSTS line, and then modify it: Enter your own domain name into brackets.
ALLOWED_HOSTS = ['yourwebsite.tld']
- Find the TEMPLATES block, and then modify it as follows:
'DIRS': [os.path.join(BASE_DIR,'templates')],
- Find the STATIC_URL line, and then add the following lines beneath it:
STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, "static_media")
7. 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:
from django.contrib import admin from django.urls import path, include
from django.conf import settings from django.conf.urls.static import static
from django.conf.urls import url from django.views.generic.base import TemplateView
urlpatterns = [ path('admin/', admin.site.urls),
url(r'^$', TemplateView.as_view(template_name='static_pages/index.html'), name='home'), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
8. Use a text editor to open the ~/myapp/passenger_wsgi.py file. Delete all of the existing text, and then copy the following text into the file:
import os import sys
import django.core.handlers.wsgi from django.core.wsgi import get_wsgi_application
# Set up paths and environment variables sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings' # Set script name for the PATH_INFO fix below
SCRIPT_NAME = os.getcwd() class PassengerPathInfoFix(object):
""" Sets PATH_INFO from REQUEST_URI because Passenger doesn't provide it.
""" def __init__(self, app):
self.app = app def __call__(self, environ, start_response):
from urllib.parse import unquote environ['SCRIPT_NAME'] = SCRIPT_NAME
request_uri = unquote(environ['REQUEST_URI']) script_name = unquote(environ.get('SCRIPT_NAME', ''))
offset = request_uri.startswith(script_name) and len(environ['SCRIPT_NAME']) or 0 environ['PATH_INFO'] = request_uri[offset:].split('?', 1)[0]
return self.app(environ, start_response) # Set the application application = get_wsgi_application() application = PassengerPathInfoFix(application)
9. Use a text editor to create a basic index.html file in the ~/myapp/templates/static_pages directory.
10. Type the following command, to create the database:
python ~/myapp/manage.py migrate
11. In order to set up the superuser account, type the following command into the terminal:
python ~/myapp/manage.py createsuperuser
At the Username prompt, type the administrator username, and then press Enter.
At the Email address prompt, type the administrator e-mail address, and then press Enter.
At the Password prompt, type the administrator password, and then press Enter.
12. Type the following command to collect the static files:
python ~/myapp/manage.py collectstatic
13. Now you must restart the Python application:
In the SOFTWARE section of the cPanel home screen, click Setup Python App.
Under WEB APPLICATIONS, find your application, and then click the Restart icon.
After finishing all steps above, you can test your website and continue editing it.
Add Comment