FinalShell
sudo apt update
sudo apt upgrade -y
sudo reboot
sudo apt install mysql-server -y #安装mysql
sudo apt install python3-dev python3-pip python-pip memcached virtualenv -y #安装pip和memcached
sudo apt install supervisor -y
sudo apt install nginx -y
sudo apt-get install python-dev default-libmysqlclient-dev #安装mysqlclient依赖
sudo apt-get install unzip #解压工具
sudo mysql_secure_installation
mysql -uroot -p
退出登录,使用 Crtl+D
文件地址:
/etc/mysql/conf.d/mysql.cnf
删除该文件的[mysql]行并贴入如下内容:
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
保存退出后,重启数据库,执行命令:
sudo /etc/init.d/mysql restart
后续操作,进入数据库,创建数据表并导入数据.mysql教程
在home目录下新建一个wwwroot文件夹存放项目(可自己建立存放地址)
cd /home
mkdir wwwroot
cd wwwroot
unzip dvadmin-backend.zip -d dvadmin
进入项目
cd dvadmin
创建虚拟环境
virtualenv venv
启用虚拟环境
source venv/bin/activate
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
等待依赖完成以后,收集静态文件
python manage.py collectstatic --no-input
python manage.py runserver
pip install gunicorn
在项目根目录下新建gunicorn_start.sh文件
文件内容如下:
#!/bin/bash
NAME="dvadmin" #项目名
DJANGODIR=/home/wwwroot/dvadmin #django项目路径
USER=root # the user to run as #用户
GROUP=root # the group to run as #用户组
NUM_WORKERS=1 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=application.settings # which settings file should Django use
DJANGO_WSGI_MODULE=application.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source /home/wwwroot/dvadmin/venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /home/wwwroot/dvadmin/venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--log-level=debug \
--log-file=-
chmod +x /home/wwwroot/dvadmin/gunicorn_start.sh
在gunicorn_start.sh文件目录下,执行
./gunicorn_start.sh
如上图则成功,通过Ctrl+c终止运行
pip install daphne
#!/bin/bash
USER=root # the user to run as #用户
GROUP=root # the group to run as #用户组
NAME="bankend" #项目名
DJANGODIR=/home/wwwroot/backend
DJANGO_ASGI=application.asgi
DJANGO_SETTINGS_MODULE=application.settings
echo "Starting $NAME as `whoami`"
cd $DJANGODIR
source $DJANGODIR/venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPAT
daphne -b 0.0.0.0 -p 8000 ${DJANGO_ASGI}:application
chmod +x /home/wwwroot/backend/daphne_start.sh
./daphne_start.sh
将前端代码上传到 /home/wwwroot文件夹
unzip dist.zip -d dvadmin-ui
sudo rm /etc/nginx/sites-enabled/default
进入 /etc/nginx/sites-enabled/ 目录,建立后端配置文件
sudo vim /etc/nginx/sites-enabled/dvadmin.conf
后端文件配置内容
server {
listen 8000;
server_name localhost;
root /home/wwwroot/dvadmin/;
access_log /home/wwwroot/django_access.log;
error_log /home/wwwroot/django_error.log;
location /static/ {
alias /home/wwwroot/dvadmin/collectedstatic/;
expires max;
access_log off;
log_not_found off;
}
location /media {
# 静态文件配置
alias /home/wwwroot/dvadmin/media/;
expires max;
}
location ~ \.py$ {
return 403;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:8000;
break;
}
}
}
建立一个前端配置文件,dvadmin-ui.conf
server {
listen 80;
server_name localhost; #有域名放域名,没域名就是 localhost
location / {
root /home/wwwroot/dvadmin-ui; #这里放前端项目目录地址
index index.html;
}
}
sudo vim /etc/supervisor/conf.d/dvadmin.conf
[program:dvadmin]
command = /home/wwwroot/dvadmin/gunicorn_start.sh
user = root
autostart=true
autorestart=true
redirect_stderr = true
stdout_logfile = /home/wwwroot/djangoblog.log
stderr_logfile=/home/wwwroot/djangoblog.err
sudo supervisorctl update #更新supervisorctl
sudo supervisorctl reload #重启supervisorctl
sudo /etc/init.d/memcached restart #重启缓存
sudo /etc/init.d/nginx restart #重启nginx
如果修改了代码,只需要上传更新覆盖代码后执行:
sudo supervisorctl update
sudo /etc/init.d/nginx restart