CentOS Nginx+python+fastcgi+postgres Deployment Summary (Django Edition)

Source: Internet
Author: User
Tags psql nginx server postgres database

Recently, because of the needs of the project to start a large number of nginx, so also want to take the opportunity to use the previous Django+apache architecture to replace the Django+nginx. The common Django WebApp deployment approach is FCGI WSGI deployed in a way that uses or deploys the setup and configuration steps of the Nginx + fastcgi + Python + Django + PostgreSQL in CentOS 6.5 to make a simple It is important to note that the main reserve is to be remembered, and we hope to help you.

One, PostgreSQL, Django, Nginx installation

Postgres, Django, nginx installation is not described here in detail, need to install can go to Baidu, Google a bit.

Second, PostgreSQL database configuration

1. Create a new User: Createuser-p user name

= Su Postgres
Bash-4.1- for new Role:enter It again:shall the new role is a superuser? (Y /  tocreate databases? (Y/tocreate more new roles?) (Y/N) n
2. Create a new database for the new user: Createdb database name-o user name
Bash-4.1-O djangobash-4.1$ psql psql (8.4.  for help.postgres =# \l 

Second, PostgreSQL + Django Configuration

1. Create a Django Project

All::/app/django- and  python/usr/bin/django-: :/app/django- = tree websites/websites/├──manage.py└──websites    ├──__init__.py    ├──__init__.pyc    ├──settings.py    ├──settings.pyc    ├──urls.py    ├── Urls.pyc    ├──wsgi.py    └──wsgi.pyc19 files

2, Configuration Django+postgres

[Email protected] One: -: the/app/django-websites/websites/Websites=VI settings.py"""Django settings for websites project. For more information on the this file, Seehttps:docs.djangoproject.com/en/1.6/topics/settings/For the full list of settings andtheir values, Seehttps:docs.djangoproject.com/en/1.6/ref/settings/"""# Build paths inside the project like This:os.path.join (Base_dir, ...) Import Osbase_dir=Os.path.dirname (Os.path.dirname (__file__)) # Quick-start Development Settings-unsuitable for production# see https:docs.djangoproject.com/en/1.6/howto/deployment/checklist/# SECURITY Warning:keep The secret key used in production secret! Secret_key='tpq!izqrqcr#[email protected]#[email protected]^p*!0ww&[email protected]%c#u0if'# SECURITY Warning:don't run with debug turned on production! DEBUG = Truetemplate_debug = Truetemplate_dirs = (  '/app/django-websites/templates/'  ,) #设置Django模板路径ALLOWED_HOSTS = []# Application Definitioninstalled_apps = ('Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'Django.contrib.sessions',    'Django.contrib.messages',    'Django.contrib.staticfiles',) Middleware_classes = ('Django.contrib.sessions.middleware.SessionMiddleware',    'Django.middleware.common.CommonMiddleware',    'Django.middleware.csrf.CsrfViewMiddleware',    'Django.contrib.auth.middleware.AuthenticationMiddleware',    'Django.contrib.messages.middleware.MessageMiddleware',    'Django.middleware.clickjacking.XFrameOptionsMiddleware',) root_urlconf ='Websites.urls'wsgi_application ='Websites.wsgi.application'# database# https://docs.djangoproject.com/en/1.6/ref/settings/#databasesDATABASES = {'Default': {        #'ENGINE': 'Django.db.backends.sqlite3',        #'NAME': Os.path.join (Base_dir,'Db.sqlite3'),        'ENGINE' : 'Django.db.backends.postgresql_psycopg2', # Django uses the Postgres database'NAME': 'Djangodb', # database name'USER': 'Django', # database owner'PASSWORD': '123456', # Database Password'HOST' : 'localhost',        'PORT' : "'}}# internationalization# Https://docs.djangoproject.com/en/1.6/topics/i18n/LANGUAGE_CODE ='En-US'Time_zone ='Utc'use_i18n = trueuse_l10n = Trueuse_tz = true# Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en /1.6/howto/static-files/static_url ='/static/'

3. Synchronize background database

:panax notoginseng /app/django-websites/+SQL0  Object 0 Fixture (s)

Third, Nginx + Django + fastcgi configuration

1. Installing the Flup module

Nginx has integrated the mod_fastcgi by default, so install the Flup before you configure it. CentOS can be installed directly under the command:

Yum Install Python-flup

2. Modify the Nginx configuration file:

Server {Listen the; server_name172.16.43.35; Add_header X-frame-Options Sameorigin; Add_header X-xss-protection"1;mode=block";        Server_tokens off; # nginx Default CSS, JavaScript, images point to location/Static {alias/usr/lib/python2.6/site-packages/django/contrib/admin/static/; } Location/{Fastcgi_pass127.0.0.1:8051;            Fastcgi_param path_info $fastcgi _script_name;            Fastcgi_param Request_method $request _method;            Fastcgi_param query_string $query _string;            Fastcgi_param Content_Type $content _type;            Fastcgi_param content_length $content _length;            Fastcgi_param remote_addr $remote _addr;            Fastcgi_param server_protocol $server _protocol;            Fastcgi_param server_port $server _port;            Fastcgi_param server_name $server _name;            Fastcgi_pass_header Authorization;        Fastcgi_intercept_errors off; }
}

3. To the project directory, run:

Python manage.py runfcgi method=threaded host=127.0. 0.1 port=8051

4. Restart the Nginx server

=/usr/local/nginx/sbin/nginx-s Reload

5. Access http://localhost/admin/, (for example, the personal machine IP for this deployment is 172.16.43.35, i.e. access: http://172.16.43.35/admin/) The login page (bottom left) of the Django backend database appears, and the Djangodb page (bottom right) after logging in:

At this point, the installation of Nginx+python+fastcgi+postgres under CentOS has been completed, and then you can build your own Django app!

CentOS Nginx+python+fastcgi+postgres Deployment Summary (Django Edition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.