码迷,mamicode.com
首页 > 数据库 > 详细

Nginx+PostgreSQL+Django+UWSGI搭建

时间:2015-01-23 14:41:17      阅读:1111      评论:0      收藏:0      [点我收藏+]

标签:

最近因为项目上的需要开始大量使用nginx,因此也想趁机将以前常用的django+apache的架构换成django+nginx。常见的 django webapp 部署方式采用FCGIWSGI的方式部署,在这里主要对CentOS 6.5下Python 2.7.5环境下采用 Nginx + PostgreSQL + Django + uwsgi 的搭建与配置步骤做一个简要说明,主要留作备忘,也希望对大家有所帮助。

一、Nginx-1.6.2安装

1. 在安装nginx前,需要确保系统安装了g++、gcc、openssl、openssl-devel、pcre、pcre-devel和zlib、zlib-devel软件。安装必须软件:

root@localhost 10:48:14 /usr/local 
=> yum install gcc-c++ 
=> yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

2. 卸载系统原有的nginx

root@localhost 10:57:36 /usr/local 
=> yum remove nginx

3. 编译安装Nginx

root@localhost 10:57:36 /usr/local/download 
=> wget http://nginx.org/download/nginx-1.6.2.tar.gz 
=> tar -zxvf nginx-1.6.2.tar.gz  
=> cd nginx-1.6.2  
=> ./configure --prefix=/usr/local/nginx-1.6.2    
=> make && make install      
=> ln -s /usr/local/nginx-1.6.2/sbin/nginx /usr/bin/nginx

4. 启动Nginx

root@localhost 17:04:38 /usr/local/download/nginx-1.6.2
=> /usr/local/nginx-1.6.2/sbin/nginx

其他nginx常用命令:
=> /usr/local/nginx-1.6.2/sbin/nginx -s reload   # nginx重启(stop, quit, reopen, reload)

5. 测试是否安装成功

#测试端口
    netstat –na|grep 80

#浏览器中测试(有时候ip:80无法显示,需要关闭防火墙的干扰:service iptables stop)
    http://localhost 

# Nginx正常启动:

技术分享

二、PostgreSQL-9.4.0安装

1. 新增用户组及用户

PostgreSQL默认是通过postgres:postgres来启动和使用的,因此在安装PostgreSQL前需要先创建postgres用户组及postgres用户。

root@localhost 11:30:18 ~
=> groupadd postgres
=> useradd postgres -g postgres 
=> passwd postgres                       #设置postgres用户密码

2、安装postgresql-9.4.0

root@localhost 11:35:04 /usr/local/download 
=> wget https://ftp.postgresql.org/pub/source/v9.4.0/postgresql-9.4.0.tar.gz
=> tar zvxf postgresql-9.4.0.tar.gz
=> cd postgresql-9.4.0
=> ./configure --prefix=/usr/local/postgresql-9.4.0
=> make
=> make install

3. PostgreSQL启动 

   # 新建数据库文件保存目录

=> mkdir /usr/local/postgresql-9.4.0/database

   # 新建数据库log文件目录

=> mkdir /usr/local/postgresql-9.4.0/logdb

   # 修改目录拥有者

=> chown postgres:postgres /usr/local/postgresql-9.4.0/database -R
=> chown postgres:postgres /usr/local/postgresql-9.4.0/logdb –R

   # 执行数据库初始化脚本

=> su postgres
[postgres@localhost postgresql-9.4.0]$ /usr/local/postgresql-9.4.0/bin/initdb --encoding=utf8 -D /usr/local/postgresql-9.4.0/database

   # 启动PostgreSQL服务

[postgres@localhost postgresql-9.4.0]$ touch /usr/local/postgresql-9.4.0/log/logfile
[postgres@localhost postgresql-9.4.0]$ /usr/local/postgresql-9.4.0/bin/pg_ctl -D /usr/local/postgresql-9.4.0/database -l /usr/local/postgresql-9.4.0/logdb/logfile start

   # 登录PostgreSQL数据库

[postgres@localhost postgresql-9.4.0]$ psql

技术分享

三、Django-1.6.10安装

1. 源码安装

root@localhost 12:51:56 /usr/local/download 
=> wget https://www.djangoproject.com/m/releases/1.6/Django-1.6.10.tar.gz
=> tar zvxf Django-1.6.10.tar.gz
=> cd Django-1.6.10
=> python setup.py install
2.测试是否安装成功

技术分享

四、uwsgi-2.0.5.1安装

1. 源码安装

root@localhost 13:05:52 /usr/local/download 
=> wget http://projects.unbit.it/downloads/uwsgi-2.0.5.1.tar.gz
=> tar zvxf uwsgi-2.0.5.1.tar.gz
=> cd uwsgi-2.0.5.1
=> python setup.py install
# 该安装,默认将uwsgi安装在了$python的路径下了,我这里安装在了:/usr/local/python2.7/bin/uwsgi

2. 测试是否安装成功

root@localhost 13:13:14 /App/tmp 
=> vi test.py

def application(env, start_response):
    start_response(200 OK, [(Content-Type,text/html)])
    return "Hello Worldi,uwsgi success!"
root@localhost 13:13:48 /App/tmp 
=> uwsgi --http :9090 --wsgi-file /App/tmp/test.py

技术分享

五、Django配置使用PostgreSQL

1. 安装psycopg2

    Python使用PostgreSQL数据库,需要通过psycopg2进行调用,因此Django中使用PostgreSQL,就先应该安装psycopg2模块:

root@localhost 13:30:37 /usr/local/download 
=> wget http://initd.org/psycopg/tarballs/PSYCOPG-2-5/psycopg2-2.5.4.tar.gz
=> tar zvxf psycopg2-2.5.4.tar.gz
=> cd psycopg2-2.5.4
=> python setup.py install

2. 创建Django Project

root@localhost 13:25:53 /App/django-websites 
=> python /usr/local/python2.7/bin/django-admin.py startproject websites

3. 设置Django使用PostgreSQL

root@localhost 13:27:33 /App/django-websites/websites/websites 
=> vi settings.py
…………
DATABASES = {
    default: {
        #ENGINE: django.db.backends.sqlite3,
        #NAME: os.path.join(BASE_DIR, db.sqlite3),
        ENGINE : django.db.backends.postgresql_psycopg2,
        NAME : djangodb,
        USER : django,
        PASSWORD : 123456,
        HOST : localhost,
        PORT : 5432,

    }
}
………

4. 创建后台数据库

shenweiyan@localhost 13:40:17 /App/django-websites/websites 
=> python manage.py syncdb
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session

You just installed Djangos auth system, which means you dont have any superusers defined.
Would you like to create one now? (yes/no): yes     # 输入yes/no
Username (leave blank to use shenweiyanj): django(不输,即默认当前系统用户名)
Email address:    # 邮箱地址,不输的话,可直接enter跳过
Password:  123456    # 密码
Password (again):  123456    # 确认密码
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

六、uWSGI搭配Nginx+Django使用

1. Nginx 配置

   #在 nginx.conf 上加入/修改,我的 server 配置如下(一切从简……):

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        add_header X-Frame-Options SAMEORIGIN;
        add_header X-XSS-Protection "1;mode=block";
        server_tokens off;

        #access_log  logs/host.access.log  main;
        access_log  /usr/local/nginx-1.6.2/logs/access.log;
        error_log  /usr/local/nginx-1.6.2/logs/error.log;
     
        location /static {
alias /usr/local/python2.7/lib/python2.7/site-packages/django/contrib/admin/static/;
        }
        location / {
                uwsgi_pass      127.0.0.1:8011;
                include         uwsgi_params;
        }
        #error_page  404              /404.html;
}

2. uWSGI 配置

   # ini 配置

root@localhost 14:02:56 ~ 
=> mkdir -p /etc/uwsgi/logs
root@localhost 14:02:56 ~ 
=> vi /etc/uwsgi/uwsgi.ini
[uwsgi]
socket=127.0.0.1:8011
listen=128
max-requests=1000
socket-timeout=10
master=true  
pidfile=/etc/uwsgi/logs/uwsgi.pid
processes=1
pythonpath=/App/django-websites/websites/
chdir=/App/django-websites/websites/websites
module=websites.wsgi:application  
profiler=true  
memory-report=true  
enable-threads=true  
logdate=true  
limit-as=256
uid=nobody
gid=nobody
daemonize=/etc/uwsgi/logs/django.log

   # 启动uwsgi

root@localhost 14:06:09 ~ 
=> uwsgi /etc/uwsgi/uwsgi.ini
[uWSGI] getting INI configuration from /etc/uwsgi/uwsgi.ini

  # 每一次Django后台数据库进行修改,均需要重启uwsgi,其重启可通过kill掉/etc/uwsgi/logs/uwsgi.pid,再利用“uwsgi /etc/uwsgi/uwsgi.ini”启动即可。

七、完成安装

   至此,Nginx+PostgreSQL+Django+UWSGI搭建已全部完成,我们在网页浏览器打开:http://localhost/admin/,输入五(4)中设置的用户名(django)和密码(django)登录Django的后台数据库管理界面,即可看到相应的后台数据库信息。

技术分享

Nginx+PostgreSQL+Django+UWSGI搭建

标签:

原文地址:http://www.cnblogs.com/shenweiyan/p/4244131.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!