Debian + Nginx + Django
软件版本:Debian7.7, Nginx1.2.1, Django1.7.
[备注:初次IT博文,不足之处请见谅]
第一步:
软件安装:
sudo apt-get install nginx
pip install django
pip install flup
第二步:
创建一个名字为“trovato”的Django项目:
cd /home/django-sites/
django-admin.py startproject trovato
第三步:
配置Nginx.(Nginx的配置文件位于:/etc/nginx/)(我们所创建的Django项目位于:/home/django-sites/trovato)
下面我们要为trovato网站新建一个配置文件:
cd /etc/nginx/site-avaliable/
vi trovato
trovato的内容贴在下面:
server {
listen 80;
#端口
root /home/django-sites/trovato;
#项目目录
server_name trovato.com.cn;
#网站的域名
#主机,端口以及Fastcgi设置:
location / {
fastcgi_pass 127.0.0.1:8080;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param pass_header Authorization;
fastcgi_intercept_errors off;
}
#Django的静态文件目录
location ~/static/ {
root /home/django-sites/trovato/;
break;
}
location ~/media/ {
root /home/django-sites/trovato/;
break;
}
#设置浏览器缓存这些图片格式文件浏览器缓存时间是30天,css/js缓存时间1小时
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
(建议阅读/etc/nginx/site-availables/default,Nginx配置文件的写法尽在其中)
然后将此文件:/etc/nginx/site-avalables/trovato复制到:/etc/nginx/site-enable/
cp /etc/nginx/site-avalilables/trovato /etc/nginx/sites-enable/trovato
service nginx restart
第四步:启动网站
切换到Django网站目录:
cd /home/django-sites/trovato
python manage.py runfcgi host=127.0.0.1 port=8080 --settings=trovato.settings
打开浏览器访问:trovato.com.cn: