django+vue+nginx+uwsgi部署,保证能用

服务器系统:Ubuntu(constos系统应该也可以使用,不管啥系统,主要是配置文件)
根据配置文件进行适当修改,确保能够自己使用,如有特殊需求,请自行官网查看!
uwsgi配置文件

[uwsgi]
# django项目地址(项目的最外层文件,也就是app的上一层目录)
chdir = /home/www/map/MicPie
# django中的wsgi文件(项目的内层文件中的wsgi,与settings同级)
module = MicPie.wsgi
# python虚拟环境位置(没有可以不需要这行)
home = /root/.virtualenvs/map_django
#启动主进程,来管理其他进程,其它的uwsgi进程都是这个master进程的子进程,如果kill这个master进程,相当于重启所有的uwsgi进程。
master = True
# 最大进程数
processes = 4
# 线程数
# threads = 2
# 指定root权限
uid=root
gid=root
# http请求端口
http = :8000
# 通过socket访问
socket = /home/www/map/micpie_uwsgi/micpie.sock
# 指定pid文件(自己创建,使用绝对路径),可以用来管理uwsgi
pidfile = /home/www/map/micpie_uwsgi/master.pid
# uwsgi的log日志文件(自己创建,使用绝对路径)
daemonize = /home/www/map/micpie_uwsgi/micpie.log
# 设置用于uwsgi包解析的内部缓存区大小为64k。默认是4k
buffer-size = 65536
 #当服务器退出的时候自动删除unix socket文件和pid文件。
vacuum = True

nginx配置文件

server {
	# 监听80端口
	listen 80;
	charset	utf-8;
	client_max_body_size	75M;
	# 监听地址,写ip或者域名
	server_name	xxx.xxx.xxx.xxx;
	# 从80端口访问后,访问下面地址或者静态文件(比如打包后的vue)
	location / {
		proxy_set_header Host $http_host;
       	proxy_set_header X-Real-IP $remote_addr;
       	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       	proxy_set_header X-Forwarded-Proto https;
		root	/home/www/map/MicPie/dist;
		# 如果vue中router是history,这里一定要写
		try_files $uri $uri/	/index.html;
	}
	# 访问后台,转发到uwsgi的http或者socket
	location /admin {
		root html/www;
        proxy_set_header Host $http_host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        rewrite ^/admin/(.*)$ /$1 break;  #重写,当访问80/admin时会转发后端,根据自身后端路由,进行重定向转发
        # 写法一,通过sock访问
        include uwsgi_params;
        uwsgi_pass unix:/home/www/map/micpie_uwsgi/micpie.sock;
		# 写法二,通过http访问,这里的IP是uwsgi中http绑定的端口
		# proxy_pass	http://xxx.xxx.xxx.xxx:8000/;
	}
	# 静态资源
	location /static {
		alias	/home/www/map/MicPie/static;
	}
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
       	return 404;
    }
}

部署项目最主要的就是配置文件,当配置文件搞定之后,其它就会显得非常简单,需要注意以下几点:
1、uwsgi是通过python安装,不是通过服务器apt安装
2、nginx中转发后端端口需与uwsgi中http端口一致,或直接使用sock访问,不需要每次修改转发端口
3、nginx的配置文件,你可以放到任意地方,但是,一定要将此文件跟nginx做软连接,软连接命令及位置

ln -s /自己文件的位置绝对路径 /etc/nginx/sites-enabled/短连接文件名(最好跟自己nginx配置文件名一样)
举例:
ln -s /home/www/nginx.conf /etc/nginx/sites-enabled/nginx.conf

4、删除掉sites-enabled文件夹中的default文件,避免不必要的麻烦
会用到的命令

nginx命令
/etc/init.d/nginx start	#启动
/etc/init.d/nginx restart	#重启
uwsgi命令
uwsgi --ini 自己编写的uwsgi的配置文件

lsof -i:8000(uwsgi启动的端口)	# 可以看到uwsgi的父进程,kill后其它子进程也会被kill掉

你可能感兴趣的:(Ubuntu,Python,ubuntu,django,vue,nginx,uwsgi)