GNU/Linux >> Linux Esercitazione >  >> Ubuntu

Installare Django con PostgreSQL e Nginx su Ubuntu 16.04

Django è un framework web Python open source, veloce e basato sul pattern MVT. In questa guida installiamo Django con PostgreSQL, Gunicorn, Supervisor e Nginx come reverse proxy.

Requisiti

  • Ubuntu 16.04
  • Privilegi root

1. Installare Django, Virtualenv e Gunicorn

sudo apt-get update
sudo apt-get install python3-pip python3-dev
ln -s /usr/bin/pip3 /usr/bin/pip
pip install --upgrade pip
pip install django virtualenv gunicorn

2. Installare e configurare PostgreSQL

sudo apt-get install postgresql postgresql-contrib libpq-dev
pip install psycopg2
su - postgres
psql

Crea utente e database:

\password postgres
CREATE USER python_dev WITH PASSWORD 'password';
CREATE DATABASE django_db OWNER python_dev;
\list

3. Creare il progetto Django

useradd -m -s /bin/bash natsume
passwd natsume
su - natsume
mkdir myproject
virtualenv --python=python3 myproject/
cd myproject/
source bin/activate
pip install django gunicorn psycopg2
django-admin startproject hello_django
cd hello_django/
vim hello_django/settings.py

In settings.py configura PostgreSQL:

'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django_db',
'USER': 'python_dev',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',

Aggiungi anche:

STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

Prepara il database:

python manage.py migrate
python manage.py createsuperuser
python manage.py collectstatic
python manage.py runserver 0.0.0.0:8080

4. Configurare Gunicorn

Crea bin/gunicorn_start:

#!/bin/bash
NAME="hello_django"
DJANGODIR=/home/natsume/myproject/hello_django
SOCKFILE=/home/natsume/myproject/hello_django/run/gunicorn.sock
USER=natsume
GROUP=natsume
NUM_WORKERS=3
DJANGO_SETTINGS_MODULE=hello_django.settings
DJANGO_WSGI_MODULE=hello_django.wsgi
cd $DJANGODIR
source ../bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
exec ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-
chmod u+x bin/gunicorn_start

5. Installare Supervisor

sudo apt-get install supervisor
cd /etc/supervisor/conf.d/
vim hello-django.conf

Configurazione:

[program:hello_django]
command = sh /home/natsume/myproject/bin/gunicorn_start
user = natsume
stdout_logfile = /home/natsume/myproject/logs/gunicorn_supervisor.log
redirect_stderr = true
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8

Crea cartelle e avvia:

su - natsume
mkdir -p myproject/hello_django/run/
mkdir -p myproject/logs/
touch myproject/logs/gunicorn_supervisor.log
exit
systemctl start supervisor
supervisorctl

6. Configurare Nginx

sudo apt-get install nginx
cd /etc/nginx/sites-available/
vim hello_django

Esempio di virtual host:

upstream hello_django_project {
    server unix:/home/natsume/myproject/hello_django/run/gunicorn.sock fail_timeout=0;
}
server {
    listen 80;
    server_name www.django-nginx.com;
    client_max_body_size 4G;
    access_log /home/natsume/myproject/logs/nginx-access.log;
    error_log /home/natsume/myproject/logs/nginx-error.log;
    location /static/ {
        alias /home/natsume/myproject/hello_django/static/;
    }
    location /media/ {
        alias /home/natsume/myproject/hello_django/media/;
    }
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass https://hello_django_project;
    }
}

Attiva e riavvia:

ln -s /etc/nginx/sites-available/hello_django /etc/nginx/sites-enabled/
nginx -t
systemctl restart nginx

7. Verifica

Visita https://www.django-nginx.com e https://www.django-nginx.com/admin/. Controlla i servizi con:

netstat -pl

Installazione completata.

Ubuntu
  1. Chat di team self-hosted: Mattermost con PostgreSQL e Nginx su Ubuntu 16.04

  2. MediaWiki su Nginx e Ubuntu 16.04: installazione con SSL gratuito

  3. Installare Magento 2 con Nginx e Let's Encrypt su Ubuntu 18.04

  4. Installare PostgreSQL e phpPgAdmin su Ubuntu 15.04

  5. Installare Nginx con PHP5 e MySQL su Ubuntu 11.10

Come installare Redmine 3.2 con Nginx su Ubuntu 16.04

Installare Osclass Classifieds CMS con Nginx e Let's Encrypt su Ubuntu 20.04

Installare Nginx con ModSecurity su Ubuntu 15.04

Come installare Nextcloud 13 su Ubuntu 16.04 con Nginx

Come installare e configurare PostgreSQL su Ubuntu

Installare e configurare Nginx su Ubuntu 20.04 e 21.04