簡體   English   中英

如何在使用 SSL 保護域的 Centos 服務器上部署 Django 應用程序?

[英]How to Deploy Django App on Centos server having domain secured With SSL?

我有一個 Django 應用程序,我想將它部署在具有global/public IPCentos Linux server上,該 IP 分配給一個domainsecured with SSL

系統配置如下:

centos-release-6-10.el6.centos.12.3.x86_64

Apache/2.2.15 (CentOS)

當我使用以下命令運行服務器時:

python manage.py runserver 0.0.0.0:8000 ,

然后它只能通過傳遞本地 IP 來從瀏覽器訪問

http://192.xxx.xx.xxx:8000/django_app/home

但是我想從公共/全局 IP 訪問它,但是當我將本地 IP 替換為全局/公共 IP 或分配給公共 IP 的域時會出錯:

105.168.296.58 took too long to respond.
Try:

Checking the connection
Checking the proxy and the firewall
Running Windows Network Diagnostics
ERR_CONNECTION_TIMED_OUT

當我簡單地將這個公共 IP 作為https://105.168.296.58放在瀏覽器中時,它會將瀏覽器重定向到應用程序/網站,例如https://mywebsite.com/home ,它在 Apache 的端口 80 上運行,並顯示分配域而不是 IP,因此 IP 工作正常。

在 settings.py 中:允許所有主機為ALLOWED_HOSTS = ['*']

嘗試的方法有:

  1. 通過使用django-extensions作為:

python manage.py runserver_plus --cert certname 0.0.0.0:8000

  1. 通過使用django-sslserver作為:

python manage.py runsslserver 0.0.0.0:8000

該應用程序僅在本地http://192.xxx.xx.xxx:8000/django_app/home從這兩種方法運行,但它們都不是從公共 IP 運行的。

請告訴,如何部署或配置此 Django 應用程序以在本地網絡外運行?

我會像這樣部署django:

  1. 在你的 virtualenv 中安裝uwsgi

    pip install uwsgi

  2. 創建一個 uwsgi 配置文件( <>中的所有內容都必須根據您的需要進行編輯

     # mysite_uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) chdir = </path/to/your/project> # Django's wsgi file module = <project>.wsgi # the virtualenv (full path) home = </path/to/virtualenv> # process-related settings # master master = true # maximum number of worker processes processes = 10 # the socket (use the full path to be safe socket = </path/to/your/project/mysite.sock> # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true
  3. 安裝 nginx 並創建以下配置

     # mysite_nginx.conf # the upstream component nginx needs to connect to upstream django { server unix:///<path/to/your/mysite/mysite.sock>; } # configuration of the server server { # the port your site will be served on listen 80; # the domain name it will serve for server_name <example.com;> # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias </path/to/your/mysite/media;> # your Django project's media files - amend as required } location /static { alias </path/to/your/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 </path/to/your/mysite/uwsgi_params;> # the uwsgi_params file you installed } }
  4. 下載並復制 uwsgi_params 到你的目錄https://github.com/nginx/nginx/blob/master/conf/uwsgi_params

  5. 來自/etc/nginx/sites-enabled符號鏈接配置

    sudo ln -s /etc/nginx/sites-available/mysite_nginx.conf /etc/nginx/sites-enabled/

  6. 啟動uwsgi和nginx

    uwsgi --ini mysite_uwsgi.ini

    sudo service nginx restart

不要忘記collectstatic以便可以提供這些文件。

我們將mysite_uwsgi.iniuwsgi_params文件添加到我們的存儲庫中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM