Diango + uwsgi + nginx项目部署的全过程(可外网访问)

yipeiwu_com7年前Python基础

前言

自己通过nginx uwsgi 部署django项目,查询了很多资料,遇到了很多问题,最终完成了部署,趁着心情愉悦,写个随笔,为曾像我一样苦寻解决方案的小伙伴们提供些思路。

方法如下

安装Nginx:

#安装nginx
sudo apt-get install nginx

#一些有用的命令
#启动nginx
sudo /etc/init.d/nginx start 
#重启nginx
 8sudo /etc/init.d/nginx restart
#停止nginx
sudo /etc/init.d/nginx stop

#很暴力的方法,我喜欢
sudo killall nginx

安装uwsgi:

 pip install uwsgi
 
 #注意uwsgi需要在虚拟环境中运行

配置uwsgi:

#在项目目录中建立个conf文件夹,将nginx和uwsgi文件都放进去,不是必须#但是个好习惯

#my_uwsgi.ini
ite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir   = /to/your/project/#这个是项目的路径
# Django's wsgi file
module   = project.wsgi#这个project要换成自己的项目名,也就是uwsgi.py所在的文件夹名
# the virtualenv (full path)
home   = /home/ubuntu/.virtualenvs/虚拟环境名#这个就是虚拟环境的路径

# process-related settings
# master
master   = true
# maximum number of worker processes
processes  = 10
# the socket (use the full path to be safe
socket   = 127.0.0.1:8080#这个用来和Nginx对接,端口号可以改,本人项目将uwsgi作为本地服务,外网不能直接访问,用nginx作为代理,所以用本地的地址。
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum   = true
~

配置nginx

#以下内容在mysite_nginx.conf中,这个文件名也可以随意起
# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream 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 server
server {
 # 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/

修改django项目中的setting.py文件,添加

#要将STATICFILES_DIRS =[os.path.join(BASE_DIR, 'static')]注释掉,Debug在生产模式也要改成False
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

将静态文件打包,让nginx代理:

python manage.py collectstatic

启动nginx,uwsgi

 sudo /etc/init.d/nginx restart
#进入conf文件夹,或者说配置的uwsgi.ini文件所在目录
#uwsgi.ini改成自己的名字
uwsgi -i uwsgi.ini

访问:

ip:port(端口为nginx.conf中配置的)

总结:

写到这也差不多了,项目可以跑起来了,nginx,uwsgi高级配置还在学习中,希望本文对你有所帮助,谢谢。

最后再提醒下,网上有很多配置文件的模板,将我写注释的地方对比修改下,别遗漏。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对【听图阁-专注于Python设计】的支持。

参考文档:https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

     http://uwsgi-docs.readthedocs.io/en/latest/Nginx.html

相关文章

PyCharm更改字体和界面样式的方法步骤

PyCharm更改字体和界面样式的方法步骤

更改主题 File → Settings → Appearance & Behavior → Appearance → Theme 结果: 更改字体大小 File → Sett...

Python 实现Windows开机运行某软件的方法

Python 实现Windows开机运行某软件的方法

开机运行:随系统启动的应用程序,当系统启动之后会自动加载的应用 在注册表中添加启动项便可实现开机启动。 代码如下: # -*- coding:utf-8 -*- import win...

python3安装crypto出错及解决方法

首先我用的python3.5的版本 问题的由来,我想通过python去实现RSA加密算法时,破解某网站的js加密认证,网上说需要安装pycrypto,我就去进行pip安装了 pip...

Python实现身份证号码解析

中国的居民身份证有18位。其中前17位是信息码,最后1位是校验码。每位信息码可以是0-9的数字,而校验码可以是0-9或X,其中X表示10。 身份证校验码算法: 设18位身份证号序列从左到...

matplotlib subplots 设置总图的标题方法

如下所示: matplotlib subplots 设置总图的标题 : fig.suptitle(dname,fontsize=16,x=0.53,y=1.05,) 以上这篇matplo...