微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ubuntu django web服务器部署

1.在ubuntu14.04上 安装pip3 https://bootstrap.pypa.io/get-pip.py

python3 get-pip.py

2.安装django 最新版

pip3 install django

3.修改django 支持中文 在settings.py 设置

LANGUAGE_CODE = 'zh_CN'

将 /usr/local/lib/python3.4/dist-packages/django/contrib/admin/locale/zh_Hans 拷贝为zh_CN

4.修改系统支持中文,同时也可解决软件源中找不到个别软件的问题

apt-get install language-pack-zh-hans*

apt-get update

5.安装虚拟环境

~/djangogirls$ sudo apt-get install python-virtualenv
~/djangogirls$ virtualenv --python=python3.4 venv

6.

使用虚拟环境

上面的命令将创建一个名为myvenv目录 (或任何你选择的名字),其中包含我们的虚拟环境 (基本上是一堆的目录和文件)。

~/djangogirls$ source myvenv/bin/activate

7. 安装web服务器 openresty 安装指导

8.安装uwsgi

pip install uwsgi 如果报错 Fatal error: Python.h: No such file or directory,就要安装python开发包

sudo apt-get install python-dev  # for python2.x installs
sudo apt-get install python3-dev  # for python3.x installs

9.编写配置文件 mysite_uwsgi.ini 启动:uwsgi --inimysite_uwsgi.ini

重新加载: uwsgi --reload /tmp/uwsgi.pid 停止:uwsgi --stop /tmp/uwsgi.pid

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir      = /root/web/mysite #django 的工程目录
# Django's wsgi file
module     = mysite.wsgi
# the virtualenv (full path)
home      = /root/web/venv

# process-related settings
# master
master     = true
pidfile            = /tmp/uwsgi.pid #方便管理uwsgi的更新和停止
# maximum number of worker processes
processes    = 10
# the socket (use the full path to be safe
socket     = /root/web/mysite/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket  = 664
# clear environment on exit
vacuum     = true


10.配置Nginx.conf 启动: Nginx (这里需要配置PATH=/your/Nginx/path/)

user  root;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
# the upstream component Nginx needs to connect to
upstream django {
    server unix:///root/web/mysite/mysite.sock; # for a file socket
   # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
    server {
        listen       8000;
        server_name  192.168.1.225;
        #charset koi8-r;
	charset     utf-8;

        #access_log  logs/host.access.log  main;
    # max upload size
    client_max_body_size 75M;   # adjust to taste
    # Django media
    location /media  {
        alias /root/web/mysite/media;  # your Django project's media files - amend as required
    }

    location /static {
	include       mime.types;#如果不加这句,你的css样式就不会显示
       alias /root/web/mysite/static; # your Django project's static files - amend as required
    }

    # Finally,send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     uwsgi_params; # the uwsgi_params file you installed
    }
    }
}

11.注意:uwsgiNginx的启动都要是同一个user,django 项目要调用python manage.py collectstatic命令,整理静态文件(js/css等)

12.加入开机启动项 :crontab -e 编辑添加如下内容

@reboot su - root -c /usr/local/openresty/Nginx/sbin/Nginx &
@reboot su - root -c "/usr/local/bin/uwsgi --ini /root/web/mysite/mysite_uwsgi.ini" &

原文地址:https://www.jb51.cc/ubuntu/353957.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐