手机版
你好,游客 登录 注册
背景:
阅读新闻

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

[日期:2015-10-15] 来源:Linux社区  作者:aolens [字体: ]

系统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的地址即可

如何使用 Docker 组件开发 Django 项目?  http://www.linuxidc.com/Linux/2015-07/119961.htm

Ubuntu Server 12.04 安装Nginx+uWSGI+Django环境 http://www.linuxidc.com/Linux/2012-05/60639.htm 

Django+Nginx+uWSGI 部署 http://www.linuxidc.com/Linux/2013-02/79862.htm 

Django实战教程 http://www.linuxidc.com/Linux/2013-09/90277.htm 

Django Python MySQL Linux 开发环境搭建 http://www.linuxidc.com/Linux/2013-09/90638.htm 

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

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-10/124183.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款