40
loading...
This website collects cookies to deliver better user experience
apt-get install python3-pip python3-dev libpq-dev curl nginx -y
systemctl start nginx
systemctl enable nginx
apt-get install postgresql postgresql-contrib -y
su - postgres
psql
CREATE DATABASE
djangodb;
CREATE USER
djangouser
WITH PASSWORD 'password';
ALTER ROLE djangouser
SET client_encoding
TO 'utf8';
ALTER ROLE djangouser
SET default_transaction_isolation
TO 'read committed';
ALTER ROLE djangouser
SET timezone
TO 'UTC';
GRANT ALL PRIVILEGES
ON DATABASE djangodb
TO djangouser;
\qexit
pip3 install --upgrade pip
pip3 install virtualenv
mkdir ~/django_project
cd ~/django_project
virtualenv djangoenv
source djangoenv/bin/activate
pip install django gunicorn psycopg2-binary
django-admin.py startproject django_project ~/django_project
nano ~/django_project/django_project/settings.py
ALLOWED_HOSTS = ['django.example.com', 'localhost']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'djangodb',
'USER': 'djangouser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
STATIC_URL = '/static/'
import os
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
./manage.py makemigrations
./manage.py migrate
./manage.py createsuperuser
Username (leave blank to use 'root'): admin
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.
./manage.py collectstatic
./manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
June 22, 2021 - 11:15:57
Django version 3.2.4, using settings 'django_project.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
gunicorn --bind 0.0.0.0:8000 django_project.wsgi
[2021-06-22 11:20:02 +0000] [11820] [INFO] Starting gunicorn 20.1.0
[2021-06-22 11:20:02 +0000] [11820] [INFO] Listening at: http://0.0.0.0:8000 (11820)
[2021-06-22 11:20:02 +0000] [11820] [INFO] Using worker: sync
[2021-06-22 11:20:02 +0000] [11822] [INFO] Booting worker with pid: 11822
deactivate
systemd
service file for the Gunicorn to start and stop the Django application server.nano /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
nano /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/root/django_project
ExecStart=/root/django_project/djangoenv/bin/gunicorn --access-logfile - --workers 3 --bind
unix:/run/gunicorn.sock django_project.wsgi:application
[Install]
WantedBy=multi-user.target
chown -R www-data:root ~/django_project
systemd
daemon with the following command:systemctl daemon-reload
systemctl start gunicorn.socket
systemctl enable gunicorn.socket
systemctl status gunicorn.socket
● gunicorn.socket - gunicorn socket
Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
Active: active (running) since Tue 2021-06-22 12:05:05 UTC; 3min 7s ago Triggers: ● gunicorn.service
Listen: /run/gunicorn.sock (Stream)
CGroup: /system.slice/gunicorn.socket
Jun 22 12:05:05 django systemd[1]: Listening on gunicorn socket.
nano /etc/nginx/conf.d/django.conf
server {
listen 80;
server_name django.example.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/django_project;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl restart nginx