先安装uWSGI :
pip install uwsgi测试测试是否安装成功:$ uwsgi --version
2.0.15测试文件:# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2运行uwsgi:uwsgi --http :8000 --wsgi-file test.py
如果想要运行项目来测试
# uwsgi --http :8000 --chdir 项目路径 -w 项目.wsg --static-map=/static=static
uWSGI常用命令:
uwsgi --chdir=/path/to/your/project \ --module=mysite.wsgi:application \ --env DJANGO_SETTINGS_MODULE=mysite.settings \ --master --pidfile=/tmp/project-master.pid \ --socket=127.0.0.1:49152 \ # 可以ip地址,也可以是文件 --processes=5 \ # 进程数量 --uid=1000 --gid=2000 \ # 如果是root用户,uwsgi可以有删除权限 --harakiri=20 \ # 一个请求超时时间 --max-requests=5000 \ # 一个工作进程最大请求数 --vacuum \ # 退出时清楚环境 --home=/path/to/virtual/env \ # virtualenv的路径 -- static # 做一个映射,指定静态文件 --http # 这个就和runserver一样指定IP 端口 --daemonize=/var/log/uwsgi/yourproject.log # 日志
配置项目启动文件uwsgi.ini
在项目根目录创建uwsgi.ini文件[uwsgi]# 指定IP端口
http=127.0.0.1:8000
#socket=/home/www/work/project/pro/nginx_uwsgi.socket#socket套接字,与uwsgi通信
socket = 127.0.0.1:8001#项目目录
chdir=/home/EdmureBlog/
chmod-socket=664# 启用主进程
master=true进程数
processes=4线程数
threads=2# 指定项目的application
module=EdmureBlog.wsgi
#wsgi-file=uwsgi_test.py
#stats=127.0.0.1:9000# 自动移除unix Socket和pid文件当服务停止的时候
#vacuum = true启动配置$ uwsgi --ini uwsgi.ini # 启动uwsgi配置
[uwsgi-static] added mapping for /static => /home/EdmureBlog//static # 启动成功
$ uwsgi --stop uwsgi.pid # 关闭uwsgi
signal_pidfile()/kill(): Operation not permitted [core/uwsgi.c line 1659]
$ uwsgi --reload uwsgi.pid #重新加载配置
2.安装NGINX (1)安装NGINX $ sudo apt-get install nginx #安装 是否安装成功 $ ps -ef|grep -i nginx root 6961 1 0 03:56 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; www-data 6962 6961 0 03:56 ? 00:00:00 nginx: worker process pala 6985 2090 0 03:57 pts/0 00:00:00 grep --color=auto -i nginx NGINX常用命令: $ /etc/init.d/nginx start #启动 $ /etc/init.d/nginx stop #关闭 $ /etc/init.d/nginx restart #重启 $ killall nginx 杀死所有nginx # 如果是生产环境的话Nginx正在运行,就不要直接stop start 或者 restart 直接reload就行了 # 对线上影响最低 $ /etc/init.d/nginx reload 配置/etc/nginx/nginx.conf
python manage.py collectstatic