0

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?

  • i would use apache as a reverse proxy: https://howto.lintel.in/setup-apache-proxy-django-application/ – FabianClemenz Jul 15 '22 at 08:00
  • I added ```ProxyPreserveHost On``` ```ProxyPass /static !``` ```ProxyPass / http://121.136.2.82:8080/``` ```ProxyPassReverse / http://121.136.2.82:8080/``` ```ProxyTimeout 300```but it shows ```Proxy Error The proxy server received an invalid response from an upstream server. The proxy server could not handle the request GET /aws_sm_plots/login. Reason: Error reading from remote server``` – Abhilash Singh Chauhan Jul 18 '22 at 19:17
  • the proxy is running on the same server as the django application? then you need to use `http://127.0.0.1:8080` – FabianClemenz Jul 19 '22 at 05:08

1 Answers1

0

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

FabianClemenz
  • 303
  • 3
  • 8
  • However I was using apache, I have configured it using ```django``` + ```mod_wsgi``` + ```apache``` later, which is working on local network, but now running through public network. Thanks for the detailed answer @FabianClemenz, I'll try this approach too. – Abhilash Singh Chauhan Jul 19 '22 at 05:40
  • Actually, when you hit some public IP, by default it takes some ```web app``` assigned on port ```80```. In the similar way some app which is routed through PHP open as the default page on entering simply this public IP, but gives an error when ```this.public.ip:8080``` is entered in the browser. It runs on the local network but not from the public network via ```Apache webserver``` – Abhilash Singh Chauhan Jul 19 '22 at 05:45
  • maybe you need to open these ports - some firewalls have only `80` or `22` opened. the `listen` section sets the port listened to. I think you don't want everybody to add a port behind the Url? – FabianClemenz Jul 19 '22 at 05:46
  • I have opened the ports through the firewall so port 8080 is open. Yeah You are right @Fabian, I will not want everybody to add a port behind the Url, but this would come later. Firstly it should run at the public IP – Abhilash Singh Chauhan Jul 19 '22 at 05:50
  • Do I need to whitelist the port ```8080``` from the ```administrator``` who has provided us with the ```domain or Gloabl/Public IP``` ? – Abhilash Singh Chauhan Jul 19 '22 at 05:51
  • sure :) just add the ip to `server_name` :) – FabianClemenz Jul 19 '22 at 05:51
  • i don't know how your network and firewall settings are set up - maybe the admin needs to open it - but its a quick fix to use 80 :) – FabianClemenz Jul 19 '22 at 05:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/246560/discussion-between-fabianclemenz-and-abhilash-singh-chauhan). – FabianClemenz Jul 19 '22 at 05:52
  • 80 is already taken by some other app, which can not be shut down, so had thought of deploying it on different port. – Abhilash Singh Chauhan Jul 19 '22 at 05:53
  • let us discuss in the chat mentioned above – FabianClemenz Jul 19 '22 at 06:10