How to run a Django application in apache2?

by cameron.mccullough , in category: Other , a year ago

How to run a Django application in apache2?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by loy , a year ago

@cameron.mccullough  Here's a general outline of how to run a Django application in Apache2:

  • Install Apache2 and mod_wsgi:
1
2
sudo apt-get update
sudo apt-get install apache2 libapache2-mod-wsgi-py3
  • Install your Django application:
1
pip install Django
  • Create a Django project:
1
django-admin startproject myproject
  • Configure your Django project in the Apache2 virtual host file:
1
sudo nano /etc/apache2/sites-available/myproject.conf
  • Paste the following content into the file, making sure to replace "myproject" with the name of your project:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<VirtualHost *:80>
    ServerName example.com
        ServerAdmin [email protected]
    WSGIScriptAlias / /var/www/myproject/myproject/wsgi.py
    Alias /static/ /var/www/myproject/static/

    <Directory /var/www/myproject/myproject>
      <Files wsgi.py>
        Require all granted
      </Files>
    </Directory>

    <Directory /var/www/myproject/static>
      Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>
  • Enable the virtual host:
1
sudo a2ensite myproject.conf
  • Restart Apache2:
1
sudo service apache2 restart

Your Django application should now be accessible at http://example.com/. Note that you may need to modify your Apache2 configuration and/or firewall rules to allow incoming traffic to your server.