django在centos生产环境的部署
# 安装数据库和web服务器nginx
# yum install –y nginx mariadb-server
# 安装虚拟环境
pip install virtualenv
pip install virtualenvwrapper
# 在bashrc中加入如下环境变量
[root@node5 ~]# cat ~/.bashrc # .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi export WORKON_HOME=$HOME/.virtualenvs export PROJECT_HOME=$HOME/workspace source /usr/bin/virtualenvwrapper.sh [root@node5 ~]# source ~/.bashrc # 创建虚拟环境mxonline [root@node5 ~]# mkvirtualenv mxonline
# 在开发环境中导出我们的依赖环境列表
C:\WINDOWS\system32>workon mxonline
(mxonline) C:\Windows\System32>pip list
将文件导出到requirement.txt中
(mxonline) C:\Windows\System32>pip freeze > requirement.txt
将requirement.txt拷贝到线上,运行批量安装
(mxonline) [root@node5 ~]# pip install -r requirement.txt
中途安装有报错:
yum install python-devel mysql-devel
# 安装uwsgi
(mxonline) [root@node5 ~]# pip install uwsgi Mysql > create database mxonline; # 试着启动 (mxonline) [root@node5 mxonline]# python manage.py runserver Performing system checks... /root/.virtualenvs/mxonline/lib/python2.7/site-packages/DjangoUeditor/urls.py:11: RemovedInDjango110Warning: django.conf.urls.patterns() is deprecated and will be removed in Django 1.10. Update your urlpatterns to be a list of django.conf.urls.url() instances instead. url(r'^controller/$',get_ueditor_controller) System check identified no issues (0 silenced). You have unapplied migrations; your app may not work properly until they are applied. Run 'python manage.py migrate' to apply them. November 14, 2019 - 19:29:31 Django version 1.9.8, using settings 'mxonline.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
把windows开发环境中的数据传输到模拟生产的数据库中
修改监听端口重新运行
(mxonline) [root@node5 mxonline]# python manage.py runserver 0.0.0.0:8000
使用uwsgi程序启动django程序
(mxonline) [root@node5 mxonline]# uwsgi --http :8000 --module mxonline.wsgi
# 修改settings.py文件
# 把这段注释掉
#STATICFILES_DIRS = [
# os.path.join(BASE_DIR, "static")
#]
# 添加static_root
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
(mxonline) [root@node5 mxonline]# python manage.py collectstatic You have requested to collect static files at the destination location as specified in your settings: /data/www/vhosts/mxonline/static This will overwrite existing files! Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: yes Copying '/root/.virtualenvs/mxonline/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/calendar.js' Copying '/root/.virtualenvs/mxonline/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/core.js' Copying '/root/.virtualenvs/mxonline/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/actions.min.js' Copying '/root/.virtualenvs/mxonline/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/collapse.js' Copying '/root/.virtualenvs/mxonline/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/timeparse.js' Copying '/root/.virtualenvs/mxonline/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/actions.js' Copying '/root/.virtualenvs/mxonline/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/prepopulate.min.js'
# 编辑uwsgi.ini启动文件
(mxonline) [root@node5 conf]# cat /data/www/vhosts/mxonline/conf/uwsgi.ini # mysite_uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) chdir = /data/www/vhosts/mxonline # Django's wsgi file module = mxonline.wsgi # the virtualenv (full path) # process-related settings # master master = true # maximum number of worker processes processes = 10 # the socket (use the full path to be safe socket = 127.0.0.1:8000 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true virtualenv = /root/.virtualenvs/mxonline logto = /tmp/mylog.log
# 启动uwsgi 后台运行
# uwsgi -i uwsgi.ini &
此时绑定hosts再次访问80端口就可以访问了
10.10.11.215 www.mxonline.com
# 启动了10个进程
# 自动重启uwsgi
pkill –f uwsgi
会自动重启uwsgi程序
Setgings.py中关闭debug
Debug = False