django+uwsgi+nginx远程服务器简单部署

 

Step:1 软件

安装nginx,mysql,redis。redis默认配置即可,为了可以远程连接数据库需要进行以下操作(root用户为例):

grant all on root.* to 'root'@'%'; 
flush privileges

修改mysql配置文件:

vim /etc/mysql/mysql.conf.d/mysqld.cnf

将此行注释。

Step:2 uwsgi的配置

在uwsgi.py所在目录下创建uwsgi.ini并编辑

[uwsgi]
#使用nginx连接时使用,Django程序所在服务器地址
socket=127.0.0.1:8001
#直接做web服务器使用,Django程序所在服务器地址
#http=10.211.55.2:8001
#项目目录
chdir=/data/server/项目
#项目中wsgi.py文件的目录,相对于项目目录
wsgi-file=path/wsgi.py
# 进程数
processes=4
# 线程数
threads=2
# uwsgi服务器的角色
master=True
# 存放进程编号的文件
pidfile=uwsgi.pid
# 日志文件,因为uwsgi可以脱离终端在后台运行,日志看不见。我们以前的runserver是依赖终端的
daemonize=uwsgi.log
# 指定依赖的虚拟环境
virtualenv=/data/virtual/venv

Step 3:nginx相关配置 

nginx配置文件一般在:/etc/nginx/nginx.cnf

#前后端分离,使用Nginx作为静态文件服务器,静态数据直接返回
server {
         listen       80;
         server_name  localhost;

        location / {
             root   /path/静态文件夹;
             index  index.html index.htm;
         }
        location /xadmin {
             include uwsgi_params;
             uwsgi_pass mywebapp;
         }
         location /ckeditor {
             include uwsgi_params;
             uwsgi_pass mywebapp;
         }


}
#动态数据
upstream mywebapp {
        #uwggi运行端口,后端端口
        server 127.0.0.1:8001;
}

server {
         #nginx监听端口
         listen  8000;
         server_name localhost;

         location / {
             include uwsgi_params;
             uwsgi_pass mywebapp;
         }

     }

 Step 4:运行

uwsgi

#虚拟环境下安装uwsgi
pip install uwsgi
#虚拟环境下运行uwsgi
uwsgi --ini uwsgi.ini
#关闭
uwsgi --stop uwsgi.pid
#上述关闭方法经常gg
lsof -Pti ":8001"#uwsgi运行端口号,找到pid
kill pid即可

nginx操作

/etc/init.d/nginx start #启动
/etc/init.d/nginx stop  #停止
/etc/init.d/nginx  reload #重新加载配置

注意事项: 

前后端分离项目,要解决跨域问题,配置文件里ALLOW_HOSTS要将域名放入。我也不清楚到底填什么,总种填

ALLOW_HOSTS=['*']#肯定没错

跨域请求需要安装,并注册

pip install django-cors-headers

中间层设置

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    ...
]
# CORS
CORS_ORIGIN_WHITELIST = (
    '127.0.0.1:80',
    '127.0.0.1:8000',
    'xxx.xxx.xxx.xxx',#公网ip
)
CORS_ALLOW_CREDENTIALS = True  # 允许携带cookie

猜你喜欢

转载自blog.csdn.net/wang785994599/article/details/81571632