bolg Django uWSGI+Nginx 项目之一

从零开始纪录学习GeekBlog博客项目的过程:
多版本管理的环境配置:
virtualenv:

virtualenv blog
cd blog 
source bin/activate

安装django1.6.5至blog环境中,并创建blog项目

pip install Django
django-admin.py startproject blog
cd blog

或用eclipse Pydev
建立Django项目

基本uWSGI安装与配置
在虚拟环境中安装

pip install uwsgi

安装nginx

sudo apt-get install nginx
sudo /etc/init.d/nginx start    # start nginx

拷贝 uwsgi_params 文件;
可从这两处 nginx directory of the uWSGI distribution, 或从 https://github.com/nginx/nginx/blob/master/conf/uwsgi_params

创建新的文件在blog工程目录下 blog_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    server unix:///var/run/blog.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      8000;
    # the domain name it will serve for
    server_name 192.168.27.130; # 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 /root/git/blog/blog/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /root/git/blog/blog/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     /root/git/blog/blog/uwsgi_params; # the uwsgi_params file you installed
    }
}

Symlink to this file from /etc/nginx/sites-enabled so nginx can see it:

sudo ln -s /root/git/blog/blog/blog_nginx.conf /etc/nginx/sites-enabled/

Deploying static files

Before running nginx, you have to collect all Django static files in the static folder. First of all you have to edit mysite/settings.py adding:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

and then run

python manage.py collectstatic

Restart nginx:

sudo /etc/init.d/nginx restart

Running the Django application with uwsgi and nginx

uwsgi --socket blog.sock --module blog.wsgi --chmod-socket=664

Configuring uWSGI to run with a .ini file

We can put the same options that we used with uWSGI into a file, and then ask uWSGI to run with that file. It makes it easier to manage configurations.

Create a file called blog_uwsgi.ini:

# blog_uwsgi.ini file
[uwsgi]

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

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

And run uswgi using this file:

uwsgi --ini blog_uwsgi.ini 
# the --ini option is used to specify a file

Once again, test that the Django site works as expected.
这里写图片描述
到此基本环境配置结束。。。接着着手。models

遇见的错误:

  1. nginx 启动失败,解决:查看配置错误原因,
nginx -t 
  1. Got ‘No such file or directory’ error while configuring nginx and uwsgi
    项目配置文件生成 blog.sock 将其移动至 /var/run/blog.rock
    参考:http://stackoverflow.com/questions/32974204/got-no-such-file-or-directory-error-while-configuring-nginx-and-uwsgi
  2. nginx权限问题
    暂时用root,安全问题以后再学习。。
    在nginx.conf中,第一行 改写为user root

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值