Django网站部署配置

安装依赖

sudo apt install mysql-server -y #安装mysql
sudo apt install python3-dev python3-pip python-pip -y #安装pip
sudo apt install supervisor -y
sudo apt install nginx -y
sudo apt-get install python-dev default-libmysqlclient-dev libmysqlclient-dev #安装mysqlclient依赖

设置虚拟环境

cd ~
mkdir -p  python/env && cd python/env
virtualenv -p /usr/bin/python3 wenknow
source wenknow/bin/activate

deactivate # 退出虚拟环境

数据库迁移

./manage.py makemigrations
./manage.py migrate
./manage.py static --no-input
./manage.py compress --force
  • 尝试启动网站
python3 manage.py runserver

gunicorn 配置

安装,终端下执行:

source ~/python/env/wenknow/bin/activate
pip install gunicorn

配置,终端下执行:

vim ~/python/gunicorn_start.sh

贴入如下内容:

#!/bin/bash

NAME="wenknow"
DJANGODIR=/home/server/python/wenknow #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=wenknow.settings # which settings file should Django use
DJANGO_WSGI_MODULE=wenknow.wsgi # WSGI module name

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source /home/server/python/env/wenknow/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/server/python/env/wenknow/bin/gunicorn  ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--log-level=debug \
--log-file=-

注意,将该文件中的server修改成你自己的用户,如果是root用户的话,也要将/home/server修改为/root。同时,wenknow/settings.py中的DEBUG = True修改为DEBUG = False。 增加可执行权限:

chmod +x gunicorn_start.sh

执行:

./gunicorn_start.sh

如果看到类似下面的输出,则说明成功了。 然后使用Ctrl+c来终止。

nginx配置

终端下执行:

sudo vim /etc/nginx/conf.d/www.wenknow.com.conf

贴入如下内容:

server {

    listen 80;
    server_name www.wenknow.com;
    root /home/server/python/wenknow/;

    access_log /var/log/nginx/django_access.log;
    error_log /var/log/nginx/django_error.log;

    location /static/ {
        alias /home/server/python/wenknow/static/;
        expires max;
        access_log        off;
        log_not_found     off;
    }
    location /media {
        # 静态文件配置
        alias /home/server/python/wenknow/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;
        }
    }

}

保存并退出。重启nginx:

sudo /etc/init.d/nginx restart

配置Supervisor

终端下执行:

sudo vim /etc/supervisor/conf.d/wenknow.conf

贴入如下内容:

[program:wenknow]
command = /home/server/python//gunicorn_start.sh
user = server
autostart=true
autorestart=true

redirect_stderr = true
stdout_logfile = /var/log/wenknow.log
stderr_logfile=/var/log/wenknow.err

同样,需要将server修改成你自己等用户名. 保存成功后继续执行:

sudo supervisorctl update
sudo supervisorctl reload 
systemctl restart nginx

至此,全部完成。你可以通过你的浏览器访问http://www.wenknow.com了

评论