码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2019-01-10 23:00:41      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:space   min   静态文件   false   dia   web   hid   权限设置   export   

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 webmaster@localhost
        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在云服务器部署打开数据库失败

标签:space   min   静态文件   false   dia   web   hid   权限设置   export   

原文地址:https://www.cnblogs.com/acer-haitao/p/10252674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!