This article describes how to deploy the Pyhton Django framework project under MacOS, and how to deploy the Django application using gunicorn and Nginx. For more information, see
1. install the software package and create a project
$ Sudo pip install django $ sudo python-c "import django; print django. VERSION "(1, 7, 0, 'final', 0) $ sudo django-admin startproject cmdb # create a project $ sudo django-admin startapp cmdb # create an application
Ii. modify configurations
1. Modify settings. py to add the cmdb application and other settings.
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'cmdb',)DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'cmdb', 'USER': 'cmdb', 'PASSWORD': 'cmdb', 'HOST': 'localhost', 'PORT': '3306', }}LANGUAGE_CODE = 'zh-cn'TIME_ZONE = 'Asia/Shanghai'
2. Modify urls. py and views. py.
The contents of urls. py are as follows:
from django.conf.urls import patterns, include, urlfrom django.contrib import adminurlpatterns = patterns('', # Examples: # url(r'^$', 'cmdb.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^admin/', include(admin.site.urls)), url(r'^index/','cmdb.views.index'),)
The contents of views. py are as follows:
from django.shortcuts import renderfrom django.http import HttpResponsedef index(req): return HttpResponse('hello welcome to django!')
3. Test
Start django
#sudo python manage.py runserver
Access:
Http: // localhost: 8000/index
PS: gunicorn combined with nginx to deploy django applications
Note: The django program is deployed in gunicorn. The front-end uses nginx to process server requests. Static resources are processed directly and dynamic resources are forwarded to the backend.
Directory structure:
cmdb/├── cmdb│ └── migrations├── device_manage├── idcroom_manage├── operation│ └── migrations└── static └── admin ├── css ├── img │ └── gis └── js └── admin
1. Install gunicorn and django
pip install gunicornpip install django
2. Install MySQLdb
wget https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zipcd MySQL-python-1.2.5python setup.py install
3. Start the django program with gunicorn
[root@backup cmdb]# gunicorn --versiongunicorn (version 19.1.1)gunicorn cmdb.wsgi:application --bind=127.0.0.1:8000 --daemon
Gunicorn parameters:
-Bind: Specify the listening address.
-Run daemon on the background.
More parameters: gunicorn-help
Nginx reverse proxy:
server { listen 8080; server_name 192.168.3.21; location / { proxy_pass http://127.0.0.1:8000; proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; proxy_set_header X-Forwared-For $proxy_add_x_forwarded_for ; proxy_set_header Host $http_host ; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; } location /static {alias /opt/wwwroot/cmdb/static; } access_log logs/cmdb.access.log; }