阿里云部署django Ubuntu 12.04.5 + Apache2.2 + django1.8

阿里云部署django Ubuntu 12.04.5 + Apache2.2 + python3.3 + django1.8 + Mysql 5.5.46

vsftpd 安装与配置

    apt-get update
    apt-get install vsftpd

ftpd: version 2.3.5
修改 /etc/vsftpd.conf

  #anonymous_enable=YES 注释,避免匿名登录
   local_enable=YES
   write_enable=YES # 没有这个会报550的权限错误

修改 /etc/ftpusers, 删除 root, 确保可以以root进行远程登录(我是新手,不知道这样的风险大不大)

apache2 安装与配置

    apt-get install apache2 apache2-dev
    # Server version: Apache/2.2.22 (Ubuntu)

安装python3.3

直接在本地下载好,ftp进阿里云, Python-3.3.6.tgz, 是所以用3.3是因为3.4的mod-wsgi总是装不好

    sudo apt-get install openssl
    sudo apt-get install libssl-dev
    tar zxvf Python-3.3.6.tgz
    cd Python-3.3.6
    ./configure # 我是默认安装,可以增加 --prefix=/path/ 自定义安装目录
    make
    make install
    # 安装  pip
    wget https://bootstrap.pypa.io/get-pip.py 
    python3 setup.py install
    # 

安装 django1.8

    pip3 install django==1.8

django建立项目,请自行搜索,不再赘述

安装mysql

    sudo apt-get install mysql-server
    sudo apt-get isntall mysql-client
    sudo apt-get install libmysqlclient-dev

django1.8 中的mysql 只支持 MySQLdb, 而python3 没有MySQLdb这个包,这里我们用pymysql代替,需要做小小的修改在 settings.py 同一文件夹下的 init.py 下添加

import pymysql
pymysql.install_as_MySQLdb()

需要将django默认的sqlite3修改为mysql,将下面代码替换settings.py 的DATABASES


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'database_name',
        'USER': 'database_user',
        'PASSWORD': 'database_pwd',
        'HOST': '',
        'PORT': '',
    }
}

安装 mod-wsgi

   pip3 install mod-wsgi

将django目录写入 /etc/apache2/httpd.conf

ServerName 你的ip
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com

to/mysite.com/mysite>

Order deny,allow
Allow from all
# Require all granted
#  If you are using a version of Apache older than 2.4, replace Require all granted with Allow from all and also add the line Order deny,allow above it.



Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico

Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/

to/mysite.com/static>
Require all granted


to/mysite.com/media>
Require all granted

重启apache apache2ctl restart
报错

Invalid command 'WSGIScriptAlias', perhaps misspelled or defined by a module not included in the server configurationAction 'configtest' failed.

需要 安装 libapache2-mod-wsgi

sudo apt-get install libapache2-mod-wsgi
sudo a2enmod wsgi
sudo service apache2 restart

输入网址

报错 403 Forbidden

修改django文件权限

sudo chmod -R 755 path

报错找不到 django.core.wsgi

修改 /etc/apache2/httpd.conf文件,加入


WSGIPythonPath /usr/local/lib/python3.3/site-packages/
这里是 python3 package的地址,可以用python sys.path查看

继续报错, Could not import settings ‘myproject.settings’ (Is it on sys.path?): No module named settings

都把人给整疯了,查找资料发现 wsgi.py 应该加入以下代码(参考http://www.ziqiangxuetang.com/django/django-deploy.html)

import os
from os.path import join,dirname,abspath

PROJECT_DIR = dirname(dirname(abspath(__file__)))#3
import sys # 4
sys.path.insert(0,PROJECT_DIR) # 5

os.environ["DJANGO_SETTINGS_MODULE"] = "blog.settings" # 7

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

第 3,4,5 行为新加的内容,作用是让脚本找到django项目的位置,也可以在sitename.conf中做,用WSGIPythonPath,想了解的自行搜索, 第 7 行如果一台服务器有多个django project时一定要修改成上面那样,否则访问的时候会发生网站互相串的情况,即访问A网站到了B网站,一会儿正常,一会儿又不正常(当然也可以使用 mod_wsgi daemon 模式,点击这里查看)

报错 Non-ASCII character

好奇怪,按说 python3 不应该有编码的问题,可还是出现了,无奈 文件首行加入 #coding=utf-8

报错 Invalid command ‘WSGIScriptAlias’, perhaps misspelled or defined by a module not included in the server configurationAction ‘configtest’ failed.

解决

sudo apt-get install libapache2-mod-wsgi
sudo a2enmod wsgi
sudo service apache2 restart

报错 无法导入 某模块

Request Method: GET
Django Version: 1.8
Exception Type: ImportError
Exception Value:
cannot import name _imaging
Exception Location: /usr/local/lib/python3.3/site-packages/PIL/Image.py in , line 66
Python Executable: /usr/bin/python
Python Version: 2.7.3
Python Path:
[‘/var/www/web’,
‘/usr/local/lib/python3.3/site-packages’,
‘/usr/lib/python2.7’,
‘/usr/lib/python2.7/plat-linux2’,
‘/usr/lib/python2.7/lib-tk’,
‘/usr/lib/python2.7/lib-old’,
‘/usr/lib/python2.7/lib-dynload’,
‘/usr/local/lib/python2.7/dist-packages’,
‘/usr/lib/python2.7/dist-packages’]

解决
这里发现python的版本居然是 python2.7,怪不得上面的编码也会出错,原来是mod_wsgi的版本不对,于是 将python3的so文件拷贝到apache下

cp /usr/local/lib/python3.3/site-packages/mod_wsgi/server/mod_wsgi-py33.cpython-33m.so /usr/lib/apache2/modules/mod_wsgi.so

重启apache

apache2ctl restart

搞定

你可能感兴趣的:(python,django,mysql)