http://logic0.blog.163.com/blog/static/18892814620136258532112/html
# 以#开头表示注释,
$ 这里是须要执行的命令 <尖括号包含须要你本身定义的内容>
○、链接VPS或者服务器python
很少说,putty就是个不错的工具,若是你本地有mac或者linux就更好了mysql
1、升级软件包,打补丁linux
# 更新本地包的索引,这个必须作
$ sudo apt-get update
# 升级全部软件包
$ sudo apt-get dist-upgrade
# 删除不须要的软件包
$ sudo apt-get autoremove
# 重启系统,大多数时候不须要,但少数须要重启,以防万一仍是重启下的好
$ sudo reboot
$ sudo apt-get install build-essential python-dev
安装 distribute 和 pipnginx
# 下载 distribute 安装文件, curl 不可用也能够用 wget http://python-distribute.org/distribute_setup.py
$ curl -O http://python-distribute.org/distribute_setup.py
# 安装distribute
$ sudo python distribute_setup.py
# 安装文件能够删除了
$ rm distribute*
# 用 distribute 安装 pip
$ sudo easy_install pip
# 安装 virtualenv 和 virtualenvwrapper
$ sudo pip install virtualenv virtualenvwrapper
# 编辑用户的bash配置文件
$ vim .bashrc
# 在文件末尾增长下边这一行,让 virtualenvwrapper.sh 能自动执行,给shell添加命令
source /usr/local/bin/virtualenvwrapper.sh
# 保存文件退出
# 可使用exit 推出从新登陆,使 virtualenvwrapper 生效,也可使用source 使之生效
$ exit
# 建立一个虚拟运行环境,通常使用 VIRTUALENV_NAME=你的project名字就好。<>包含的是须要你本身指定的东西,如下再也不说明
$ mkvirtualenv <VIRTUALENV_NAME>
# 建立完后会自动启动虚拟环境,使用 deactivate 可退出
$ deactivate
# 激活特定的虚拟环境 或者 更改到另外一个虚拟环境,执行下边的命令
$ workon <VIRTUALENV_NAME>
# 退出虚拟环境,若是你在虚拟环境内的话
$ deactivate
$ pip freeze # pip freeze 能够查看都安装了哪些软件包及其版本
$ workon <VIRTUALENV_NAME> # 切换到虚拟环境
$ pip freeze # 查看虚拟环境内的软件包及版本,就能够发现不一样
# 安装最新版的Django,须要特定版本请查看 pip 指定版本的方法
$ pip install django
# 安装 docutils, Django 的 admin 会用到这个
$ pip install docutils
# 测试 Django 是否安装成功
$ django-admin.py startproject <APP_NAME>
$ cd <APP_NAME>
# 给 manage.py 运行权限
$ chmod +x manage.py
# 测试服务器,若是只是在本机测试则不须要指定 0.0.0.0
$ ./manage.py runserver 0.0.0.0:8000
# 给系统添加相应的图形软件包支持,这步要在最早执行,由于PIL 须要编译,若是没有jpeg就没法支持.jpg图片
$ sudo apt-get install libjpeg8-dev libfreetype6-dev zlib1g-dev
# 如下两个任选一个,有人说 PIL 偶尔会有问题,若是你也不肯定,那就选 Pillow 吧
$ pip install pillow
$ pip install pil
# 安装 MySQL
$ sudo apt-get install mysql-server libmysqlclient-dev
# 安装 MySQL 的 Python 链接器
$ pip install MySQL-Python
$ pip install south
# 将 south 加到你的 Django 工程的配置文件里
$ vim <YOUR_APP>/settings.py
INSTALLED_APPS =
...
'south',
...
# 安装 memcached 和 开发包
$ sudo apt-get install memcached libmemcached-dev
# 安装 memcached 的 Python 链接器,有不少同类型的链接器,推荐pylibmc
$ pip install pylibmc
# 修改 Django project 的配置文件settings.py,添加 cache 支持
$ vim <YOUR_APP>/settings.py
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
'LOCATION': '127.0.0.1:11211',
}
}
$ sudo apt-get install rabbitmq-server
$ sudo rabbitmqctl add_user <RABBIT_USER> <RABBIT_PASSWORD>
$ sudo rabbitmqctl add_vhost <RABBIT_VHOST>
$ sudo rabbitmqctl set_permissions -p <RABBIT_VHOST> <RABBIT_USER> ".*" ".*" ".*"
# 经过pip 安装
$ pip install django-celery
$ vim <YOUR_APP>/settings.py
# 将 djcelery 应用安装到当前的 project
INSTALLED_APPS =
...
'djcelery',
...
# 乱七八糟的配置,甭管具体是啥了,加上就是了,注意修改须要自定义的部分
BROKER_URL = "amqp://<RABBIT_USER>:<RABBIT_PASSWORD>@localhost:5672/<RABBIT_VHOST>"
CELERY_RESULT_BACKEND = "database"
CELERY_RESULT_DBURI = "mysql://<DB_USER>:<DB_PASSWORD>@localhost/<DB_NAME>"
# 把这两行放到 settings.py 文件尾
import djcelery
djcelery.setup_loader()
# pip 安装 Gunicorn
$ pip install gunicorn
# 将 Gunicorn 添加到你的 Django project 中
$ vim <YOUR_APP>/settings.py
INSTALLED_APPS =
...
'gunicorn',
...
$ ./manage.py run_gunicorn -w 4 -k gevent
# Ctrl+C 能够退出 Gunicorn,PS:若是它成功运行的话
# apt-get 安装 Supervisor
$ sudo apt-get install supervisor
# 配置文件必须放在 /etc/supervisor/conf.d/ 目录下,并且必须以 .conf 后缀结尾 # celery 配置文件 /etc/supervisor/conf.d/celeryd.conf # 告诉 Supervisor 这个程序叫什么名字 [program:celeryd] # 启动命令,你能够注意到这里的python 是用的 virtualenv 下的 python command = /home/<USERNAME>/.virtualenvs/<VIRTUALENV_NAME>/bin/python /home/<USERNAME>/<APP_NAME>/manage.py celeryd -B -E # 服务运行时处在哪一个目录 directory = /home/<USERNAME>/<APP_NAME> # 以哪一个用户的身份运行 user = <USERNAME> # 是否随系统自动启动 autostart = true # 挂掉后是否自动重启 autorestart = true # 标准输出和错误信息log文件 stdout_logfile = /var/log/supervisor/celeryd.log stderr_logfile = /var/log/supervisor/celeryd_err.log
# celery 监控进程配置文件 /etc/supervisor/conf.d/celerycam.conf
[program:celerycam]
command = /home/<USERNAME>/.virtualenvs/<VIRTUALENV_NAME>/bin/python /home/<USERNAME>/<APP_NAME>/manage.py celerycam
directory = /home/<USERNAME>/<APP_NAME>
user = <USERNAME>
autostart = true
autorestart = true
stdout_logfile = /var/log/supervisor/celerycam.log
stderr_logfile = /var/log/supervisor/celerycam_err.log
# Gunicorn 的配置文件 /etc/supervisor/conf.d/gunicorn.conf
[program:gunicorn]
command = /home/<USERNAME>/.virtualenvs/<VIRTUALENV_NAME>/bin/python /home/<USERNAME>/<APP_NAME>/manage.py run_gunicorn -w 4 -k gevent
directory = /home/<USERNAME>/<APP_NAME>
user = <USERNAME>
autostart = true
autorestart = true
stdout_logfile = /var/log/supervisor/gunicorn.log
stderr_logfile = /var/log/supervisor/gunicorn_err.log
# 重启 Supervisor
$ sudo service supervisor restart
# restart/stop/start Supervisor 管理的全部服务
$ sudo supervisorctl restart all
$ sudo supervisorctl stop all
$ sudo supervisorctl start all
# 只重启 celeryd,这个名字,就是在配置文件里告诉 Supervisor的那个
$ sudo supervisorctl restart celeryd
# 只启动 Gunicorn
$ sudo supervisorctl start gunicorn
# 建立文件目录 static 、 media
sudo mkdir /var/www
sudo mkdir /var/www/static
sudo mkdir /var/www/media
# 赋权
sudo chown -R <USERNAME>:www-data /var/www
$ vim <YOUR_APP>/settings.py
# 修改 Django project 配置
MEDIA_ROOT = '/var/www/media/'
MEDIA_URL = '/media/'
STATIC_ROOT = '/var/www/static/'
STATIC_URL = '/static/'
# 每次static 文件发生变动,要运行下边的命令
$ ./manage.py collectstatic
# 安装 nginx
$ sudo apt-get install nginx
# 删除默认
$ sudo rm /etc/nginx/sites-enabled/default
# 空白配置文件,并创建一个链接到 sites-enabled 使其生效
$ sudo touch /etc/nginx/sites-available/<你的应用>
$ cd /etc/nginx/sites-enabled
$ sudo ln -s ../sites-available/<你的应用>
# 编辑配置文件
$ vim /etc/nginx/sites-available/<你的应用>
# 配置一个 upstream server ,起个名字叫 gunicorn,监听 8000 端口
upstream gunicorn {
server localhost:8000;
}
# 配置 nginx 服务器
server {
# 监听 80 端口
listen 80;
# 绑定对这些域名的请求
server_name <你的域名>.com www.<你的域名>.com;
# 在这个目录下查找文件
root /var/www/;
# log文件
access_log /var/log/nginx/<你的应用>.access.log;
error_log /var/log/nginx/<你的应用>.error.log;
# 这个选项来指定用户能够上传大文件
# 具体见 http://wiki.nginx.org/HttpCoreModule#client_max_body_size
# 不知道放到哪儿好,写了两次,正常运行
client_max_body_size 0;
# 这行比较重要
# 尝试以静态文件方式处理当前请求
# 若是找不到知足条件的静态文件,就把链接传给 Gunicorn
try_files $uri @gunicorn;
# 配置 Gunicorn 信息
location @gunicorn {
# 再写一次,以防万一
client_max_body_size 0;
# 转发给上边配置的 upstream server
proxy_pass http://gunicorn;
# 确保 URL 不转到 http://gunicorn
proxy_redirect off;
# Gunicorn 在5 minutes 内没有回应就放弃
# 这个时间能够随意改,本身看着办
proxy_read_timeout 5m;
# 肯定这些 HTTP headers 配置是正确的
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
重启Nginx,搞定!你能够运行你的Django 应用了。sql
# 重启 nginx
$ sudo service nginx restart