Nginx+PostgreSQL+Django+UWSGI搭建

最近因为项目上的需要开始大量使用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 :: /usr/local
=> yum install gcc-c++
=> yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

2. 卸载系统原有的nginx

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

3. 编译安装Nginx

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

4. 启动Nginx

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

5. 测试是否安装成功

#测试端口
    netstat –na|

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

# Nginx正常启动:

Nginx+PostgreSQL+Django+UWSGI搭建

二、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 :: /usr/local/download
=> wget https://ftp.postgresql.org/pub/source/v9.4.0/postgresql-9.4.0.tar.gz
=> .tar.gz
=> cd postgresql-
=> ./configure --prefix=/usr/local/postgresql-
=> make
=> make install

3. PostgreSQL启动

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

=> /database

# 新建数据库log文件目录

=> /logdb

# 修改目录拥有者

=> /database -R
=> /logdb –R

# 执行数据库初始化脚本

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

# 启动PostgreSQL服务

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

# 登录PostgreSQL数据库

[postgres@localhost postgresql-]$ psql

Nginx+PostgreSQL+Django+UWSGI搭建

三、Django-1.6.10安装

1. 源码安装

root@localhost :: /usr/local/download
=> wget https://www.djangoproject.com/m/releases/1.6/Django-1.6.10.tar.gz
=> .tar.gz
=> cd Django-
=> python setup.py install

2.测试是否安装成功

Nginx+PostgreSQL+Django+UWSGI搭建

四、uwsgi-2.0.5.1安装

1. 源码安装

root@localhost :: /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 :: /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 :: /App/tmp
=> uwsgi --http : --wsgi-file /App/tmp/test.py

Nginx+PostgreSQL+Django+UWSGI搭建

五、Django配置使用PostgreSQL

1. 安装psycopg2

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

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

2. 创建Django Project

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

3. 设置Django使用PostgreSQL

root@localhost :: /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',
        ',
        'HOST' : 'localhost',
        ',

    }
}
………

4. 创建后台数据库

shenweiyan@localhost :: /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 Django's auth system, which means you don't 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   fixture(s)

六、uWSGI搭配Nginx+Django使用

1. Nginx 配置

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

server {
        listen       ;
        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-/logs/access.log;
        error_log  /usr/local/nginx-/logs/error.log;
     
        location /static {                alias /usr/local/python2.7/lib/python2.7/site-packages/django/contrib/admin/static/;        }
        location / {
                uwsgi_pass      ;
                include         uwsgi_params;
        }
        #error_page                /.html;
}

2. uWSGI 配置

# ini 配置

root@localhost :: ~
=> mkdir -p /etc/uwsgi/logs
root@localhost :: ~
=> vi /etc/uwsgi/uwsgi.ini
[uwsgi]
socket=
listen=
max-requests=
socket-timeout=
master=true
pidfile=/etc/uwsgi/logs/uwsgi.pid
processes=
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=
uid=nobody
gid=nobody
daemonize=/etc/uwsgi/logs/django.log

# 启动uwsgi

root@localhost :: ~
=> 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搭建

上一篇:Java15变量竟然没什么区别,八大基本数据类型你知道吗?


下一篇:JAVA15.JDK15.6 Record二次预览特性