afi
afi

Reputation: 603

how can i access django with apache? apache showing default page on django

i am getting the default page of apache2 when i try to access the django on my lan ip address via apache server. i am also running django python manage.py runsever command and try to access with 192.168.10.11. but it still showing the default page of apache. how can i access django with apache?

<VirtualHost *:80>
 ServerName localhost
 DocumentRoot /home/five/NewsDesk/server
 WSGIScriptAlias / /home/five/NewsDesk/server/server/wsgi.py

 # adjust the following line to match your Python path 
 WSGIDaemonProcess 192.168.10.11 processes=2 threads=15 display-name=%{GROUP} python-home=/home/five/NewsDesk/env/lib/python3.8
 WSGIProcessGroup 192.168.10.11

 <directory /home/five/NewsDesk/server>
   <Files wsgi.py>
    Allow from all
    Require all granted
   </Files>
   AllowOverride all
   Require all granted
   Options FollowSymlinks
 </directory>

 Alias /static/ /home/five/NewsDesk/server/staticfiles/

 <Directory /home/five/NewsDesk/server/staticfiles>
  Require all granted
 </Directory>
</VirtualHost>

apache2ctl -S

$apache2ctl -S

VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server localhost (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost localhost (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost localhost (/etc/apache2/sites-enabled/newsdesk.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/run/apache2/" mechanism=default 
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used

Upvotes: 0

Views: 823

Answers (1)

cizario
cizario

Reputation: 4269

i guess, your django project should have the following structure :

. home
  .. five
     .. NewsDesk
        .. server #  Your Django project root folder
           .. server
              .. settings.py
              .. wsgi.py

           ..

           .. staticfiles #  Your public folder to collect assets (js, css, img ..)
                          #  and served using apache

           .. manage.py

to serve/expose your django project locally using apache server you need to make some changes on:

vhost config

<VirtualHost *:80>
  # ServerName localhost
  ServerName myapp.local  #  rename your server

  DocumentRoot /home/five/NewsDesk/server
  WSGIScriptAlias / /home/five/NewsDesk/server/server/wsgi.py
  
  # adjust the following line to match your Python path 

  # i intentionnally commented the 2 lines below
  # WSGIDaemonProcess 192.168.10.11 processes=2 threads=15 display-name=%{GROUP} python-home=/home/five/NewsDesk/env/lib/python3.8
  # WSGIProcessGroup 192.168.10.11
  
  <directory /home/five/NewsDesk/server/server>  # HERE a missing "/server"
    <Files wsgi.py>
      # Allow from all
      Require all granted
    </Files>
    # AllowOverride all
    # Require all granted
    # Options FollowSymlinks
  </directory>
  
  Alias /static /home/five/NewsDesk/server/staticfiles  # HERE remove the triailling "/"
  <Directory /home/five/NewsDesk/server/staticfiles>
    Require all granted
  </Directory>

</VirtualHost>

/etc/hosts

(depending on linux distro and how apache is installed and configured)

127.0.0.1  myapp.local

settings.py

ALLOWED_HOSTS = [
  '127.0.0.1', 'localhost',
  '192.168.10.11', 'myapp.local',
]

and now, to expose your app locally, use the command below

python manage.py runserver YOUR_IP_ADDRESS:PORT

but in your case, it should be :

python manage.py runserver 192.168.10.11

let me know if this was useful for you

Upvotes: 1

Related Questions