前言
自己通过nginx uwsgi 部署django项目,查询了很多资料,遇到了很多问题,最终完成了部署,趁着心情愉悦,写个随笔,为曾像我一样苦寻解决方案的小伙伴们提供些思路。
方法如下
安装Nginx:
#安装nginxsudo apt-get install nginx#一些有用的命令#启动nginxsudo /etc/init.d/nginx start #重启nginx 8sudo /etc/init.d/nginx restart#停止nginxsudo /etc/init.d/nginx stop#很暴力的方法,我喜欢sudo killall nginx
安装uwsgi:
pip install uwsgi #注意uwsgi需要在虚拟环境中运行
配置uwsgi:
#在项目目录中建立个conf文件夹,将nginx和uwsgi文件都放进去,不是必须#但是个好习惯#my_uwsgi.iniite_uwsgi.ini file[uwsgi]# Django-related settings# the base directory (full path)chdir = /to/your/project/#这个是项目的路径# Django's wsgi filemodule = project.wsgi#这个project要换成自己的项目名,也就是uwsgi.py所在的文件夹名# the virtualenv (full path)home = /home/ubuntu/.virtualenvs/虚拟环境名#这个就是虚拟环境的路径# process-related settings# mastermaster = true# maximum number of worker processesprocesses = 10# the socket (use the full path to be safesocket = 127.0.0.1:8080#这个用来和Nginx对接,端口号可以改,本人项目将uwsgi作为本地服务,外网不能直接访问,用nginx作为代理,所以用本地的地址。# ... with appropriate permissions - may be needed# chmod-socket = 664# clear environment on exitvacuum = true~
配置nginx
#以下内容在mysite_nginx.conf中,这个文件名也可以随意起# mysite_nginx.conf# the upstream component nginx needs to connect toupstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8080; #这个是用来跟uwsgi对接的,要和my_uwsgi.ini中写一致}# configuration of the serverserver { # the port your site will be served on listen 8000;#这个端口是nginx用来监听uwsgi的,默认的是80,总之项目是通过下面的server_name:8000来访问的 # the domain name it will serve for server_name xxx.xxx.xx.xx ; #这个ip就是服务器的ip charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias /your/project/media; #这个目录是项目的meda目录 } location /static { alias /your/project/static; # 这个目录是项目的static目录 } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django;#这个是对接uwsgi的 include uwsgi_params; # 这个参数按我这样写nginx就能找到的 }}
将nginx配置文件链接到启动配置目录:
#注意修改下面的路径及文件名,哈哈不要只复制粘贴啊sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
新闻热点
疑难解答