cd helloworld
sudo python manage.py runserver0.0.0.0:8080
5使用浏览器访问,就能看到成功显示网页了
http://115.159.157.136:8080/
6 ctrl+C 关闭web服务器
7创建应用,到项目根目录下
sudo python manage.py startapp learn
8把我们新定义的app加到settings.py中的INSTALL_APPS中
修改 helloworld/helloworld/settings.py
#Application definition
INSTALLED_APPS=['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','learn',
]
9将/data/helloworld/learn/views.py修改权限为其它人可写
sudo chmod 666 /data/helloworld/learn/views.py
编辑内容
from django.http.response importHttpResponsedefhello(request):
user= request.GET.get('user')if not user: user = 'world'
return HttpResponse('hello %s' % user)
10修改 urls.py 文件权限
修改 /data/helloworld/helloworld/urls.py 文件权限为其它人可写,并且修改为
sudo chmod 666 /data/helloworld/helloworld/urls.py
from django.contrib import admin
from django.urls import path
from learn import views as learn_views
urlpatterns = [
path('admin/', admin.site.urls),
path('hello',views.hello),
]
11启动web服务器,浏览器输入网址
sudo python manage.py runserver 0.0.0.0:8080http://115.159.157.136:8080/hello
http://115.159.157.136:8080/hello?user=test
12关闭web服务器
3部署 uwsgi 和 nginx
1使用 pip 安装 uwsgi
sudo pip install uwsgi
2配置 uwsgi
创建文件 /data/helloworld/uwsgi.ini ,并修改权限为其它人可写
sudo touch /data/helloworld/uwsgi.ini
sudo chmod666 /data/helloworld/uwsgi.ini
编辑输入以下内容
[uwsgi]
chdir= /data/helloworld
module=helloworld.wsgi
socket= 127.0.0.1:8080master=true
vhost=true
no-site =true
workers= 2reload-mercy = 10vacuum=true
max-requests = 1000limit-as = 512buffer-size = 30000pidfile= /tmp/uwsgi.pid
daemonize= /tmp/uwsgi.log
3启动uwsgi
找到uwsgi所在位置,并加入环境变量
whereis uwsgi
得到结果:uwsgi:/usr/local/bin/uwsgi
export PYTHONPATH=/usr/local/bin
启动uwsgi
uwsgi--ini /data/helloworld/uwsgi.ini
4安装nginx
sudo apt-get install nginx -y
5添加 nginx 配置文件
创建文件 /etc/nginx/sites-enabled/helloworld.conf ,并修改权限为其它人可写
sudo touch /etc/nginx/sites-enabled/helloworld.conf
sudo chmod666 /etc/nginx/sites-enabled/helloworld.conf
server {
listen80;
server_name115.159.157.136;
charset utf-8;
location/{
uwsgi_pass127.0.0.1:8080;
include/etc/nginx/uwsgi_params;
client_max_body_size 10m;
}
client_body_timeout 3m;
send_timeout 3m;
proxy_send_timeout 3m;
proxy_read_timeout 3m;
}
6重启nginx服务
sudo systemctl restart nginx
7测试 nginx + uwsgi + django 是否工作正常
http://115.159.157.136/hello