centos6.7+nginx+uwsgi+django

来源:互联网 发布:万龙滑雪场 知乎 编辑:程序博客网 时间:2024/05/21 01:53

参考:http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html


1. 基本依赖包安装:

yum groupinstall "Development tools" -y
yum install zlib-devel bzip2-devel pcre-devel openssl-devel python-devel ncurses-devel mysql-devel sqlite-devel readline-devel tk-devel -y


2. 安装python2.7.11   
tar -zxvf Python-2.7.tgz
cd Python-2.7.11
./configure --enable-shared --prefix=/usr 
make
make install


## delete original link
rm /usr/bin/python
##create new link
ln -s /usr/bin/python2.7 /usr/bin/python
##create the file
vim /etc/ld.so.conf.d/python2.7.conf
##add the content
/usr/lib
##run the command
ldconfig   
   
3. 下载并安装setuptools
python setup.py install
   
4. 安装pip
python setup.py install
   
5. 安装django1.8.17
pip install django==1.8.17
   
6. 安装uwsgi
pip install uwsgi
   
7. 安装nginx
./configure  --with-http_stub_status_module  --with-http_gzip_static_module
make
make install
   
8. 测试uwsgi是否正常工作
创建测试文件test.py
# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
    #return ["Hello World"] # python2
  
运行以下命令,并访问http://127.0.0.1:8000
uwsgiuwsgi --http :8000 --wsgi-file test.py
   
9. 配置uwsgi+django
用django-admin命令创建mysite站点
django startproject mysite


测试django工作是否正常,运行djago,访问http://127.0.0.1:8000
python manage.py runserver 0.0.0.0:8000


测试django+uwsgi工作是否正常,运行以下命令并访问http://127.0.0.1:8000
uwsgi --http :8000 --module mysite.wsgi




10. 配置uwsgi+django+nginx
配置ningx.conf文件


# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}


# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;


    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /usr/local/nginx/conf/uwsgi_params; # the uwsgi_params file you installed
    }
}






**********以端口8001启动uwsgi加载测试文件test.py文件,访问http://127.0.0.1
uwsgi --socket :8001 --wsgi-file test.py --chmod-socket=666


启动nginx
/usr/local/nginx/sbin/nginx


**************以sock方式启动uwsgi加载测试文件test.py文件,访问http://127.0.0.1
在/home创建mysite.sock空文件,并更改nginx.conf文件
upstream django {
    server unix:///home/mysite.sock; # for a file socket
    #server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}


重新启动nginx


uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 


在/home/mysite目录下运行以下命令,并访问http://127.0.0.1
uwsgi --socket /home/mysite.sock --module mysite.wsgi --chmod-socket=666




****配置mysite_ningx.conf和mysite_uwsgi_ini文件
step1:nginx.conf的http配置中添加
include /usr/local/nginx/conf/mysite_nginx.conf;


step2:并创建mysite_nginx.conf
# mysite_nginx.conf


# the upstream component nginx needs to connect to
upstream django {
    server unix:///home/mysite.sock; # for a file socket
    #server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}


# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;


    # max upload size
    client_max_body_size 75M;   # adjust to taste


    # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }


    location /static {
        alias /path/to/your/mysite/static; # your Django project's static files - amend as required
    }


    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /usr/local/nginx/conf/uwsgi_params; # the uwsgi_params file you installed
    }
}


step3:更改nginx.conf文件的listen的默认80端口改为8000,避免和mysite_nginx.conf监听端口重复


step4:创建/etc/uwsgi/mysite_uwsgi_ini


# mysite_uwsgi.ini file
[uwsgi]


# Django-related settings
# the base directory (full path)
chdir           = /home/mysite
# Django's wsgi file
module          = mysite.wsgi
# the virtualenv (full path)
#home            = /path/to/virtualenv


# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /home/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true


step5: 在django 的setting.py文件配置allowhost,如

ALLOWED_HOSTS = ['172.17.*.*']


step6: 运行uwsgi和重启nginx
uwsgi -i mysite_uwsgi.ini
/usr/local/nginx/sbin/nginx




   
#杀掉当前端口的进程
fuser -k 80/tcp
#杀掉当前占用的yum进程
rm -rf /var/run/yum.pid
0 0
原创粉丝点击