- 前言:之前在腾讯云弄了一个学生云主机和一个域名,后面又学了朋友python,所以就想建一个网站来玩玩。
准备工作:一台安装了Ubuntu16.04的云服务器,一个域名(这里用buduannuli.cn),假设你的云主机是刚重装的,现在连接上那你的云主机,然后开始。。。 - 配置 ubuntu16.04 python2.7
- 首先在安全组设置开放80端口。
- 安装pip
sudo apt install python-pip -y
- 安装django
sudo pip install django==1.9
- 创建 hello world 应用
定位到 /data/helloworldcd /data sudo django-admin startproject helloworld
- 修改配置文件
修改 /data/helloworld/helloworld/settings.py 文件权限为其它人可写
编辑 /data/helloworld/helloworld/settings.pysudo chmod 666 /data/helloworld/helloworld/settings.py
将 ALLOWED_HOSTS = [] 修改为 ALLOWED_HOSTS = ["你的主机ip"] ,这样可以允许通过 ip 访问 - 创建简单的页面
1、创建文件 /data/helloworld/helloworld/views.py ,并修改权限为其它人可写
2、编辑 /data/helloworld/helloworld/views.py添加如下内容:sudo touch /data/helloworld/helloworld/views.py sudo chmod 666 /data/helloworld/helloworld/views.py
3、修改 /data/helloworld/helloworld/urls.py 文件权限为其它人可写# -*- coding: utf-8 -*- from django.http.response import HttpResponse def home(request): return HttpResponse('this is test page') def hello(request): user = request.GET.get('user') if not user: user = 'world' return HttpResponse('hello %s this is home page' % user)
4、添加路由配置,编辑 /data/helloworld/helloworld/urls.py 将sudo chmod 666 /data/helloworld/helloworld/urls.py
修改为urlpatterns = [ url(r'^admin/', admin.site.urls), ]
import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', views.hello), url(r'^home', views.home), ]
- 部署 uwsgi 和 nginx
1、使用 pip 安装 uwsgi
2、创建文件 /data/helloworld/uwsgi.ini ,并修改权限为其它人可写sudo pip install uwsgi
3、编辑 /data/helloworld/uwsgi.ini ,输入以下内容,并保存sudo touch /data/helloworld/uwsgi.ini sudo chmod 666 /data/helloworld/uwsgi.ini
4、启动 uwsgi[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
5、安装 nginxexport PYTHONPATH=/usr/local/lib/python2.7/dist-packages uwsgi --ini /data/helloworld/uwsgi.ini
6、添加 nginx 配置文件,创建文件 /etc/nginx/sites-enabled/helloworld.conf ,并修改权限为其它人可写sudo apt-get install nginx -y
7、编辑 /etc/nginx/sites-enabled/helloworld.conf ,输入以下内容,并保存sudo touch /etc/nginx/sites-enabled/helloworld.conf sudo chmod 666 /etc/nginx/sites-enabled/helloworld.conf
8、重启 nginx 服务server { listen 80; server_name buduannuli.cn;//改为你的域名 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; }
sudo systemctl restart nginx
- 测试 nginx + uwsgi + django 是否正常工作
在浏览器输入buduannuli.cn和buduannuli.cn/hello就可以看到结果 - 当django的调试模式打开时,理论上可以在浏览器上看到更新,但是因为uwsgi的原因,并不能看到更新,这时候就需要重启uwsgi,在刷新浏览器就可以看到更新:
sudo killall -9 uwsgi sudo --ini /data/helloworld/uwsgi.ini
- 主要文件:views.py:管理逻辑,urls.py:管理域名访问
在腾讯云上用 nginx + uwsgi + django 搭建自己的网站
最新推荐文章于 2024-09-02 21:23:50 发布