创建应用的数据库用户django和数据库hello_django:
su - postgres
createuser -P
Enter name of role to add: django
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles (y/n) n
createdb --owner django hello_django
logout
运行django自带的服务器来测试:
cd hello
python manage.py runserver localhost:8000
Validating models...
0 errors found
June 14, 2014 - 03:28:01
Django version 1.6.5, using settings 'hello.settings'
Starting development server at http://localhost:8000/
Quit the server with CONTROL-C.
登录http://localhost:8000 ,可查看到django的欢迎页面。
注解:构建过程中会出现error:IDENT authentication failed for user “django”
出现这个问题是因为PostgreSQL的Client Authentication 使用了ident authentication。
通过修改pg_hba.conf可以解决,编辑/var/lib/pgsql/data/pg_hba.conf,修改为:
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
NAME="hello_app" # Name of the application
DJANGODIR=/django_webapps/hello_django/hello #Django project directory
SOCKFILE=/django_webapps/hello_django/run/gunicorn.sock #unix socket fro communication
USER=django # the user to run as
GROUP=django_webapps # the group to run as
NUM_WORKERS=5 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=hello.settings # which settings file should Django use
DJANGO_WSGI_MODULE=hello.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source ../bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--log-level=debug \
--bind=unix:$SOCKFILE
配置hello应用:vim /etc/supervisord.conf
[program:hello]
command=/django_webapps/hello_django/bin/gunicorn_start ;command to start app
user=django ;user to run as
stdout_logfile=/django_webapps/hello_django/logs/gunicorn_supervisor.log ;path to write log messages
redirect_stderr=true ;save stderr in the same log[program:hello]
command=/django_webapps/hello_django/bin/gunicorn_start ;command to start app
user=django ;user to run as
stdout_logfile=/django_webapps/hello_django/logs/gunicorn_supervisor.log ;path to write log messages
redirect_stderr=true ;save stderr in the same log
#gzip on;
# Nginx virtual server configuration for Django
upstream hello_app_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/django_webapps/hello_django/run/gunicorn.sock fail_timeout=0;
}
server {
location /static/ {
alias /django_webapps/hello_django/static/;
}
location /media/ {
alias /django_webapps/hello_django/media/;
}
location / {
# an HTTP header important enough to have its own Wikipedia entry:
# http://en.wikipedia.org/wiki/X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if and only if you use HTTPS, this helps Rack
# set the proper protocol for doing redirects:
# proxy_set_header X-Forwarded-Proto https;
# pass the Host: header from the client right along so redirects
# can be set properly within the Rack application
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
# set "proxy_buffering off" *only* for Rainbows! when doing
# Comet/long-poll stuff. It's also safe to set if you're
# using only serving fast clients with Unicorn + nginx.
# Otherwise you _want_ nginx to buffer responses to slow
# clients, really.
# proxy_buffering off;
# Try to serve static files from nginx, no point in making an
# *application* server like Unicorn/Rainbows! serve static files.
if (!-f $request_filename) {
proxy_pass http://hello_app_server;
break;
}
}
运行多个Django应用 在同一个Nginx服务器上可以运行多个Django应用,通过不同的域名设置可以登录到不同的url来实现对多个django应用的访问。每一个应用将会设置自己的Python虚拟开发环境,创建专门的运行用户和对应的权限设置、数据库用户和数据库等。
依据前文构建hello项目的步骤可以构建一个jay项目,包括数据库的创建、系统用户的设置、supervisord的设置等等。现在最重要的是Nginx虚拟服务器的设置了:
与前文设置hello_app_server类似,可在nginx配置文件中增加jay_app_server虚拟服务器的设置:
upstream jay_app_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/django_webapps/jay_django/run/gunicorn.sock fail_timeout=0;
}
server {
location /static/ {
alias /django_webapps/jay_django/static/;
}
location /media/ {
alias /django_webapps/jay_django/media/;
}
location / {
# an HTTP header important enough to have its own Wikipedia entry:
# http://en.wikipedia.org/wiki/X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if and only if you use HTTPS, this helps Rack
# set the proper protocol for doing redirects:
# proxy_set_header X-Forwarded-Proto https;
# pass the Host: header from the client right along so redirects
# can be set properly within the Rack application
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
# set "proxy_buffering off" *only* for Rainbows! when doing
# Comet/long-poll stuff. It's also safe to set if you're
# using only serving fast clients with Unicorn + nginx.
# Otherwise you _want_ nginx to buffer responses to slow
# clients, really.
# proxy_buffering off;
# Try to serve static files from nginx, no point in making an
# *application* server like Unicorn/Rainbows! serve static files.
if (!-f $request_filename) {
proxy_pass http://jay_app_server;
break;
}
}