centos7安装和配置uWSGI
安装
使用pip进行安装
pip install uwsgi
创建测试文件
安装好后创建一个测试文件test.py,内容如下:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
测试
进入到test.py文件的目录下运行启动命令
uwsgi --http :8001 --wsgi-file test.py
这时会提示我们uwsgi: 未找到命令…,这就需要我们为uwsgi命令创建一个软链。
uwsgi安装完成后启动文件一般会放到/usr/python/bin文件夹,找到后就可以执行下面的命令来创建软链,之后uwsgi就会成为受系统支持的命令了。
ln -s /usr/python/bin/uwsgi /usr/bin/uwsgi
这个时候再执行启动命令的话就会不在报错了。
启动好了之后可以通过ip+端口来访问程序了。
注:
常用选项:
http:协议类型和端口号
processes:开启的进程数量
workers:开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
chdir:指定运行目录(chdir to specified directory before apps loading)
wsgi-file:载入wsgi-file(load .wsgi file)
stats:在指定的地址上,开启状态服务(enable the stats server on the specified address)
threads:运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
master:允许主进程存在(enable master process)
daemonize:使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
pidfile:指定pid文件的位置,记录主进程的pid号。
vacuum:当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
参考下面的启动命令
uwsgi --http :8001 --chdir /home/fnngj/pydj/myweb/ --wsgi-file myweb/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191