[关闭]
@rayiooo 2018-05-26T03:50:52.000000Z 字数 3588 阅读 1179

Readme

BlogRayiooo

这是一个Django博客。

更方便地查看文档

Author 爱吃大板
Email rayiooo@foxmail.com

1 支持python库(python3.6)

2 软件工具

3 搭建步骤

3.1 准备工作

3.1.1 新建项目

在PyCharm的File/New Project中新建一个Django项目。

3.1.2 修改数据库类型

mysite/settings.py中修改数据库类型为MySQL,如下:

  1. import pymysql
  2. pymysql.install_as_MySQLdb()
  3. DATABASES = {
  4. 'default': {
  5. 'ENGINE': 'django.db.backends.mysql',
  6. 'NAME': 'blogdb',
  7. 'USER': 'root',
  8. 'PASSWORD': '123456',
  9. 'HOST': '127.0.0.1',
  10. 'PORT': '3306',
  11. }
  12. }

3.1.3 创建数据库

通过phpStudy的MySQL管理器进入数据库,新建一个数据库blogdb,并保持数据库登入账号密码与上面的值相同。

3.1.4 执行数据库同步

Django默认帮我们做很多事情,比如User、Session 这些都需要创建表来存储数据,Django已经把这些模块帮我准备好了,我们只需要执行数据库同步,把相关表生成出来即可:

  1. mysite> python manage.py migrate
  2. Operations to perform:
  3. Apply all migrations: admin, auth, contenttypes, sessions
  4. Running migrations:
  5. Applying contenttypes.0001_initial... OK
  6. Applying auth.0001_initial... OK
  7. Applying admin.0001_initial... OK
  8. Applying admin.0002_logentry_remove_auto_add... OK
  9. Applying contenttypes.0002_remove_content_type_name... OK
  10. Applying auth.0002_alter_permission_name_max_length... OK
  11. Applying auth.0003_alter_user_email_max_length... OK
  12. Applying auth.0004_alter_user_username_opts... OK
  13. Applying auth.0005_alter_user_last_login_null... OK
  14. Applying auth.0006_require_contenttypes_0002... OK
  15. Applying auth.0007_alter_validators_add_error_messages... OK
  16. Applying auth.0008_alter_user_username_max_length... OK
  17. Applying auth.0009_alter_user_last_name_max_length... OK
  18. Applying sessions.0001_initial... OK

如果在这个过程中出现任何错误,参照 django数据库错误相关问题

3.1.5 创建超管账号

要想登录admin后台,必须要有帐号,接下来创建超级管理员帐号。

  1. mysite> python manage.py createsuperuser
  2. Username (leave blank to use 'fnngj'): admin # 管理员帐号
  3. Email address: admin@mail.com # email
  4. Password: # 密码
  5. Password (again): # 重复密码
  6. Superuser created successfully.

3.1.6 运行本地服务器

在PyCharm中直接运行程序,服务器即可启动。当然也可以通过python manage.py runserver命令行指令执行。

访问 127.0.0.1:8000 以及 127.0.0.1:8000/admin 即可在本地查看主界面和admin界面。

3.2 部署服务器端

特别参考:windows 下 apache 部署 django python3.6

特别强调注意的是:apache 的位制和python的位制必须一致(即apache 32位的就只能安装python 32位的,64位也一样)网上很多教程没强调这一点,导致后续的很多工作白费,还找不到问题所在!

因此如果使用phpStudy的apache 32位,就必须使用python 32位。

3.2.1 mod_wsgi安装

通过pip install mod_wsgi安装mod_wsgi。

如果无法直接安装,就到https://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi下载whl文件,放到任意目录下,并在该目录下启动cmd,输入pip install 文件名.whl安装模块。

比如Apache2.4、python3.6/32位就选择下载mod_wsgi‑4.6.4+ap24vc14‑cp36‑cp36m‑win32.whl

3.2.2 部署wsgi模块到apache

在命令行中输入以下指令:

  1. mod_wsgi-express module-config

可以看到以下wsgi模块位置的输出结果,将它们复制下来。(在cmd中选中、右击即可复制粘贴。)

LoadFile "c:/program files (x86)/python36/python36.dll"

LoadModule wsgi_module "c:/program files (x86)/python36/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win32.pyd"

WSGIPythonHome "c:/program files (x86)/python36"

在phpStudy软件中点击其他选项菜单/打开配置文件/httpd-conf,把这三行内容复制到到LoadModule最后的部分(应该也可以复制到整个文件的最后部分)。

3.2.3 部署django项目路径到apache

同样,在apache的http.conf末尾添加下面的内容。目录不同,请对照自己的项目做相应更改。

  1. #指定website的wsgi.py配置文件路径
  2. WSGIScriptAlias / C:/Users/Administrator/Desktop/WebServer/_180524_BlogRayiooo/mysite/wsgi.py
  3. #指定项目路径
  4. WSGIPythonPath C:/Users/Administrator/Desktop/WebServer/_180524_BlogRayiooo
  5. <Directory C:/Users/Administrator/Desktop/WebServer/_180524_BlogRayiooo/mysite>
  6. <Files wsgi.py>
  7. Require all granted
  8. setHandler wsgi-script
  9. </Files>
  10. </Directory>

3.2.4 wsgi.py配置

在项目的wsgi.py文件中import os后添加如下代码。

  1. import sys
  2. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  3. sys.path.append(BASE_DIR)

3.2.5 配置完成

在phpStudy中重启apache,从本地(127.0.0.1)和外网(服务器地址)分别访问,查看是否配置成功。

如果出现HTTP 500 - Internal server error错误,在apache/logs/error.log中查看错误原因。

如果出现DisallowedHost at /错误,就在项目的settings.py文件下做出更改:

  1. ALLOWED_HOSTS = ['*']

参考资料

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注