python+django+fastcgi+nginx

來源:互聯網
上載者:User

標籤:django

安裝依賴

安裝 flup:

$ sudo pip install flup

安裝 nginx

$ sudo apt-get install nginx
設定 django

本例中項目名為 bbs:

$ pwd/var/www/bbs$ tree . -d.|-- bb  # 應用|-- bbs  # settings.py wsgi.py|-- static  # 靜態檔案|   `-- css`-- templates  # TEMPLATE_DIRS

配置 settings:

TEMPLATE_DIRS = (    ‘/var/www/bbs/templates‘,  # 這裡要是絕對路徑)STATIC_ROOT = ‘/var/www/bbs/static‘

配置 wsgi.py:

import osimport syssys.path.append(‘/var/www/bbs/‘)  # 項目目錄的絕對路徑os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bbs.settings")

本例中使用 /var/www/bbs/static/ 儲存靜態檔案,使用命令 python manage.py collectstatic 收集靜態檔案。

最終 /var/www/bbs/static/ 目錄結構應該類似(admin 目錄及其目錄下檔案一定要有):

$ tree static -dstatic|-- admin|   |-- css|   |-- img|   |   `-- gis|   `-- js|       `-- admin`-- css
配置 nginx
$ vim /etc/nginx/conf.d/bbs.confserver {    listen   80;    server_name bbs.com;    access_log /var/log/nginx/bbs.access.log;    error_log /var/log/nginx/bbs.error.log;    location / {        # host and port to fastcgi server        fastcgi_pass 127.0.0.1:8081;  # host:port        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_pass_header Authorization;        fastcgi_intercept_errors off;        fastcgi_param SERVER_PROTOCOL $server_protocol;        fastcgi_param SERVER_PORT $server_port;        fastcgi_param SERVER_NAME $server_name;        }    location /static/ {  # STATIC_URL        alias /var/www/bbs/static/;  # STATIC_ROOT     }    location /meida/ {  # MEDIA_URL        alias /var/www/bbs/media/;  # MEDIA_ROOT     }   # 如果 admin 檔案夾在 static 目錄下,可以不配置下面項    location /static/admin/ {  # admin static file        alias /var/www/bbs/static/admin/;    }}

以 fastcgi 的形式運行 django 項目(注意:這裡的 host 及 port 要與上面配置的一樣):

$ python manage.py runfcgi host=127.0.0.1 port=8081

終止 fastcgi 話,查看相關進程然後結束掉即可:

$ ps aux | grep cgimzg       2500  0.0  5.0  34860 12480 ?        S    15:43   0:00 python manage.py runfcgi host=127.0.0.1 port=8081mzg       2501  0.0  4.7  34860 11784 ?        S    15:43   0:00 python manage.py runfcgi host=127.0.0.1 port=8081mzg       2502  0.0  4.7  34860 11788 ?        S    15:43   0:00 python manage.py runfcgi host=127.0.0.1 port=8081$ sudo kill 2500$ ps aux | grep cgimzg       2745  0.0  0.3   5128   876 pts/1    S+   16:05   0:00 grep --colour=auto cgi

測試環境的話,由於網域名稱是虛構的,所以要配置 /etc/hosts 添加 127.0.0.1 bbs.com

$ sudo vim /etc/hosts$ tail /etc/hosts127.0.0.1   bbs.com

更改目錄許可權:

$ sudo chown www-data:www-data /var/www/bbs -R

啟動 nginx 服務

$ service nginx restart


python+django+fastcgi+nginx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.