nginx+uwsgi
个人推荐使用yum或者编译安装,pip可能出现问题。
博主已经出现了uwsgi假死状态,正愁人呢。
修改pip使用国内源
yum install epel-release –y mkdir ~/.pip cat > ~/.pip/pip.conf <<EOF [global] index-url = http://pypi.douban.com/simple trusted-host = pypi.douban.com EOF
升级pip
pip3 install --upgrade pip
安装uwsgi
pip3 install uwsgi
安装nginx
#1.安装依赖包 yum install -y pcre-devel openssl-devel #2.下载源码包 wget http://nginx.org/download/nginx-1.12.1.tar.gz #3.创建www用户 useradd -s /sbin/nologin -M www -u 977 #4.编译安装 tar xf nginx-1.12.1.tar.gz cd nginx-1.12.1 ./configure --user=www --group=www --prefix=/usr/local/nginx-1.12.1 --with-http_stub_status_module --with-http_ssl_module make make install # 创建软连接 ln -s /usr/local/nginx-1.12.1/ /usr/local/nginx
uwsgi配置文件,使用用户是www,跟nginx统一
/etc/uwsgi.ini
cat >/etc/uwsgi.ini <<EOF [uwsgi] socket = 127.0.0.1:9090 uid = www master = true vhost = true no-stie = true workers = 2 reload-mercy = 10 vacuum = true max-requests = 1000 limit-as = 512 buffer-sizi = 30000 pidfile = /var/run/uwsgi.pid daemonize = /var/log/logs/uwsgi.log EOF
nginx配置文件
worker_processes auto; worker_rlimit_nofile 65535; events { worker_connections 10240; } http { # 压缩 gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/css application/javascript application/json; gzip_vary on; # transfor tcp_nopush on; tcp_nodelay on; # 打开文件的缓存 open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; server_tokens off; include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { uwsgi_cache_valid 1m; uwsgi_temp_file_write_size 64k; uwsgi_busy_buffers_size 64k; uwsgi_buffers 8 64k; uwsgi_buffer_size 64k; uwsgi_read_timeout 300; uwsgi_send_timeout 300; uwsgi_connect_timeout 300; listen 33334; server_name localhost; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9090; uwsgi_param UWSGI_SCRIPT luffy.wsgi; uwsgi_param UWSGI_CHDIR /usr/local/nginx/html/uwsgi; index index.html index.htm; client_max_body_size 35m; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.(png|jpg|jpeg|css|img|js|flv|swf|download|eot|svg|ttf|woff|woff2|otf)$ { root html/code_dev; } } }
uwsgi启动文件
DESC="uwsgi daemon" NAME=uwsgi DAEMON=/usr/local/python/bin/uwsgi CONFIGFILE=/etc/$NAME.ini PIDFILE=/var/run/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME set -e [ -x "$DAEMON" ] || exit 0 do_start() { $DAEMON $CONFIGFILE || echo -n "uwsgi running" } do_stop() { $DAEMON --stop $PIDFILE || echo -n "uwsgi not running" rm -f $PIDFILE echo "$DAEMON STOPED." } do_reload() { $DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload" } do_status() { ps aux|grep $DAEMON } case "$1" in status) echo -en "Status $NAME: \n" do_status ;; start) echo -en "Starting $NAME: \n" do_start ;; stop) echo -en "Stopping $NAME: \n" do_stop ;; reload|graceful) echo -en "Reloading $NAME: \n" do_reload ;; *) echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2 exit 3 ;; esac exit 0
测试
pip install django
安装完之后,拿你们的项目启动吧,首先要在
python manage.py runserver 能启动的情况下启动,不然你会报错的莫名其妙的。
项目存放位置
/usr/local/nginx/html/uwsgi
补充:
nginx的uwsgi参数官网:
http://nginx.org/en/docs/http/ngx_http_uwsgi_module.html
uwsgi的文档参考
http://uwsgi-docs.readthedocs.io/en/latest/ 英文文档
http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/Caching.html#id1 中文文档
你们会发现,css样式等内容居然不见啦,不要着急,百度一下即可。
此处乃是一个坑。
当然,此处也有简单的方法。
settings配置一个参数
STATIC_ROOT = os.path.join(BASE_DIR, 'static',)
这个目录不能和STATICFILES_DIRS一样
然后使用
python3 manage.py collectstatic # 进行拉取静态页面。
最后
nginx配置文件
location ~ \.(png|jpg|jpeg|css|img|js|flv|swf|download|eot|svg|ttf|woff|woff2|otf)$ { root html/uwsgi; }
对啦,这里又出现一个坑了。这里指定了root之后,上面就要用绝对路径,或者直接注释掉。
uwsgi_param UWSGI_CHDIR /usr/local/nginx/html/uwsgi;