nginx+uwsgi部署django项目
程序员文章站
2022-05-14 12:53:47
1、django项目部署前需要生成admin的静态资源文件 (1)生成admin的静态资源文件 (2)pycharm中根据下面步骤进行操作 (i)pycharm-->Tools-->Run manage.py Task (ii)collectstatic 信息提示,是否覆盖现有的static文件夹 ......
1、django项目部署前需要生成admin的静态资源文件
(1)生成admin的静态资源文件
# 关闭debug模型 debug = false # 允许所有域名访问 allowed_hosts = ['*'] # 静态资源路径 # static_root设置项目上线后使用的静态资源 static_root = 'd:/code/mydeploy/static'(自定义本机路径) # staticfiles_dirs将admin的静态资源保存在static文件夹中 staticfiles_dirs = [os.path.join(base_dir, 'static'), ]
(2)pycharm中根据下面步骤进行操作
(i)pycharm-->tools-->run manage.py task
(ii)collectstatic
信息提示,是否覆盖现有的static文件夹
you have requested to collect static files at the destination location as specified in your settings. this will overwrite existing files! are you sure you want to do this? type 'yes' to continue, or 'no' to cancel:
输入yes并回车
(3)一般来说,项目上线的静态资源都由配置属性static_root决定,所以项目的urls.py新增如下配置
from django.views import static from django.conf.urls import url from django.conf import settings urlpatterns = [ # 设置项目上线的静态资源路径 url('^static/(?p<path>.*)$', static.serve, {'document_root': settings.static_root}, name='static') ]
2、安装python3
注:在安装python3.6前,分别需要安装linux的wgt工具,gcc编译器环境已经python3使用的依赖组件
(1)安装liunx的wget工具,用于网上下载文件
yum -y install wget
(2)gcc编译器环境,安装python3所需的编译环境
yum -y install gcc
(3)python3使用的依赖组件
yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite*-devel mysql-devel
(4)下载python3.6压缩包
wget https://www.python.org/ftp/python/3.6.3/python-3.6.3.tgz
(5)解压python3.6安装包
tar -zxvf python-3.6.3.tgz
(6)进入python3.6.3文件夹编译安装
cd python3.6.3 sudo ./configure make && make install
3、部署uwsgi服务器
(1)安装python3所需模块
pip3 install mysqlclient pip3 install django pip3 install uwsgi
(2)修改项目setting.py配置
# 数据库连接信息 databases = { 'default': { 'engine': 'django.db.backends.mysql', 'name': '',(改为mysql对应数据库) 'user': '',(改为mysql数据库连接用户名) 'password': '',(改为mysql数据库连接密码) 'host': '',(改为mysql服务器地址) 'port': '3306', } }
(3)测试uwsgi服务器是否正常运行
uwsgi --http :8080 --chdir 项目目录 -w 项目目录下.wsgi
例:
uwsgi --http :8080 --chdir /data/work/mydeploy -w mydeploy.wsgi
其中/data/work/mydeploy为项目的绝对路径,mydeploy.wsgi是项目的wsgi.py文件
(4)为项目编写uwsgi配置文件
在项目主目录下创建*.ini配置文件,如:mydeploy.ini
[uwsgi] # django-related settings socket= :8080 # the base directory (full path) chdir=/data/work/mydeploy # django s wsgi file module=mydeploy.wsgi # process-related settings # master master=true # maximum number of worker processes processes=16 # ... with appropriate permissions - may be needed chmod-socket=664 # clear environment on exit vacuum=true
启动uwsgi命令,查看配置文件是否正确
uwsgi --ini mydeploy.ini
4、安装nginx部署项目
(1)添加nginx的安装源
rpm -uvh http://nginx.org/packages/centos/7/noarch/rpms/nginx-release-centos-7-0.el7.ngx.noarch.rpm
(2)yum安装
yum install nginx
(3)启动nginx,验证瘦安装成功
nginx(systemctl start nginx)
(4)修改nginx配置文件,实现nginx服务器与uwsgi服务器的通信连接
cd /etc/nginx vim nginx.conf
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; # 新增内容 server { listen 8090; server_name 127.0.0.1; charset utf-8; access_log /var/log/nginx/myweb_access.log; error_log /var/log/nginx/myweb_error.log; client_max_body_size 75m; # 连接uwsgi服务器,uwsgi_pass的端口与uwsgi设置的socket= :8080端口一致 location / { allow all; include uwsgi_params; uwsgi_pass 127.0.0.1:8080; uwsgi_read_timeout 2; } # 设置静态资源路径 location /static/ { expires 30d; autoindex on; add_header cache-control private; # /data/work/mydeploy/static为项目静态目录路径 alias /data/work/mydeploy/static/; } } }
(5)重启nginx,启动uwsgi服务器
cd /(项目路径) uwsgi --ini mydeploy.ini