Dajngo 网站开发 ---Task2(下)
安装一些需要的应用
suppervisor
sudo apt install supervisor -y
复制代码
2. 在虚拟环境安装 gunicorn
它的配置要比用 uwsgi 方便很多
1. 在项目根目录下创建一个 gunicorn_start.sh 文件
NAME="MyBlog"
DJANGODIR=********/MyBlog #Django project directory
USER=server # the user to run as
GROUP=server # the group to run as
NUM_WORKERS=1 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=config.settings # which settings file should Django use
DJANGO_WSGI_MODULE=config.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source ********/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 *******/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--log-level=debug \
--log-file=-
复制代码
USER=server # the user to run as
GROUP=server # the group to run as
这里的用户名要改为自己的比如:tango
2. 修改权限
chmod +x gunicorn_start.sh
复制代码
3. 运行
3. 配置 Nginx
location /static/ {
alias /home/tango/Desktop/DjangoDev/www/MyBlog/static/;
expires max;
access_log off;
log_not_found off;
}
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;
}
}
复制代码
4. 配置 Supervisor
sudo vim /etc/supervisor/conf.d/myblog.conf
复制代码
并写入如下内容
[program:myblog]
command = *******/gunicorn_start.sh
user = server
autostart=true
autorestart=true
redirect_stderr = true
stdout_logfile = /var/log/myblog.log
stderr_logfile=/var/log/myblog.err
复制代码
user = server
同样也要修改成为自己的用户名
最后更新一下我们设置的一系列内容
sudo supervisorctl update
sudo supervisorctl reload
复制代码
5. 关于后台静态样式丢失问题
修改 settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static")
复制代码
并在项目目录下新建一个 static 的文件夹
运行如下命令进行静态文件收集
python manage.py collectstatic
复制代码
评论