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

532 Views Asked by At

I have a Django App which I want to deploy on a Centos Linux server having a global/public IP which is assigned to a domain and is secured with SSL.

The System configuration is as:

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

Apache/2.2.15 (CentOS)

When I run the server using:

python manage.py runserver 0.0.0.0:8000,

then it is only accessible from the browser by passing a local IP say

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

But I want to access it from the public/global IP, but it gives an error when I replace the local IP with Global/Public IP or domain assigned to the public IP as:

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

When I simply put this public IP in the browser as https://105.168.296.58 then it redirects the browser to the app/website say https://mywebsite.com/home which is running at port 80 on Apache, and shows the assigned domain instead of IP, so IP is working fine.

in settings.py: all hosts are allowed as ALLOWED_HOSTS = ['*']

Methods tried are:

  1. by using django-extensions as:

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

  1. by using django-sslserver as:

python manage.py runsslserver 0.0.0.0:8000

The app runs from both of these methods only locally at http://192.xxx.xx.xxx:8000/django_app/home, but None of them worked from a Public IP.

Kindly tell, How to deploy or configure this Django App to Run Outside the Local Network?

1

There are 1 best solutions below

10
FabianClemenz On

i would deploy django like this:

  1. install uwsgi in your virtualenv

    pip install uwsgi

  2. create a uwsgi config file (everything inside <> has to be edited to your needs

     # 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. install nginx and create following config

     # 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. Download and copy uwsgi_params to your directory https://github.com/nginx/nginx/blob/master/conf/uwsgi_params

  5. symlink config from /etc/nginx/sites-enabled

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

  6. Start uwsgi and nginx

    uwsgi --ini mysite_uwsgi.ini

    sudo service nginx restart

don't forget collectstatic so that these files can be served.

we added the mysite_uwsgi.ini and uwsgi_params files to our repository