如何在 CentOS 8 上安装和配置 Django

介绍

在本文中,我们将讨论 Django 是什么、它的用途以及如何在 CentOS 8 上安装和配置它。

什么是姜戈?

Django 是一个高级 Python 框架,用于在 Python 中开发 Web 应用程序。 它为开发未来的应用程序提供了必要的结构。 由于这个平台,我们简化了我们的工作和许多其他必要的功能(如授权)。 框架中已经编写了各种其他用途,这让我们可以专注于功能。 Django 是最流行的 Python 框架之一。 它是小问题以及大型企业解决方案的理想选择。

Django 使用模型-视图-控制器(或 MVC)设计模式。 它也经常用于社交网络软件,如聊天室和其他基于 Web 的应用程序。 应用程序如 InstagramSpotify, Pinterest, YouTube、Google 等都是用 Django 编写的。

在 CentOS 8 上安装

先决条件

Django 安装需要以下要求:

  • 服务器 4 GB RAM 和 2 个内核。
  • 操作系统 CentOS 8
  • 蟒蛇 3
  • 点 3
  • 我们以 root 用户身份执行所有命令。 (如果您从普通用户运行命令,则必须使用 sudo 命令。)

系统升级

首先,通过运行以下命令更新系统和应用程序包。

[root@host]# dnf update && dnf upgrade
Last metadata expiration check: 1:47:26 ago on Sat 20 Feb 2021 02:22:42 AM EST.
Dependencies resolved.
Nothing to do.
Complete!
Last metadata expiration check: 1:47:27 ago on Sat 20 Feb 2021 02:22:42 AM EST.
Dependencies resolved.
Nothing to do.
Complete!
[root@host]#  

安装 Python

安装和运行 Django 需要 Python。 使用以下命令安装它。

[root@host]# dnf install python3 python3-pip
Last metadata expiration check: 1:48:19 ago on Sat 20 Feb 2021 02:22:42 AM EST.
Package python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64 is already installed.
Package python3-pip-9.0.3-18.el8.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@host]#

正如我们在上面看到的,我已经在我的系统上安装了 Pip,并且安装通知了我。

python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64 is already installed.
Package python3-pip-9.0.3-18.el8.noarch is already installed.

但为了确定起见,我们将使用 python3 -V 命令进行检查。

[root@localhost ~]# python3 -V
Python 3.6.8
[root@localhost ~]#

验证点子版本

从 Python 3.4 开始,默认安装 Pip。 接下来,我们将验证安装的 Pip 版本。

[root@localhost ~]# pip3 -V
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
[root@localhost ~]#  

Django 安装

我们将使用 Pip3 安装 Django。 这个 python 包管理器有助于安装和配置使用 Django 所需的所有应用程序。

[root@host]# pip3 install Django
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting Django
  Downloading https://files.pythonhosted.org/packages/b8/6f/9a4415cc4fe9228e26ea53cf2005961799b2abb8da0411e519fdb74754fa/Django-3.1.7-py3-none-any.whl (7.8MB)
    100% |████████████████████████████████| 7.8MB 209kB/s 
Collecting asgiref<4,>=3.2.10 (from Django)
  Downloading https://files.pythonhosted.org/packages/89/49/5531992efc62f9c6d08a7199dc31176c8c60f7b2548c6ef245f96f29d0d9/asgiref-3.3.1-py3-none-any.whl
Collecting sqlparse>=0.2.2 (from Django)
  Downloading https://files.pythonhosted.org/packages/14/05/6e8eb62ca685b10e34051a80d7ea94b7137369d8c0be5c3b9d9b6e3f5dae/sqlparse-0.4.1-py3-none-any.whl (42kB)
    100% |████████████████████████████████| 51kB 4.1MB/s 
Requirement already satisfied: pytz in /usr/lib/python3.6/site-packages (from Django)
Installing collected packages: asgiref, sqlparse, Django
Successfully installed Django-3.1.7 asgiref-3.3.1 sqlparse-0.4.1
[root@host]#  

接下来,我们可以验证 Django 的版本以确保一切都安装正确。 我们将版本标志与主要 django-admin 命令。

[root@host]# django-admin --version
3.1.7
[root@host]#  

创建 Django 应用程序

现在我们已经安装了 Django,让我们对其进行测试。 让我们使用 Django 创建我们的第一个应用程序。

使用 django- 创建一个项目admin 启动项目命令。 在主命令之后,我们指定我们正在构建的应用程序的名称——BYNSS_app。 这将创建一个名为BYNSS_app/ 的文件夹。

[root@host]# django-admin startproject BYNSS_app

现在,将目录 (cd) 更改为项目文件夹。

[root@host]# cd BYNSS_app/
[root@host BYNSS_app]#

应用更改

接下来,我们使用 Python 执行迁移。 为了让 Django 将必要的库和文件传输到项目中,此步骤是必要的。

[root@host BYNSS_app]# python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK
[root@host BYNSS_app]#

创建管理员

接下来,让我们创建一个 Django 应用程序 admin 使用命令。

[root@host BYNSS_app]# python3 manage.py createsuperuser
Username: margaret
Email address: [email protected]
Password: 
Password (again): 
Superuser created successfully.
[root@host BYNSS_app]#

配置应用程序

我们的测试应用程序几乎完成了。 要在浏览器中启动界面,我们需要配置 IP 地址解析。 如果您不知道您的 IP,请使用 ifconfig 命令查找它。

[root@host BYNSS_app]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.50.154  netmask 255.255.255.0  broadcast 192.168.50.255
        inet6 fe80::efca:4bfb:98f8:5655  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:af:58:e0  txqueuelen 1000  (Ethernet)
        RX packets 22918  bytes 17925384 (17.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5861  bytes 426326 (416.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@host BYNSS_app]#

现在,使用您选择的编辑器将 IP 输入到 Django 配置文件中。 我正在使用 nano 编辑器来完成这项任务。

[root@host BYNSS_app]# nano BYNSS_app/settings.py 

然后,使用命令 ctrl + s 和 ctrl + x(对于 nano)保存并退出。

配置防火墙

接下来,让我们在防火墙中打开端口以通过网络访问 Django。 我们将打开端口 80 和 8000。

[root@host BYNSS_app]# firewall-cmd --permanent --add-port=80/tcp
success
[root@host BYNSS_app]#
[root@host BYNSS_app]# firewall-cmd --permanent --add-port=8000/tcp
success
[root@host BYNSS_app]#

重新加载防火墙

我们必须重新加载防火墙才能使更改生效。

[root@host BYNSS_app]# firewall-cmd --reload
success
[root@host BYNSS_app]#

启动 Django 应用程序

最后,我们启动我们的 Django 应用程序。

[root@host BYNSS_app]# python3 manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
February 20, 2021 - 09:57:32
Django version 3.1.7, using settings 'BYNSS_app.settings'
Starting development server at https://0.0.0.0:8000/
Quit the server with CONTROL-C.

现在,让我们打开浏览器访问我们之前输入的 IP – https://My_IP_Address: 8000。

之前,我们创建了一个管理员,所以现在我们将进入管理部分,在这里我们可以测试一些测试应用程序的功能。 转到以下地址:https://192.168.50.154:8000/admin.

安装 django 2

接下来,输入我们的用户名和密码

安装 django 3

我们以管理员身份进入应用程序,所以现在我们可以仔细看看 Django 在我们的测试运行中提供的所有可能性。

结论

我们研究并弄清楚了 Django 框架是什么类型的,它是做什么用的,以及它在哪里使用。 我们还在 CentOS 8 上安装了它并在测试模式下运行它。 Django 擅长构建一个快速的网站,而且它做得很好。 它不仅可以让您快速编写,还可以提供质量保证。 剩下的就看开发商了。

有问题吗? 如果您是完全托管的 VPS 服务器, Cloud 专用,VMWare 私有 Cloud私有父服务器, 托管 Cloud 服务器或专用服务器所有者,并且您对执行概述的任何步骤感到不舒服,可以通过电话 800.580.4985 与我们联系, 聊天 或支持票以帮助您完成此过程。