Nginx+Gunicorn

1.Nginx

安装

sudo apt-get install nginx

启动,停止和重启

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart

or

sudo service nginx start
sudo service nginx stop
sudo service nginx restart

配置nginx

编辑/etc/nginx/sites-enabled/default 配置监听端口,配置静态文件

vim /etc/nginx/sites-enabled/default
upstream django1 { 
     server unix:///home/www/company/company.sock; # for a file socket
     #server 127.0.0.1:8011; # for a web port socket (we'll use this first)
}
# configuration of the server

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=off;

    location / {
        proxy_pass http://your IP:8000;#change your port
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /static {
        alias /your project path/static/; # your Django project's static files - amend as required
    }
    location /media  {
        alias /your project path/media;  # your Django project's media files - amend as required
    }

}

注意在一台服务器上部署多个项目时 要注意

测试配置文件:

nginx -t

重启服务:

sudo service nginx restart
python manage.py collectstatics 

Gunicorn

安装

pip install gunicorn

gunicorn 的简单用法:

gunicorn code:application

在多核处理器上同时启动8个进程处理HTTP请求

 gunicorn -w 8 code:applcation

django +gunicorn:

INSTALLED_APPS = ( 
    'django.contrib.admin',
    'django.contrib.auth',  
    'django.contrib.contenttypes', 
    'django.contrib.sessions',  
    'django.contrib.messages',
    'django.contrib.staticfiles', 
    # add app 
    'yourapp',
    'gunicorn',
 )

在你得项目文件下运行:

$ nohup gunicorn project.wsgi:application -b 127.0.0.1:1010&

nohup 可以指定重定向文件 nohup command > myout.file 2>&1 &
然后用tail查看nohup.out,没有任何信息就对了

你可能感兴趣的:(Nginx+Gunicorn)