Django项目配置到Ubuntu阿里云服务器并从外网访问详细步骤

it2022-05-05  129

Django项目配置到Ubuntu阿里云服务器并从外网访问详细步骤

坏境:

服务器:ubuntu 18.04 本地电脑:win10 python环境:spyder +python3.6 依赖服务:nginx uwsgi

1. 服务器端口配置

为了方便以后的使用,建议开放22,80,8000端口。具体配置教程。 具体安全组配置可参考 端口配置

2. 连接服务器

ssh连接或远程窗口连接,这里用xshell sh连接。把django项目上传到服务器。 在xshell 窗口/传输新文件中使用xftp传送。 窗口如下: 左边是我们本地桌面,右边是ubuntu文件系统,通过拖拽就可以实现文件传送。

3. 配置必要的软件

安装Django

sudo apt-get update sudo apt-get install pip3 pip3 install django2.0

安装nginx

sudo apt-get update sudo apt-get install nginx

安装nginx是否成功,可以在本地服务器输入自己服务器的ip地址,若显示以下内容,则表示成功。这里把公网ip换成自己的即可。

安装uwsgi

下载压缩包,传送到服务器,建议传送到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

4. 配置项目

配置nginx

第一步:

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

配置uwsgi

在博客项目的根目录下,也就是有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 = true

配置settings

DEBUG = False ALLOWED_HOSTS = ['*']

假设我们使用默认数据库,在我的测试代码中,我是这样写的:

project下的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

配置好项目以后,进行数据迁移与runserver

python manage.py makemigrations python manage.py migrate python manage.py runserver

成功反馈

重启nginx服务启动uwsgi服务 sudo service nginx restart

在uwsgi.ini所在项目文件下:

uwsgi --ini uwsgi.ini

出现了测试界面,说明连接成功。

配置mysql(如果你有使用的话)

配置mysql文件 sudo vim/etc/mysql/mysql.conf.d/mysqld.cnf 注释掉 # band-address = localhost init.py import pymysql pymysql.install_as_MySQLdb()

一些有用命令的用法

uwsgi服务器开启和关闭 uwsgi --ini uwsgi.ini uwsgi --stop uwsgi.pid nginx 服务器开启和关闭 sudo /usr/local/nginx/sbin/ nginx sudo nginx -s stop

最新回复(0)