如何部署Django应用程序在Centos服务器上有域安全与SSL?

sc4hvdpw  于 2022-11-07  发布在  Go
关注(0)|答案(1)|浏览(169)

我有一个Django应用程序,我想在一个Centos Linux server上部署它,它有一个global/public IP,它被分配给一个domain,并且是secured 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工作正常。
在www.example.com中settings.py:所有主机都允许为ALLOWED_HOSTS = ['*']
尝试的方法有:

  1. by usingdjango-extensions为:
    python manage.py runserver_plus --cert certname 0.0.0.0:8000
  2. by usingdjango-sslserver为:
    python manage.py runsslserver 0.0.0.0:8000
    该应用程序仅在本地以http://192.xxx.xx.xxx:8000/django_app/home通过这两种方法运行,但它们都不能从公共IP运行。
    请告诉,如何部署或配置这个Django应用程序在本地网络之外运行?
gupuwyp2

gupuwyp21#

我会这样部署django:
1.在虚拟环境中安装uwsgi
pip install uwsgi
1.创建一个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

1.安装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
     }
}

1.下载uwsgi_params并将其复制到目录https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
1.来自/etc/nginx/sites-enabled的符号链接配置
sudo ln -s /etc/nginx/sites-available/mysite_nginx.conf /etc/nginx/sites-enabled/
1.启动uwsgi和nginx
uwsgi --ini mysite_uwsgi.ini
sudo service nginx restart
不要忘记collectstatic,以便可以提供这些文件。
我们将mysite_uwsgi.iniuwsgi_params文件添加到存储库中

相关问题