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-Hans'
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.
使用虚拟环境
上面的命令将创建一个名为 venv
目录 (或任何你选择的名字),其中包含我们的虚拟环境 (基本上是一堆的目录和文件)。
~/djangogirls$ source venv/bin/activate
7. 安装web服务器 openresty 安装指导
8.安装uwsgi 在虚拟环境下安装,适合不同版本的python ,安装后uwsgi 在虚拟环境的bin目录下
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 --ini mysite_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.注意:uwsgi 和 nginx的启动都要是同一个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" &
13.升级最新的python
sudo apt update sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update sudo apt install python3.7