Django在云服务器部署打开数据库失败

Posted 喻晓生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django在云服务器部署打开数据库失败相关的知识,希望对你有一定的参考价值。

sudo vi /etc/apache2/envvars
将下面2行改成普通能够登陆系统的账户就可以了,比如
export APACHE_RUN_USER=www-data #设置用户为qunero
export APACHE_RUN_GROUP=www-data #设置组为 www-data

重启服务:sudo service apache2 restart

 

 

【1】/etc/apache2/sites-available目录新建配置文件:Djang2019.conf

技术分享图片
Listen 8008

WSGIPythonPath  /home/ubuntu/.VENV/Django2019:/home/ubuntu/.VENV/Django2019/lib/python3.5/site-packages/ 
WSGIPythonHome  /home/ubuntu/.VENV/Django2019

<VirtualHost 0.0.0.0:8008>
        ServerName 0.0.0.0    
        ServerAlias 0.0.0.0

        ServerAdmin [email protected]
        DocumentRoot /var/www/html
    
        WSGIScriptAlias / /home/ubuntu/workspace/Django2019/Django2019/wsgi.py 

        <Directory /home/ubuntu/workspace/Django2019>
                  Require all granted
     </Directory>

        <Directory /home/ubuntu/workspace/Django2019/Django2019>
             <Files wsgi.py>
                         Require all granted
                 </Files>
        </Directory>

        Alias /media/ /home/ubuntu/workspace/Django2019/media/ 
        Alias /static/ /home/ubuntu/workspace/Django2019/static/

        <Directory /home/ubuntu/workspace/Django2019/media/>
                Require all granted
         </Directory>

         <Directory /home/ubuntu/workspace/Django2019/static/>
         Require all granted
         </Directory>
    


        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>
View Code

【2】加载模块

# Python 2
sudo apt-get install libapache2-mod-wsgi
# Python 3
sudo apt-get install libapache2-mod-wsgi-py3
 

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
<Directory />
        Options FollowSymLinks
        AllowOverride None
        #Require all denied
</Directory>

【3】mysqldb(python3)----pip install mysqlclient 或者PyMySQL

 项目文件__init__.py

import pymysql
pymysql.install_as_MySQLdb()

【4】wsgi.py

import os
import sys

from os.path import join,dirname,abspath
PROJECT_DIR = dirname(dirname(abspath(__file__)))

sys.path.insert(0,PROJECT_DIR)
os.environ[DJANGO_SETTINGS_MODULE] = Django2019.settings


from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

【5】setting.py

技术分享图片
import os
import sys

BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,os.path.join(BASE_DIR,apps))
sys.path.insert(1,os.path.join(BASE_DIR,extra_apps))




ALLOWED_HOSTS = [*]


# Application definition

INSTALLED_APPS = [
    django.contrib.admin,
    django.contrib.auth,
    django.contrib.contenttypes,
    django.contrib.sessions,
    django.contrib.messages,
    django.contrib.staticfiles,
    xadmin,
    crispy_forms,
    index,
]

MIDDLEWARE_CLASSES = [
    django.middleware.security.SecurityMiddleware,
    django.contrib.sessions.middleware.SessionMiddleware,
    django.middleware.common.CommonMiddleware,
    django.middleware.csrf.CsrfViewMiddleware,
    django.contrib.auth.middleware.AuthenticationMiddleware,
    django.contrib.auth.middleware.SessionAuthenticationMiddleware,
    django.contrib.messages.middleware.MessageMiddleware,
    django.middleware.clickjacking.XFrameOptionsMiddleware,
]

ROOT_URLCONF = Django2019.urls

TEMPLATES = [
    {
        BACKEND: django.template.backends.django.DjangoTemplates,
        DIRS: [os.path.join(BASE_DIR,templates)],
        APP_DIRS: True,
        OPTIONS: {
            context_processors: [
                django.template.context_processors.debug,
                django.template.context_processors.request,
                django.contrib.auth.context_processors.auth,
                django.contrib.messages.context_processors.messages,
                django.core.context_processors.media,
            ],
        },
    },
]

WSGI_APPLICATION = Django2019.wsgi.application



DATABASES = {
    default: {
        ENGINE: django.db.backends.mysql,
         NAME: ‘‘,
          USER: root,
           PASSWORD: ‘‘,
           HOST: 127.0.0.1,
            PORT: 3306,
    }
}



AUTH_PASSWORD_VALIDATORS = [
    {
        NAME: django.contrib.auth.password_validation.UserAttributeSimilarityValidator,
    },
    {
        NAME: django.contrib.auth.password_validation.MinimumLengthValidator,
    },
    {
        NAME: django.contrib.auth.password_validation.CommonPasswordValidator,
    },
    {
        NAME: django.contrib.auth.password_validation.NumericPasswordValidator,
    },
]


LANGUAGE_CODE = zh-Hans

TIME_ZONE = Asia/Shanghai

USE_I18N = True

USE_L10N = True

USE_TZ = False



STATIC_URL = /static/
STATIC_ROOT = os.path.join(BASE_DIR, static)
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    os.path.join(BASE_DIR,"xadmin")
]
MEDIA_URL = /media/
MEDIA_ROOT = os.path.join(BASE_DIR, media)
View Code

【6】权限--https://code.ziqiangxuetang.com/django/django-deploy.html

一般目录权限设置为 755,文件权限设置为 644 

假如项目位置在 /home/tu/XXX (在XXX 下面有一个 manage.py,XXX 是项目名称)

sudo chmod -R 644 XXX
sudo find zqxt -type d | xargs chmod 755
 

假如上传目录为 zqxt/media/uploads 文件夹,进入media文件夹,将 uploads 用户组改为www-data,并且赋予该组写权限:

cd media/ # 进入media文件夹
sudo chgrp -R www-data uploads
sudo chmod -R g+w uploads
 
sudo chgrp www-data blog
sudo chmod g+w blog
sudo chgrp www-data blog/db.sqlite3  # 更改为你的数据库名称
sudo chmod g+w blog/db.sqlite3
 
python manage.py collectstatic 收集静态文件
 
 
【7】http://134.175.54.177:8008
 
 

 



 


















以上是关于Django在云服务器部署打开数据库失败的主要内容,如果未能解决你的问题,请参考以下文章

在云服务部署个人的Django项目

导致资产预编译在heroku部署上失败的代码片段

docker 在部署 Redis 容器使用 redis.conf 配置启动失败

Django部署——uwsgi+Nginx

asp.net网站部署在云服务器windows server 2008上

flask+sae