Table of Contents
hello welcome to django!
Home Backend Development Python Tutorial 解析Mac OS下部署Pyhton的Django框架项目的过程

解析Mac OS下部署Pyhton的Django框架项目的过程

Jun 10, 2016 pm 03:05 PM
django mac nginx

一、安装软件包并创建项目

$sudo pip install django
$sudo python -c "import django;print django.VERSION"
(1, 7, 0, 'final', 0)
$sudo django-admin startproject cmdb #创建项目
$sudo django-admin startapp cmdb #创建应用

Copy after login

二、修改配置
1、修改settings.py,添加cmdb应用,以及其他设置

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'

Copy after login

2、修改urls.py和views.py
urls.py内容如下:

from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = 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'),
)
Copy after login

views.py内容如下:

from django.shortcuts import render
from django.http import HttpResponse
def index(req):
 return HttpResponse('<h1 id="hello-welcome-to-django">hello welcome to django!</h1>')
Copy after login

3、测试
启动django

#sudo python manage.py runserver
Copy after login

访问:
http://localhost:8000/index

PS:gunicorn结合nginx来部署django应用
说明:gunicorn部署django程序,前端用nginx处理服务器请求,静态资源直接处理,动态资源转发到后端。

目录结构:

cmdb/

├── cmdb

│  └── migrations

├── device_manage

├── idcroom_manage

├── operation

│  └── migrations

└── static

  └── admin

    ├── css

    ├── img

    │  └── gis

    └── js

      └── admin

Copy after login

1、安装gunicorn和django

pip install gunicorn
pip install django
Copy after login

2、安装MySQLdb

wget https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip
cd MySQL-python-1.2.5
python setup.py install
Copy after login

3、用gunicorn启动django程序

[root@backup cmdb]# gunicorn --version
gunicorn (version 19.1.1)
gunicorn cmdb.wsgi:application --bind=127.0.0.1:8000 --daemon
Copy after login

gunicorn参数:

–bind指定侦听地址

–daemon放到后台运行

更多参数:gunicorn –help

nginx反向代理:

 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;
  }
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

What to do if nginx server is hung What to do if nginx server is hung Apr 14, 2025 am 11:42 AM

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.

See all articles