GNU/Linux >> Tutoriels Linux >  >> Cent OS

Comment installer Django Python Framework sur CentOS 8

Django est un framework Web gratuit, open-source et de haut niveau utilisé pour développer des applications Web Python. Il est livré avec un ensemble d'outils qui vous aident à créer des applications Web sécurisées et évolutives. Son objectif principal est de faciliter la création d'applications complexes et prend soin de la structure interne.

Dans ce didacticiel, nous allons apprendre à installer Django et à configurer Nginx en tant que proxy inverse pour Django sur CentOS 8.

Prérequis

  • Un serveur exécutant CentOS 8.
  • Un mot de passe root est configuré sur votre serveur.

Installer les packages requis

Django est un framework basé sur Python, vous devrez donc installer Python et PIP sur votre système. Vous pouvez les installer en exécutant la commande suivante :

dnf installer python36 python3-pip -y

Une fois les deux packages installés, vous pouvez passer à l'étape suivante.

Installer Django

Vous pouvez installer Django avec la commande PIP comme indiqué ci-dessous :

pip3 installer Django

Après avoir installé Django, vous pouvez vérifier la version de Django avec la commande suivante :

django-admin --version

Vous devriez voir la version de Django dans la sortie suivante :

3.0.3

Créer un projet Django

À ce stade, Django est installé. Il est maintenant temps de créer une nouvelle application Django.

Vous pouvez créer une nouvelle application Django en utilisant la commande django-admin dans le répertoire /opt comme indiqué ci-dessous :

cd /opt
django-admin startproject djangoproject

Une fois le projet django créé, changez le répertoire en djangoproject et migrez les changements avec la commande suivante :

cd djangoproject
python3 manage.py migrate

Vous devriez obtenir le résultat suivant :

Opérations à effectuer :Appliquer toutes les migrations :admin, auth, contenttypes, sessionsExécution des migrations :Appliquer contenttypes.0001_initial... OK Appliquer auth.0001_initial... OK Appliquer admin.0001_initial... OK Appliquer admin.0002_logentry_remove_auto_add.. . OK Application admin.0003_logentry_add_action_flag_choices... OK Application contenttypes.0002_remove_content_type_name... OK Application auth.0002_alter_permission_name_max_length... OK Application auth.0003_alter_user_email_max_length... OK Application auth.0004_alter_user_username_opts... OK Application auth.0005_alter_user_username_opts... OK Application auth.0005_alter_user_username_opts... OK Application auth.0005_alter_user_email_max_length... Application auth.0006_require_contenttypes_0002... OK Application auth.0007_alter_validators_add_error_messages... OK Application auth.0008_alter_user_username_max_length... OK Application auth.0009_alter_user_last_name_max_length... OK Application auth.0010_alter_group_name_max_length... OK Application auth.001_up_proxy sessions... OK Application auth.001_up_proxy sessions... .0001_initial... D'accord

Ensuite, vous devrez créer un compte utilisateur administrateur pour gérer le projet Django. Vous pouvez le créer avec la commande suivante :

python3 manage.py createsuperuser

Il vous sera demandé de fournir votre nom d'utilisateur, email et mot de passe. Vous pouvez les fournir selon votre choix comme indiqué ci-dessous :

Nom d'utilisateur (laissez vide pour utiliser 'root') :dadminAdresse e-mail :[email protected]Mot de passe :Mot de passe (encore) :Superutilisateur créé avec succès.

Une fois que vous avez terminé, vous pouvez passer à l'étape suivante.

Démarrer l'application Django

Par défaut, l'application Django n'est pas accessible depuis les hôtes distants. Vous devrez donc autoriser Django pour les hôtes externes. Vous pouvez le faire en ajoutant l'IP de votre serveur dans settings.py :

nano /opt/djangoproject/djangoproject/settings.py

Modifiez la ligne suivante :

ALLOWED_HOSTS =['votre-serveur-ip']

Enregistrez et fermez le fichier. Ensuite, démarrez l'application Django avec la commande suivante :

cd /opt/djangoproject
python3 manage.py runserver 0.0.0.0:8000

Vous devriez voir le résultat suivant :

Surveillance des modifications de fichiers avec StatReloaderExécution des vérifications du système...La vérification du système n'a identifié aucun problème (0 silencieux). http://0.0.0.0:8000/Quitter le serveur avec CONTROL-C. L'application Django est maintenant lancée et tourne sur le port 8000. 

À ce stade, l'application Django est maintenant démarrée et s'exécute sur le port 8000. Vous pouvez maintenant passer à l'étape suivante.

Configurer SELinux et le pare-feu

Ensuite, vous devrez autoriser les ports 8000 et 80 via firewalld. Vous pouvez les autoriser avec la commande suivante :

firewall-cmd --permanent --add-port=8000/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

Ensuite, configurez SELinux avec la commande suivante :

setsebool httpd_can_network_connect sur -P

Une fois que vous avez terminé, vous pouvez passer à l'étape suivante.

Accéder à l'application Django

Vous pouvez accéder à l'application Django en visitant l'URL http://your-server-ip:8000. Vous devriez voir la page suivante :

Vous pouvez également accéder à l'interface d'administration de Django en utilisant l'URL http://your-server-ip:8000/admin. Vous devriez voir la page suivante :

Indiquez votre nom d'utilisateur et votre mot de passe d'administrateur et cliquez sur Journal dans bouton. Vous devriez voir la page suivante :

Installer Nginx et Gunicorn

Dans cette section, nous allons installer Gunicorn pour créer et gérer le service Django, et Nginx pour servir l'application Django.

Tout d'abord, installez le Nginx avec la commande suivante :

dnf installer nginx -y

Ensuite, installez Gunicorn en utilisant la commande PIP comme indiqué ci-dessous :

pip3 installer gunicorn

Une fois les deux packages installés, démarrez le service Nginx et autorisez-le à démarrer après le redémarrage du système avec la commande suivante :

systemctl démarrer nginx
systemctl activer nginx

Ensuite, changez la propriété du répertoire /opt/djangoproject en Nginx comme indiqué ci-dessous :

chown -R nginx:nginx /opt/djangoproject

Créer un fichier de service Systemd pour Django

Ensuite, créez un fichier de service systemd pour gérer le service Django avec la commande suivante :

nano /etc/systemd/system/django.service

Ajoutez les lignes suivantes :

[Unit]Description=django daemonAfter=network.target[Service]User=nginxGroup=nginxWorkingDirectory=/opt/djangoprojectExecStart=/usr/local/bin/gunicorn --workers 3 --bind unix:/opt/djangoproject/ djangoproject.sock djangoproject.wsgi:application[Install]WantedBy=multi-user.target

Enregistrez et fermez le fichier puis rechargez le démon systemd avec la commande suivante :

rechargement du démon systemctl

Ensuite, démarrez le service Django et activez-le après le redémarrage du système avec la commande suivante :

systemctl démarrer django
systemctl activer django

Vous pouvez maintenant vérifier l'état du service Django avec la commande suivante :

statut systemctl django

Vous devriez voir le résultat suivant :

 ? django.service - django daemon Chargé :chargé (/etc/systemd/system/django.service ; désactivé ; préréglage fournisseur :désactivé) Actif :actif (en cours d'exécution) depuis le lundi 2020-03-02 22:27:51 EST ; il y a 3min 32s PID principal :960 (django) Tâches :4 (limite :25028) Mémoire :95,2 Mo CGroup :/system.slice/django.service ??960 /usr/bin/python3.6 /usr/local/bin/ gunicorn --workers 3 --bind unix:/opt/djangoproject/djangoproject.sock djangoproject.wsgi:a> ??964 /usr/bin/python3.6 /usr/local/bin/gunicorn --workers 3 --bind unix:/opt/djangoproject/djangoproject.sock djangoproject.wsgi:a> ??965 /usr/bin/python3.6 /usr/local/bin/gunicorn --workers 3 --bind unix:/opt/djangoproject/djangoproject .sock djangoproject.wsgi:a> ??966 /usr/bin/python3.6 /usr/local/bin/gunicorn --workers 3 --bind unix:/opt/djangoproject/djangoproject.sock djangoproject.wsgi:a> 02 mars 22:27:51 centos8 systemd[1] :Démon django démarré. Démarrage de django 20.0.4Mar 02 22:27:52 centos8 django[960] :[2020-03-02 22:27:52 -0500] [960] [INFO] En écoute :unix :/opt/djangoproject/djangoproject.sock (960)2 mars 22:27 :52 centos8 django[960] :[2020-03-02 22:27:52 -0500] [960] [INFO] Utilisation du travailleur :syncMar 02 22:27:52 centos8 django[960] :[2020-03-02 22 :27:52 -0500] [964] [INFO] Booter worker avec pid :964Mar 02 22:27:52 centos8 django[960] :[2020-03-02 22:27:52 -0500] [965] [INFO ] Booter worker avec pid :965Mar 02 22:27:52 centos8 django[960] :[2020-03-02 22:27:52 -0500] [966] [INFO] Booter worker avec pid :966h pid :966 

Configurer Nginx pour Django

Ensuite, vous devrez configurer Nginx en tant que proxy inverse pour Django. Pour ce faire, créez un nouveau fichier de configuration Nginx avec la commande suivante :

nano /etc/nginx/conf.d/django.conf

Ajoutez les lignes suivantes :

serveur { écoute 80 ; server_name your-server-ip location =/favicon.ico { access_log off; log_not_found désactivé ; } emplacement /statique/ { root /opt/djangoproject ; } emplacement / { proxy_set_header Hôte $http_host ; proxy_set_header X-Real-IP $remote_addr ; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; proxy_set_header X-Forwarded-Proto $scheme ; proxy_pass http://unix:/opt/djangoproject/djangoproject.sock ; }}

Enregistrez et fermez le fichier lorsque vous avez terminé. Ensuite, testez nginx pour toute erreur de syntaxe avec la commande suivante :

nginx -t

Si tout va bien, vous devriez obtenir le résultat suivant :

nginx :la syntaxe du fichier de configuration /etc/nginx/nginx.conf est oknginx :le test du fichier de configuration /etc/nginx/nginx.conf est réussi

Ensuite, redémarrez le service Nginx pour mettre en œuvre les modifications :

systemctl démarrer nginx

Vous pouvez également vérifier le Nginx avec la commande suivante :

statut systemctl nginx

Vous devriez obtenir le résultat suivant :

 ? nginx.service - Le serveur HTTP et proxy inverse de nginx Chargé :chargé (/usr/lib/systemd/system/nginx.service ; désactivé ; préréglage du fournisseur :désactivé) Actif :actif (en cours d'exécution) depuis le lundi 2020-03-02 22 :28h13 HNE ; 4min 14s ago Processus :984 ExecStart=/usr/sbin/nginx (code=sorti, statut=0/SUCCESS) Processus :982 ExecStartPre=/usr/sbin/nginx -t (code=sorti, status=0/SUCCESS) Processus :980 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) PID principal :985 (nginx) Tâches :3 (limite :25028) Mémoire :5,5 M CGroup :/system.slice/nginx.service ??985 nginx :processus principal /usr/sbin/nginx ??986 nginx :processus de travail ??987 nginx :processus de travail Mar 02 22:28:12 centos8 systemd[1] :Démarrage de nginx HTTP et serveur proxy inverse...Mar 02 22:28:12 centos8 nginx[982] :nginx :la syntaxe du fichier de configuration /etc/nginx/nginx.conf est correcteMar 02 22:28:12 centos8 nginx[982] :nginx :le test du fichier de configuration /etc/nginx/nginx.conf est réussiMar 02 22:28:13 centos8 systemd[1] :Démarrage du serveur HTTP et proxy inverse nginx.

Vous pouvez maintenant accéder à votre application Django en utilisant l'URL http://your-server-ip.

Conclusion

Dans ce guide, nous avons appris à installer Django sur CentOS 8. Nous avons également appris à utiliser Gunicorn pour créer et gérer le service Django et configurer Nginx en tant que proxy inverse pour servir l'application Django.


Cent OS
  1. Comment installer Pip sur CentOS 8

  2. Comment installer Python 3 sur CentOS 7

  3. Comment installer Django sur CentOS 7

  4. Comment installer Python 3.6.4 sur CentOS 7

  5. Comment installer Python 2.7 ou Python 3.x + Django 1.8 + sur CentOS 6 + cPanel

Comment installer Ionic Framework sur CentOS 7

Comment installer Django sur CentOS 8

Comment installer Ionic Framework sur CentOS 8

Comment installer Python 3.9 sur CentOS 8

Installer Django Python Framework sur Rocky Linux 8 / CentOS 8

Comment installer Python sur CentOS 7