Nginx+uWSGI+Django+Python 应用架构部署

系统CentOS 6.5

1,安装配置nginx 和Python

yum install nginx

python 默认安装的是2.6 需要升级到2.7见教程

2,django 安装

下载当前最新的版本:Django-1.8.5.tar.gz

$ wget --no-check-certificate https://www.djangoproject.com/download/Django-1.8.5.tar.gz
(备注:--no-check-certificate 免证书认证,不然下载不了https资源)
$ tar zxf Django-1.8.5.tar.gz
$ cd Django-1.8.5
$ python setup.py install
[root@LinuxOT-Test-02 dj17]# python Python 2.7.3 (default, Jan  5 2013, 10:09:00) [GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import django>>> django.VERSION(1, 8, 5, 'final', 0)

说明django安装成功了。

3,新建django项目

[root@LinuxOT-Test-02 nginx]# mkdir www
[root@LinuxOT-Test-02 nginx]# cd www
[root@LinuxOT-Test-02 www]# django-admin startproject mysite
[root@LinuxOT-Test-02 www]# tree mysite
/mysite/
├── manage.py
└── mysite   
├── __init__.py   
├── settings.py   
├── urls.py   
└── wsgi.py1
directory, 5 files
[root@LinuxOT-Test-02 www]# cd mysite/
[root@LinuxOT-Test-02 mysite]# python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.Run 'python manage.py migrate' to apply them.
October 13, 2015 - 09:50:28
Django version 1.8.5, using settings 'mysite.settings'Starting development server at
 Quit the server with CONTROL-C.
[root@LinuxOT-Test-02 mysite]# python manage.py migrate
Operations to perform:  Synchronize unmigrated apps: staticfiles, messages 
Apply all migrations: admin, contenttypes, auth, sessionsSynchronizing apps without migrations:  Creating tables...   
Running deferred SQL... 
Installing custom SQL...
Running migrations:  Rendering model states... DONE 
Applying contenttypes.0001_initial... OK 
Applying auth.0001_initial... OK 
Applying admin.0001_initial... OK 
Applying contenttypes.0002_remove_content_type_name... OK 
Applying auth.0002_alter_permission_name_max_length... OK 
Applying auth.0003_alter_user_email_max_length... OK 
Applying auth.0004_alter_user_username_opts... OK 
Applying auth.0005_alter_user_last_login_null... OK 
Applying auth.0006_require_contenttypes_0002... OK 
Applying sessions.0001_initial... OK
[root@LinuxOT-Test-02 mysite]# python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
October 13, 2015 - 09:51:25Django version 1.8.5, using settings 'mysite.settings'Starting development server at
 Quit the server with CONTROL-C.
[root@LinuxOT-Test-02 dj17]# curl http://127.0.0.1:8000

响应正常

4,安装uwsgi

下载地址http://uwsgi-docs.readthedocs.org/en/latest/Download.html

[
root@LinuxOT-Test-02
 dj17]# wget
http://projects.unbit.it/downloads/uwsgi-2.0.11.2.tar.gz

对于Python/wsgi的支持。

安装uwsgi模块

[root@LinuxOT-Test-02 mysite]# pip install uwsgi
Collecting uwsgi/usr/lib/python2.7/site-packages/pip-7.1.2-py2.7.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see insecureplatformwarning. 
InsecurePlatformWarning 
Downloading uwsgi-2.0.11.2.tar.gz (782kB)   
100% |████████████████████████████████| 782kB 413kB/s Installing collected packages: uwsgi 
Running setup.py install for uwsgi
Successfully installed uwsgi-2.0.11.2
[root@LinuxOT-Test-02 mysite]# pip install
 Collecting http://projects.unbit.it/downloads/uwsgi-lts.tar.gz 
 Downloading http://projects.unbit.it/downloads/uwsgi-lts.tar.gz (782kB)   
 100% |████████████████████████████████| 782kB 22kB/s 
 Requirement already satisfied (use --upgrade to upgrade): uWSGI==2.0.11.2 from http://projects.unbit.it/downloads/uwsgi-lts.tar.gz in /usr/lib/python2.7/site-packages
 
 yum install python-devel2.7
[root@LinuxOT-Test-02 .test]# tar zxf uwsgi-2.0.11.2.tar.gz
[root@LinuxOT-Test-02 .test]# cd uwsgi-2.0.11.2
[root@LinuxOT-Test-02 uwsgi-2.0.11.2]# python uwsgiconfig.py --build
[root@LinuxOT-Test-02 uwsgi-2.0.11.2]# uwsgi --version2.0.11.2
[root@LinuxOT-Test-02 uwsgi-2.0.11.2]# cp uwsgi /usr/sbin/uwsgi

5,关联django到nginx

 vim /etc/nginx/conf.d/default.conf
server {
    listen      80 default_server;
    server_name  _;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:9090;
    }
}
[root@LinuxOT-Test-02 uwsgi-2.0.11.2]# service nginx restart
停止 nginx:                      [确定]
正在启动 nginx:                    [确定]
[root@LinuxOT-Test-02 mysite]# uwsgi -s 127.0.0.1:9090 -w demo

启动项目

访问nginx的地址即可

Django 的详细介绍:请点这里
Django 的下载地址:请点这里

相关推荐