在腾讯云上用 nginx + uwsgi + django 搭建自己的网站


  • 前言:之前在腾讯云弄了一个学生云主机和一个域名,后面又学了朋友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 应用
     
    cd /data
    sudo django-admin startproject helloworld
     定位到 /data/helloworld

  • 修改配置文件
     修改 /data/helloworld/helloworld/settings.py 文件权限为其它人可写
     
    sudo chmod 666 /data/helloworld/helloworld/settings.py
     编辑 /data/helloworld/helloworld/settings.py
     将 ALLOWED_HOSTS = [] 修改为 ALLOWED_HOSTS = ["你的主机ip"] ,这样可以允许通过 ip 访问


  • 创建简单的页面
     1、创建文件 /data/helloworld/helloworld/views.py ,并修改权限为其它人可写
     
    sudo touch /data/helloworld/helloworld/views.py
    sudo chmod 666 /data/helloworld/helloworld/views.py
     2、编辑 /data/helloworld/helloworld/views.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)
    
     3、修改 /data/helloworld/helloworld/urls.py 文件权限为其它人可写  
    sudo chmod 666 /data/helloworld/helloworld/urls.py
     4、添加路由配置,编辑 /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
     
    sudo pip install uwsgi
     2、创建文件 /data/helloworld/uwsgi.ini ,并修改权限为其它人可写

     
    sudo touch /data/helloworld/uwsgi.ini
    sudo chmod 666 /data/helloworld/uwsgi.ini
     3、编辑 /data/helloworld/uwsgi.ini ,输入以下内容,并保存

     
    [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
     4、启动 uwsgi
     
    export PYTHONPATH=/usr/local/lib/python2.7/dist-packages
    uwsgi --ini /data/helloworld/uwsgi.ini
     5、安装 nginx
     
    sudo apt-get install nginx -y
     6、添加 nginx 配置文件,创建文件 /etc/nginx/sites-enabled/helloworld.conf ,并修改权限为其它人可写
     
    sudo touch /etc/nginx/sites-enabled/helloworld.conf
    sudo chmod 666 /etc/nginx/sites-enabled/helloworld.conf
     7、编辑 /etc/nginx/sites-enabled/helloworld.conf ,输入以下内容,并保存
     
    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;
    }
     8、重启 nginx 服务 

     
    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:管理域名访问

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值