当前位置:首页 » python web » 正文

Django+python服务器部署与环境部署教程详解

看: 1281次  时间:2020-07-28  分类 : python web

需要准备环境:python3.6、vultr(或者其他服务器)、xshell

第一步:python安装必备环境Django库

Xshell链接远程主机:

点击连接之后:弹窗输入访问用户及密码,一般为root用户


成功连接到目标服务器:

[root@vultr ~]#

安装python以及需要环境(此为安装完python3.6环境),运行pip安装即可:

pip install django

提示Success安装成功

第二步:项目创建

首先cd到自己想要新建项目的路径:
我这里选择的是在data下新建文件夹pytest来保存项目

cd /data
mkdir pytest
cd pytest

接下来进行新建项目 django-admin startproject +(你的项目名:此处使用的是pytest)

django-admin startproject pytest
cd pytest #进入项目
cd pytest #进入项目配置列表

此时项目的状态是

[root@vultr pytest]# tree
.
├── db.sqlite3
├── manage.py
└── pytest
 ├── asgi.py
 ├── __init__.py
 ├── __pycache__
 │ ├── __init__.cpython-36.pyc
 │ ├── settings.cpython-36.pyc
 │ ├── urls.cpython-36.pyc
 │ └── wsgi.cpython-36.pyc
 ├── settings.py
 ├── urls.py
 └── wsgi.py

接下来重要的一步在setting.py中设置访问权限:
划重点——这个地方是可以访问的外部主机地址,为了方便访问我们改为['*'],意思为任何主机都可访问

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*'] #划重点——这个地方是可以访问的外部主机地址,为了方便访问我们改为['*'],意思为任何主机都可访问


# Application definition

INSTALLED_APPS = [
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
"settings.py" 120L, 3093C

修改完成后(确保开放8000端口的访问,防火墙中设置):

python manage.py runserver 0.0.0.0:8000

成功开启:

[root@vultr pytest]# python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

March 28, 2020 - 02:27:55
Django version 3.0.4, using settings 'pytest.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

然后访问浏览器输入服务器ip+端口号例如:22.21.21.200:8000

之后可以在服务器端查看日志:

[28/Mar/2020 02:28:42] "GET / HTTP/1.1" 200 16299

总结

到此这篇关于Django+python服务器部署与环境部署教程详解的文章就介绍到这了,更多相关django python服务器部署内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!

标签:django  pytest  

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!