Django1.8 CentOS7 Nginx uwsgi

The deployment set for a long time, at first i met the environment problem which is my code based on Python3.4. Python2.7 and python3.4 conflict. So, i try to use virtualenv to make it success, and i have to do many works again.python

Django

I used it in august 2015, later i found Flask with Gunicorn is better way to finish a small project.mysql

Install django

This is needless to say, it is ok to use pip.
Recommend a source image: https://pypi.doubanio.com/simple/sql

Start django

start projectdjango

django-admin.py startproject xxc

start appapp

python manage.py startapp xxc_student

if you want to user pymysql, you should change __init.py__ as follow:less

import pymysql

pymysql.install_as_MySQLdb()

after all, synchronous databasesocket

python3 manage.py syncdb

uwsgi

Install uwsgi

These were some error you may encounter:this

  • uwsgi: option '--http' is ambiguousrest

The problems associated with the environment of python,it is recommented to use pip install uwsgi to install uwsgicode

  • ImportError: No module named 'wsgi'

Wsgi configuration file with the manage.py in the same directory,there are many kinds of format wsgi configuration file,such as.wsgi and .py, you can use both of them in the same way.

Start uwsgi

start command, this command is a test way to run the Django.

uwsgi --http-socket :8000 --chdir /opt/AnJiBei_Python_Project/anjibei/ --module django_wsgi

You can make uwsgi become a deamon with log.

uwsgi --http-socket :8000 --chdir /opt/AnJiBei_Python_Project/anjibei/ --module django_wsgi --daemonize /opt/AnJiBei_Python_Project/anjibei/log/uwsgi.log

Also important is that django_wsgi is the configuration file which should be in the same directory with manage.py.Don't write an absolute path, otherwise, may lead to the import error.

These were some error you may encounter:

  • Static resource access failure

Run python manage.py collectstatic,and static files will be copied to the STATIC_ROOT specified static folder.

This command feels a bit strange, my static files distribution under different startapp, now, all of them... hum... be synchronized.The location of the static files were affected by the position where you execute the uwsgi command. So, you can find the static files were in the same directory with manage.py. Here is a picture of original, but... you know...

Stop uwsgi

Just we specify th 8000 port, so we can use it to find the process no.

netstat -apn | grep 8000

Or, you can user the process name directly.

netstat -apn | grep uwsgi

Later, use kill to stop the uwsgi deamon.

kill -9 3532

Git pull

You have to restart the uwsgi to make you code to take effect.

相關文章
相關標籤/搜索