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