Installing Django on a virtual hosting
Preparation
Beget manual
,
Reg.ru manual
It will help if you are familiar with
Python 3
and
Bash
Domain
On the hosting you need to create a site and attach a domain to it. If you do not have hosting or a domain - I recommend Beget I have been using it myself for more than ten years.
Do not forget to allow FTP and SSH connections
Docker
First you need to connect to the server over SSH
(
instructions from Beget
)
This requires a username on the hosting - it can
be either an administrator or a specially created ftp account.
ssh username@website_url.com
Instead of username enter the name of the account that has access to the hosting
Instead of website_url.com - your domain or IP address
After you have logged in to the hosting - you need to activate the virtual environment
To switch to the virtual environment
Docker
you need to run the following command:
ssh localhost -p222
After that you may have to enter the account password once again.
username@localhost's password:
Welcome to Ubuntu 18.04 LTS (GNU/Linux 4.9.132-0-beget-acl x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage
Python
You can check whether python3 and pip are installed with the commands
python3 -V
pip3 -V
Python 3.7.0
pip 20.1 from /home/u/user/.local/lib/python3.7/site-packages/pip (python 3.7)
You can check where python3 is located with the command
which pyhton3
/home/u/user/.local/bin/python3
Installing a new version of Python
If Python is not installed or you need a fresher version - you can install it manually
Installing Python
To read more about installing programs in Linux with the help of configure_make_make-install read here
(docker) login@server:~ [0] $ mkdir -p .beget/tmp && cd .beget/tmp && wget ftp://sourceware.org/pub/libffi/libffi-3.2.1.tar.gz && tar -xf libffi-3.2.1.tar.gz && cd libffi-3.2.1 && ./configure --prefix=$HOME/.local && make -j $(($(nproc)/3)) && make install && mkdir ~/.local/include && cp x86_64-unknown-linux-gnu/include/ffi.h ~/.local/include/ && cp x86_64-unknown-linux-gnu/include/ffitarget.h ~/.local/include/ (docker) login@server:~ [0] $ cd ~/.beget/tmp && wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz && tar -xf Python-3.9.1.tgz && cd Python-3.9.1 && ./configure --prefix=$HOME/.local && make -j $(($(nproc)/3)) && make install
Then in the .bashrc of this account specify $PATH to the directory ~/.local/bin/ or an alias for python3,
(docker) eth1.ru@server:~ [0] $ vi .bashrc
Let us check the path to python3
eth1.ru@server:~ [0] $ which python3
/home/u/user/.local/bin/python3
This is the path we need to put in PATH. We just drop /python3
PATH=/home/a/andreyolegovichru/.local/bin/:$PATH
And restart .bashrc
source ~/.bashrc
Virtual environment
It is often useful to use a virtual environment. You can create them in different ways, for example with:
For example
python3 -m venv venv_django
source venv_django/bin/activate
Usually, when a virtual environment is activated, the name of the environment in parentheses
appears before the user name in the terminal.
For example
(venv) andrey@eth1.ru:~/user/
Inside the container you most likely already see (docker), and the environment name may
not appear.
To check whether you have switched to the environment, run
which python3
/home/u/user/venv_django/bin/python3
As you can check here - a different Python was used before entering the environment
Or go into interactive mode
python3
>>>
>>> import sys
Run two commands
>>> sys.prefix
and
>>> sys.base_prefix
They should show different results
Installing Django
First of all, I advise updating pip
python3 -m pip install --upgrade pip
Then install Django itself
python3 -m pip install django
Check the installation
python3 -m django --version
3.2.4
or
python3 -m pip list
Package Version ----------------- -------- asgiref 3.3.4 Django 3.2.4 pip 21.1.2 pytz 2021.1 setuptools 39.0.1 sqlparse 0.4.1 typing-extensions 3.10.0.0
Starting the first project in Django
We go to the directory in which the project needs to be created. For example into public_html.
We run the command
django-admin startproject Project_Name
After this command is executed successfully, a directory is created
Project_Name
NB: If you write code in
PyCharm
you need to right-click this directory and choose Mark Directory as → Sources Root
Inside Project_Name there will be the file
manage.py
and one more directory with the same name - Project_Name.
ls /home/u/user/site.ru/public_html/Project_Name
manage.py Project_Name
Inside the nested Project_Name directory there are the files
ls /home/u/user/site.ru/public_html/Project_Name/Project_Name
__init__.py
settings.py
urls.py
wsgi.py
WSGI stands for Web Server Gateway Interface.
This is the standard of interaction between a Python program running on the server side,
and the web server itself, for example Apache
Example of the file manage.py
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Project_Name.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
passenger_wsgi.py
You need to create a file in the site root passenger_wsgi.py
cd /home/u/user/site.ru
touch passenger_wsgi.py
To configure
passenger_wsgi.py
you will need the path to Django and the path to the project.
You can get the path to Django by running
python3 -c "import django; print(django.get_version()); print(django.__file__)"
3.2.4
/home/u/user/site.ru/venv_django/lib/python3.7/site-packages/django/__init__.py
From this path you need to keep
/home/u/user/site.ru/venv_django/lib/python3.7/site-packages
You can see the path to the project by going to the parent Project_Name directory (there are two Project_Name directories, the higher-level one is needed) and running pwd
pwd
/home/u/user/site.ru/public_html/Project_Name
Example of the file passenger_wsgi.py
# -*- coding: utf-8 -*-
import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.insert(0, '/home/u/user/site.ru/public_html/Project_Name')
sys.path.insert(1, '/home/u/user/site.ru/venv_django/lib/python3.7/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = 'Project_Name.settings'
application = get_wsgi_application()
Starting the application
Every project usually uses one or several applications
When all applications and configurations are combined they make up a web application.
applications + configurations = web application
We create the first application
python3 manage.py startapp first_app
A first_app directory will appear containing the following files
admin.py
apps.py
__init__.py
migrations
models.py
tests.py
views.py
settings.py
We go to the Project_Name/Project_Name directory
We edit the file
settings.py
We add the domain on which you are deploying the site to ALLOWED_HOSTS of the (project) file settings.py
ALLOWED_HOSTS = ['http://www.eth1.ru','127.0.0.1','localhost','eth1.ru','www.eth1.ru']
We need to add our new first_app application to the list of installed applications
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'first_app',
]
We start/restart the server. Read about how to do this in the next chapter.
Starting/restarting the server
If the restart is done with a script, we run (as far as I know, this is relevant for hostings Beget and dreamhost)
(docker) site@server:~/site.ru [0] $ touch tmp/restart.txt
Perhaps on your hosting the launch will be done with runserver
From the same directory that contains the manage.py script we enter the command
python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
February 06, 2019 - 20:54:38
Django version 2.1.5, using settings 'Project_Name.settings'
Starting development server at http://127.0.0.1:8000/
Article author: Andrei Olegovich