ubuntu14.04下单机安装nginx+uwsgi

django是python的一个web框架,它用来处理后台数据,uwsgi也是一个高效稳定的web server,它用来处理动态请求,而nginx用来处理静态请求以及请求的分发。uWSGI正式部署需要安装在系统范围中,而不是虚拟环境中。

更新源和nginx

apt-get update && apt-get install nginx build-essential libpcre libpcre-devel

ubuntu14.03自带python3.4.3和python2.7.6

apt-get install python3-pip

pip3 install django

创建项目

cd /home && django-admin startproject mysite && chown -R www-data.www-data mysite

安装uwsgi  //顺便安装下监控实例的工具,另外可通过pip3 uninstall uwsgi卸载重装uwsgi

pip3 install uwsgi uwsgitop

创建uwsgi配置文件 //使用命令行启动不太美观

mkdir -p /etc/uwsgi/ && touch /etc/uwsgi/1.ini

cat /etc/uwsgi/1.ini

[uwsgi]

# 指定运行目录
chdir = /home/mysite
# 载入wsgi-file
module          = mysite.wsgi
# the virtualenv (full path)
#home            = /path/to/virtualenv
# 允许主进程存在
master = true
# 指定pid文件的位置,记录主进程的pid号
#pidfile = /run/8001.pid
# maximum number of worker processes
#processes = 1
socket = 127.0.0.1:8001
# run each worker in prethreaded mode with the specified number of threads
#threads = 2
# don't use root
uid = www-data
gid = www-data
# reload workers after the specified amount of managed requests
#max-requests = 2000
# ... with appropriate permissions - may be needed
#chmod-socket = 664
# 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件
vacuum = true
# 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器
daemonize = /home/mysite/8001.log
# 以固定的文件大小(单位Bytes),切割日志文件,100M。
log-maxsize = 104857600
# 当启动时切分日志
#log-truncate = 1
# reopen log after reload
#log-reopen = 1
# monitor python modules mtime to trigger reload (use only in development)
py-autoreload = 1
# enable the stats server on the specified address
stats = /home/mysite/8001.sock
#stats =  127.0.0.1:9001

启动

uwsgi /etc/uwsgi/1.ini 

通过Upstart运行uWSGI,Upstart是类Ubuntu发行版本的init系统。

cat /etc/init/uwsgi.conf 

# simple uWSGI script

description "uwsgi tiny instance"
start on runlevel [2345]
stop on runlevel [06]

respawn

exec uwsgi /etc/uwsgi/1.ini

这里有个问题,就是重启机器后虽然可以开机启动,但是有端口被占用的错误日志???

监控uwsgi性能

 uwsgitop 127.0.0.1:9001 或  uwsgitop /home/mysite/8001.sock

接下来配置nginx

 cat   /etc/nginx/sites-enabled/mysite.conf    

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 www.qbbkj.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     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
                proxy_connect_timeout 600;
                proxy_read_timeout 600;
                proxy_send_timeout 600;
    }
}

nginx -s reload

目前,我们使用了一个TCP端口socket,因为它简单些,但事实上,使用Unix socket会比端口更好 - 开销更少。

没有实战过,看看别人是怎么优化的。https://my.oschina.net/aibati2008/blog/724978

nginx优化

- worker_connections  65535

- worker_rlimit_nofile  65535

- proxy timeout  600

- keepalive_timeout  0

uwsgi配置优化

socket-timeout = 10  # 设置间隔socket超时时间,默认值是4秒,同chunked_read默认超时时间
listen = 65535 # 设置socket监听队列大小。默认值队列是100,超过100个并发就要配置。

调整监听队列只是其中一个原因,不要盲目地以将其设置为巨大值作为提高可用性的方法。

系统内核优化

net.core.somaxconn = 262144
net.core.netdev_max_backlog = 65536
net.ipv4.tcp_max_syn_backlog = 8192

用ab压一下,ab -c 300 -n 70000,没看到nginx错误日志,之前没调优的话还有少量502,error.log为104: Connection reset by peer

 

转载于:https://my.oschina.net/longquan/blog/1635075

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值