Fork me on GitHub

基于 Ubuntu 搭建 Django 站点

Django 是一个用 Python 开发的开源 Web 应用框架,采用 MVC 模式。本实验带您搭建一个最简单的 Django 站点,并使用 uwsgi 和 nginx 作为 Web 服务器。

安装 django

安装 pip

1
sudo apt install python-pip -y

使用 pip 安装 django

1
sudo pip install django

创建 Hello World 应用并启动 Web 服务器

创建项目

1
2
cd /data
sudo django-admin startproject helloworld

定位到 /data/helloworld 目录

修改配置文件

修改 /data/helloworld/helloworld/settings.py 文件权限为其它人可写

1
sudo chmod 666 /data/helowrold/helloworld/settings.py

编辑 /data/helloworld/helloworld/settings.py

ALLOWED_HOSTS =[] 修改为 `ALLOWED_HOSTS =[“您的 CVM IP 地址”] ,这样可以允许通过 ip 访问

在实际运营中一般要改为对应的域名

启动 django 自带的 Web 服务器

1
2
cd helloworld
sudo python manage.py runserver 0.0.0.0:8000

使用浏览器访问

1
http://<IP 地址>:8080/

看到如下页面,表示 django 服务已经部署成功

image

创建简单的页面

关闭 WEB 服务器

Ctrl + C 关闭 Web 服务器

创建 views.py

创建文件 /data/helloworld/helloworld/views.py ,并修改权限其它人可写

1
2
sudo touch /data/helloworld/helloworld/views.py
sudo chmod 666 /data/helloworld/helloworld/views.py

添加视图函数

编辑 /data/helloworld/helloworld/views.py

添加内容如下,并保存

1
2
3
4
5
6
7
8
# -*- coding: utf-8 -*-
from django.http.response import HttpResponse
def hello(request):
user = request.GET.get('user')
if not user: user = 'world'
return HttpResponse('hello %s' % user)

修改 urls.py 文件权限

修改 /data/helloworold/helloworold/urls.py 文件权限为其它人可写

1
sudo chmod 666 /data/helloworld/helloworld/urls.py

添加路由配置

编辑 /data/helloworld/helloworld/urls.py

1
2
3
urlpatterns = [
url(r'^admin/', admin.site.urls),
]

修改为

1
2
3
4
5
6
import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello', views.hello),
]

测试 url 访问是否正常

再次启动 Web 服务器

1
sudo python manage.py runserver 0.0.0.0:8080

使用浏览器访问下面两个 url

1
2
http://<IP 地址>:8080/hello
http://<IP 地址>:8080/hello?user=test

关闭 Web 服务器

Ctrl + C 停止 Web 服务器

部署 uwsgi 和 nginx

使用 pip 安装 uwsgi

1
sudo pip install uwsgi

配置 uwsgi

创建文件 /data/helloworld/uwsgi.ini,并修改权限为其它人可写

1
2
sudo touch /data/helloworld/uwsgi.ini
sudo chmod 666 /data/helloworold/uwsgi.ini

输入以下内容,并保存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[uwsgi]
chdir = /data/helloworld
module = helloworld.wsgi
socket = 127.0.0.1:8080
master = true
vhost = true
no-site = true
workers = 2
reload-mercy = 10
vacuum = true
max-requests = 1000
limit-as = 512
buffer-size = 30000
pidfile = /tmp/uwsgi.pid
daemonize = /tmp/uwsgi.log

启动uwsgi

1
2
export PYTHOONPATH=/usr/local/lib/python2.7/dist-packages
uwsgi --ini /data/helloworold/uwsgi.ini

安装 nginx

1
sudo apt-get install nginx -y

添加 nginx 配置文件

创建文件 /etc/nginx/sites-enabled/helloworld.conf ,并修改权限为其它人可写

1
2
sudo touch /etc/nginx/sites-enabled/helloworld.conf
sudo chmod 666 /etc/nginx/sites-enabled/helloworld.conf

编辑 /etc/nginx/sites-enabled/helloworld.conf

输入一下内容,并保存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80;
server_name <您的 CVM IP 地址>;
charset utf-8;
location / {
uwsgi_pass 127.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;
}

重启 nginx 服务

1
sudo systemctl restart nginx

测试 nginx + uwsgi + django 是否工作正常

测试页面是否能够正常访问

使用浏览器测试下面的 url 是否可以正常访问

1
http://<您的IP 地址>/hello
坚持原创技术分享,您的支持将鼓励我继续创作
显示 Gitment 评论