如何使用Ubuntu 16.04的Django一键安装镜像

时间:2022-06-02
本文章向大家介绍如何使用Ubuntu 16.04的Django一键安装镜像,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

介绍

Django是一个用于快速开发Web应用程序的高级Python框架。Django一键安装程序通过Django,Nginx,Gunicorn和Postgres为您的服务器快速部署预配置的开发环境。腾讯云实验室也提供了基于Ubuntu搭建Django站点pip安装django的方法;

创建Django Droplet

要创建Django Droplet,请从Droplet创建页面开始。在“ 选择镜像”部分中,单击“ 一键安装”应用程序选项卡,然后选择16.04图像上的Django 1.8.7

接下来,选择Droplet的大小,所需的区域以及任何其他设置(如专用网络IPv6支持或备份)。添加SSH密钥并填写Droplet的主机名。准备好后,单击“ 创建Droplet”以启动服务器。

创建后,导航http://your_server_ip到您喜欢的浏览器以验证Django是否正在运行。你会看到一个带有标题的页面!祝贺你成功进入第一个支持Django的页面。

您现在可以以root用户身份登录Droplet 。

$   ssh root@your_server_ip

请务必阅读当天的消息,其中包含有关安装的重要信息,例如Django用户和Postgres数据库的用户名和密码。

Login output
-------------------------------------------------------------------------------
Thanks for using the DigitalOcean Django One-Click Application Image
The "ufw" firewall is enabled. All ports except for 22, 80, and 443 are BLOCKED.
Let's Encrypt has been pre-installed for you. If you have a domain name, and
you will be using it with this 1-Click app, please see: http://do.co/le-lemp
Django is configured to use Postgres as the database back-end.
You can use the following SFTP credentials to upload your files (using
FileZilla/WinSCP/Rsync):
Host: your_server_ip
User: django
Pass: 2fd21d69bb13890c960b965c8c88afb1

You can use the following Postgres database credentials:
DB: django
User: django
Pass: 9853a37c3bc81bfc15f264de0faa9da5

Passwords have been saved to /root/.digitalocean_passwords

如果以后需要再参考,可以在文件/etc/update-motd.d/99-one-click中找到相关信息。

配置细节

Django项目由Gunicorn提供服务,在/home/django/gunicorn.socket侦听。Gunicorn由Nginx代理,后者在端口80侦听。

Nginx配置文件位于/etc/nginx/sites-enabled/django。如果重命名项目文件夹,请记住更改静态文件的路径。

Gunicorn是在一个Systemd文件/etc/systemd/system/gunicorn.service启动时启动的。此Systemd脚本还提供位于/etc/gunicorn.d/gunicorn.py中的配置文件,该文件设置工作进程的数量。您可以在Gunicorn项目的文档中找到有关配置Gunicorn的更多信息。

Django项目本身位于/home/django/django_project。

注意:如果重命名项目文件夹,则需要进行一些配置文件更新。具体来说,您需要在Nginx配置中更改静态文件的路径。您也需要在Gunicorn Systemd文件中更新WorkingDirectory,name以及pythonpath。

可以使用Gunicorn服务启动,重新启动或停止项目。例如,要在进行更改后重新启动项目,请运行:

#   systemctl restart gunicorn.service

在开发过程中,每次进行更改时重新启动服务器都会很烦人。在这种情况下,您可以使用Django的内置开发服务器,它自动检测更改:

#   systemctl stop gunicorn.service
#   python manage.py runserver 0.0.0.0:8000

然后,您可以通过http://your_server_ip:8000访问该应用程序。此内置服务器无法提供最佳性能,因此最好使用Gunicorn服务进行生产。

编写你的第一个Django应用程序

有很多关于编写Django应用程序的高级教程,但是这一步将让您启动并运行一个非常基本的Django应用程序。

如果您还没有,请以root用户身份登录服务器。

ssh root@your_server_ip

接下来,切换到django用户。

#   su django

进入项目目录。

$   cd /home/django/django_project

现在创建一个名为hello的新应用。

python manage.py startapp hello

这将在文件夹django_project中创建一个名为hello的新目录。整个目录的结构如下:

目录结构
├── django_project
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── settings.py.orig
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── hello
│   ├── admin.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── manage.py

这没有必要,但您可以使用该tree实用程序自行生成此输出。安装sudo apt-get install tree然后使用tree /home/django/django_project。

接下来,创建您的第一个视图。打开文件hello/views.py使用nano进行编辑或选择您喜欢的文本编辑器。

$   nano hello/views.py

原文 hello/ views.py 看起来像这样:

from django.shortcuts import render
# Create your views here.

修改以下内容。这告诉Django返回Hello,world!这是我们作为HTTP响应的第一个视角。

修改了 hello/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
	return HttpResponse("Hello, world! This is our first view.")

保存并关闭文件。接下来我们需要将刚创建的视图连接到URL。为此,打开django_project/urls.py进行编辑。

$   nano django_project/urls.py

将以下两行添加到文件中,该文件将导入刚刚创建的视图并将其设置为默认URL:

django_project / urls.py
. . .
from django.conf.urls import include, url
from django.contrib import admin
from hello import views
urlpatterns = [
	url(r'^$', views.index, name='index'),
	url(r'^admin/', include(admin.site.urls)),
]

保存并关闭该文件,然后注销django用户并返回到root shell。

$   exit

root身份重启项目。

$   systemctl restart gunicorn.service

现在,如果您重新加载Droplet的IP地址http://your_server_ip,您将看到一个包含Hello,world的页面!这是我们的第一个界面。

结论

你已经准备好开始使用Django了。您还可以查看官方Django项目文档,还可以从腾讯云社区获取基于 CentOS 搭建 Python 的 Django 环境等其他相关知识。


参考文献:《How To Use the Django One-Click Install Image for Ubuntu 16.04》