服务器:ubuntu 18.04 本地电脑:win10 python环境:spyder +python3.6 依赖服务:nginx uwsgi
为了方便以后的使用,建议开放22,80,8000端口。具体配置教程。 具体安全组配置可参考 端口配置
ssh连接或远程窗口连接,这里用xshell sh连接。把django项目上传到服务器。 在xshell 窗口/传输新文件中使用xftp传送。 窗口如下: 左边是我们本地桌面,右边是ubuntu文件系统,通过拖拽就可以实现文件传送。
安装nginx是否成功,可以在本地服务器输入自己服务器的ip地址,若显示以下内容,则表示成功。这里把公网ip换成自己的即可。
下载压缩包,传送到服务器,建议传送到home目录并解压 下载地址 或者百度网盘下载 链接:https://pan.baidu.com/s/139hoLiSYShdY4-xyQsKsOQ 提取码:j1dq cd进入解压好后的目录,依次输入
sudo apt-get install python3-setuptools sudo apt-get install python3-dev sudo python3 setup.py install第一步:
cd /etc/nginx/sites-available vim default第二步,修改default文件
server_name 192.168.1.121(你的公网ip); location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. # try_files $uri $uri/ =404;(注释掉) include uwsgi_params; uwsgi_pass 127.0.0.1:8000; } location /static { alias /home/feixue/python/www/for_test/static(你的静态文件目录); } location /media { alias /home/ubuntu/blog/media(你的媒体文件目录); }注意:location后面是有空格的,必须要有!alias后面也是有空格的;include上面那句话也是要注释掉的!修改后保存。
第三步:重启服务
sudo service nginx restart在博客项目的根目录下,也就是有manage.py文件的目录下,新建一个uwsgi.ini文件和一个run.log文件 然后我们使用vim编辑器编辑uwsgi.ini文件:
vim uwsgi.ini这个文件应该是空白的,添加以下内容:
[uwsgi] chdir = /root/a/testDjango(你的Django项目路径) module = testDjango.wsgi:application(把testDjango改为你的项目名称即可) socket = 127.0.0.1:8000 master = true daemonize = /root/a/testDjango/run.log(上一步创建的日志文件路径) disable-logging = trueproject下的url,在路由到ip/test/时,选择nofind 视图。
from django.contrib import admin from django.urls import path from .views import * urlpatterns = [ path('admin/', admin.site.urls), path('test/', nofind)在project中的view中,定义nofind函数
from django.shortcuts import render,redirect from django.http import HttpResponse # Create your views here. from django.conf import settings def nofind(request): resp = HttpResponse() resp.write('<title>'+'测试:路径不存在'+'</title>') resp.write('<h1>'+'请求具体信息'+'</h1>') resp.write('<a>'+'请求路径:%s'%request.path+'</a>') resp.write('<hr>') resp.write('<a>'+'请求方法:%s'%request.method+'</a>') resp.write('<hr>') resp.write('<a>'+'编码格式:%s'%request.encoding+'</a>') resp.write('<hr>') resp.write('<a>'+'Cookies:%s'%request.COOKIES+'</a>') resp.write('<hr>') resp.write('<a>'+'咨询电话:15520778293'+'</a>') return resp配置好项目以后,进行数据迁移与runserver
python manage.py makemigrations python manage.py migrate python manage.py runserver在uwsgi.ini所在项目文件下:
uwsgi --ini uwsgi.ini出现了测试界面,说明连接成功。