ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
Django_Ubuntu_Web部署 一、环境配置 1、Ubuntu 14.04 阿里云 2、python2.7 3、nginx 4、uWSGI 二、环境部署 1、服务器更新升级 ~~~ sudo apt-get update sudo apt-get upgrade ~~~ 2、通过宝塔搭建nginx,方便后续多网站管理 `wget -O install.sh http://download.bt.cn/install/install-ubuntu.sh && sudo bash install.sh` 登录宝塔安装 [nginx-1.12] 3、通过宝塔建立网站 4、django源码上传至网站目录/www/py.guigudoor.com 5、更换pip源 ~~~ sudo mkdir ~/.pip sudo vi ~/.pip/pip.conf ~~~ 输入: ~~~ [global] index-url = http://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com ~~~ 三、上传django源码 1、安装配置文件 在已配置好的机器上导出配置文件 `pip freeze > requirements.txt` 然后进入项目目录安装配置 `pip install -r requirements.txt` 2、测试外网访问 python manage.py runserver 0.0.0.0:8000 > 备注:如果访问不来,请确定服务器、宝塔或者其他安全是否放行8000端口。 三、uWSGI的配置 1、安装uWSGI: `sudo pip install uwsgi` 2、启动测试: www/wwwroot/py.guigudoor.com 为网站目录、guigu为django项目目录 `uwsgi --http :8000 --chdir /www/wwwroot/py.guigudoor.com/ -w guigu.wsgi` > 如果出错更新下面操作 > 安装python-dev包:sudo apt-get install python-dev > 安装或升级pip :sudo apt-get install python-pip > 升级:sudo pip install --upgrade pip 3、配置uWSGI文件: ~~~ mkdir -p /etc/uwsgi/sites cd /etc/uwsgi/sites vi guigu.ini ~~~ 配置guigu.ini chdir = www/wwwroot/py.guigudoor.com 网站目录 guigu.wsgi django项目目录 ~~~ [uwsgi] chdir = /www/wwwroot/py.guigudoor.com module = guigu.wsgi master = true processes = 10 socket = /www/wwwroot/py.guigudoor.com/guigu.sock vacuum = true ~~~ 4、建立一个自启脚本: `vi /etc/init/uwsgi.conf` 配置uwsgi.conf ~~~ description "uWSGI application server in Emperor mode" start on runlevel [2345] stop on runlevel [!2345] setuid guigu setgid www-data exec /usr/local/bin/uwsgi --emperor /etc/uwsgi/sites ~~~ 四、通过宝塔配置网站Nginx 1、网站—设置—配置文件 配置添加 ~~~ server { listen 80; server_name py.guigudoor.com; location /static/ { root /www/wwwroot/py.guigudoor.com; } location / { include uwsgi_params; uwsgi_pass unix:/www/wwwroot/py.guigudoor.com/guigu.sock; } } ~~~ ~~~ #样本 server { listen 80; server_name py.guigudoor.com; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/py.guigudoor.com; location /static/ { root /www/wwwroot/py.guigudoor.com; } location / { include uwsgi_params; uwsgi_pass unix:/www/wwwroot/py.guigudoor.com/guigu.sock; } #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则 #error_page 404/404.html; #SSL-END #ERROR-PAGE-START 错误页配置,可以注释、删除或修改 error_page 404 /404.html; error_page 502 /502.html; #ERROR-PAGE-END #PHP-INFO-START PHP引用配置,可以注释或修改 include enable-php-54.conf; #PHP-INFO-END #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效 include /www/server/panel/vhost/rewrite/py.guigudoor.com.conf; #REWRITE-END #禁止访问的文件或目录 location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; access_log off; } location ~ .*\.(js|css)?$ { expires 12h; access_log off; } access_log /www/wwwlogs/py.guigudoor.com.log; } ~~~ 2、重启nginx 3、启动uWSGI配置 `uwsgi /etc/uwsgi/sites/guigu.ini -d /www/wwwroot/py.guigudoor.com/guigu.log` > etc/uwsgi/sites/guigu.ini uWSGI配置文件 > www/wwwroot/py.guigudoor.com/guigu.log 项目目录 4、静态文件收集 `python manage.py collectstatic` 备注:需要settings.py修改配置 ~~~ #样本 STATIC_URL = '/static/' # 加入下面的配置 STATIC_ROOT = os.path.join(BASE_DIR, 'static') ~~~ 静态文件访问不了(服务器模式没有添加'DIRS': [os.path.join(BASE_DIR, 'templates')],) ~~~ #样本 TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], ~~~ ### 提示:每次重启需要启动uWSGI配置