用apache2部署Django应用

来源:互联网 发布:淘宝办学历证真的假的 编辑:程序博客网 时间:2024/05/21 14:05

环境要求:Ubuntu14.04及以上,python3.4及以上


查看系统版本:cat /etc/issue

切换python版本

python2可能导致ImportError: No module named Django错误

查看python版本:python --version

一般Ubuntu系统自带python,在/usr/bin可查看已安装的版本


若为python2,在系统级别更改版本:

罗列出所有可用的 python 替代版本信息:update-alternatives --list python

更新替代列表:

update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2

--install选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先级的选项就会被选中。

此时查看所有的替代版本:


切换python版本:

sudo update-alternatives --config python

会列出可切换的列表,填入数字:


移除替代版本示例:update-alternatives --remove python /usr/bin/python2.7

安装python相关组件

安装pip:sudo apt-get install python3-pip

安装mysqlclient:sudo pip install mysqlclient

安装django:sudo pip install django

如果出现mysql_config not found错误,安装libmysqlclient-dev:sudo apt-get install libmysqlclient-dev

如果出现Python.h:No such file or directory,安装python-dev:sudo apt-get install python-dev


安装mysql

方法一:

docker pull mysql

sudo docker run --name mysql -p 3306:3306 -e MYSQL \_ROOT\_PASSWORD=123456 -d mysql        运行时须指定root密码

运行时可能出现mysql.socket不存在的问题

方法二:

sudo apt-get install mysql-server       安装时会要求输入root密码

注意检查mysql是否有时区表,没有的话可能出现UTC日志转换CONVERT_TZ出错的情况

执行:

select * from mysql.time_zone;

select * from mysql.time_zone_name;

若为空,执行

mysql_tzinfo_to_sql /usr/share/zoneinfo

注:mysql命令行登录

mysql -u root -p mysql

输入root密码


安装apache2

sudo apt-get install apache2

sudo apt-get install libapache2-mod-wsgi-py3

查看apache版本:apachectl -v

配置

我们希望在浏览器通过xxx.xxx.xxx访问网站

新建一个apache2的网站配置文件:sudo vi /etc/apache2/sites-available/sitename.conf

<VirtualHost *:8800>    ServerName xxx.xxx.xxx    DocumentRoot /var/www/a/b                          这里是wsgi.py所在目录    <Directory /var/www/a/b>        Order allow,deny        Allow from all    </Directory>    <Directory /var/www/a/static>       Require all granted    </Directory>    Alias /static /var/www/a/static    WSGIScriptAlias / /var/www/a/b/wsgi.py    ErrorLog ${APACHE_LOG_DIR}/error_8800.log    CustomLog ${APACHE_LOG_DIR}/access_8800.log combined</VirtualHost>

/etc/apache2/ports.conf下也将80端口改为8800

这里使用的端口是8800,因为我们这里80端口被nginx占用

在nginx的配置文件中修改

location / {    proxy_pass http://127.0.0.1:8800;}

重启nginx:

sudo nginx -s reload

一般将根目录及以下目录权限设置为755,文件权限设置为644

sudo chmod -R 644 a

sudo find a -type d | xargs chmod 755

如果有上传需求,将上传文件夹设置写权限

激活网站:sudo a2ensite sitename 或 sudo a2ensite sitename.conf

更改网站程序需要重启apache2,service apache2 restart若无效

ps -aux | grep apache2 将三个进程全部杀死

kill -s 9 <pid>

再启动service apache2 start

调试

Django的日志在/your_project_name/settings.py文件中配置。具体配置如下:

LOGGING = {    'version': 1,    'disable_existing_loggers': False,    'formatters': {        'standard': {            'format': '%(asctime)s %(levelname)-8s %(message)s'        },        'detail': {            'format': '%(asctime)s %(levelname)-8s %(pathname)s[line:%(lineno)d] %(message)s'        },    },    'handlers': {        'console': {            'level': 'INFO',            'class': 'logging.StreamHandler',            'formatter': 'standard',        },        'file': {            'level': 'INFO',            'class': 'logging.handlers.RotatingFileHandler',            'filename': '/var/log/django.log',            'maxBytes': 1024 * 1024 * 5,  # 5 MB            'backupCount': 100,            'formatter': 'detail',        },        'app1_file': {            'level': 'INFO',            'class': 'logging.handlers.RotatingFileHandler',            'filename': '/var/log/app1.log',            'maxBytes': 1024 * 1024 * 5,  # 5 MB            'backupCount': 100,            'formatter': 'detail',        },        'app2_file': {            'level': 'INFO',            'class': 'logging.handlers.RotatingFileHandler',            'filename': '/var/log/app2.log',            'maxBytes': 1024 * 1024 * 5,  # 5 MB            'backupCount': 100,            'formatter': 'detail',        },    },    'loggers': {        'django': {            'handlers': ['console', 'file'],            'level': 'INFO',            'propagate': True,        },        # 自定义模块日志        'users': {            'handlers': ['console', 'file'],            'level': 'DEBUG',            'propagate': True,        },        'common': {            'handlers': ['console', 'file'],            'level': 'DEBUG',            'propagate': True,        },        'myapp': {            'handlers': ['console', 'file'],            'level': 'DEBUG',            'propagate': True,        },        'app1': {            'handlers': ['console', 'app1_file'],            'level': 'INFO',            'propagate': True,        },        'pushdata': {            'handlers': ['console', 'app2_file'],            'level': 'INFO',            'propagate': True,        },    },}

此配置分成三个部分:

  • formatters: 指定输出的格式,被handler使用。
  • handlers: 指定输出到控制台还是文件中,以及输出的方式。被logger引用。
  • loggers: 指定django中的每个模块使用哪个handlers。以及日志输出的级别。

注意:日志的输出级别是由loggers中的每个模块中level选项定义。如果没有配置,那么默认为warning级别。

然后在每个模块的views.py中,通过下面代码使用:

import logginglogger = logging.getLogger(__name__)

具体的输出部分代码为:

logger.debug("hello, world")logger.info("hello, world")logger.error("hello, world")

日志在/var/log/apache2/

如果采用print来打印调试信息

python manage.py runserver --noreload

这样print出来的信息就可以在终端看到了,但是这样作,每次python代码有修改,就要重启服务器。

参考链接:

http://www.linuxidc.com/Linux/2007-07/6178.htm

http://blog.csdn.net/u011534057/article/details/51615193

https://segmentfault.com/q/1010000003713912

https://stackoverflow.com/questions/18363022/importerror-no-module-named-pip

http://www.cnblogs.com/xiazh/archive/2012/12/12/2814289.html

http://blog.csdn.net/hello_orange/article/details/6184420

http://code.ziqiangxuetang.com/django/django-deploy.html

http://www.cnblogs.com/OnlyDreams/p/7171612.html

http://www.cnblogs.com/zhangqunshi/p/6641173.html

https://zhidao.baidu.com/question/496750276253579564.html