[Fixed]-Apache2 and context path for virtual host with Django and AngularJS

1👍

For the Directory you need to add the follow directive:

  Require all granted 

or some equivalent for the access to the directory. Apache might not have access to view that directory or be configured to have access to it. You also need to make sure that Apache has permissions to read, write and execute.

Also check the apache logs for additional information on the error (403) that is happening.

I would also recommend, like Aki003, using nginx as it is easier to configure and use with uwsgi.

1👍

For your Django app to work under Apache, you should have mod_wsgi installed (it is recommend to use CMMI method so that you make sure you compile it for the same python version you are using to create the virtual environment).

If you don’t have httpd.conf file, edit /etc/apache2/apache2.conf and write this line:

Include /etc/apache2/httpd.conf

And proceed to create httpd.conf and inside it add the following content:

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

WSGIPythonHome /some/path/where/is/an/empty/venv

Alias /static/ /MyDjangoProjectFolder/MyDjangoProject/static/

<Directory /MyDjangoProjectFolder/MyDjangoProject/static>
    Require all granted
</Directory>

WSGIScriptAlias / /MyDjangoProjectFolder/MyDjangoProject/wsgi.py process-group=MyDjangoProject

<Directory /MyDjangoProjectFolder/MyDjangoProject>
<Files wsgi.py>
    Require all granted
</Files>
</Directory>

(side note): You might also want to include the security headers here:

Header set X-Frame-Options SAMEORIGIN
Header set X-XSS-Protection 1;mode=block
Header set X-Content-Type-Options nosniff
Header set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"

Your VirtualHost file should look like that:

<VirtualHost *>
    WSGIDaemonProcess MyDjangoProject 
    WSGIProcessGroup MyDjangoProject

    WSGIScriptAlias / /MyDjangoProjectFolder/MyDjangoProject/wsgi.py 

    <Directory /MyDjangoProjectFolder/MyDjangoProject>
        Require all granted
    </Directory>
<VirtualHost>

According to mod_wsgi documentation, this is the configuration for using the Daemon Mode for multiple apps (you might want to add or test other apps, so this is a good preparation).

/some/path/where/is/an/empty/venv is the path where you would want to create an empty virtual environment (using virtualenv !! so that you have access to activate_this.py script !!)
Make sure to create and activate another virtual environment inside you django project where you will install all your dependencies.

You might want to do so because when you are activating virtual environment from within the WSGI file, only the site-packages directory from the python virtual environment is being used.
If you by any chance omit to install a package from requirements.txt, but those are installed in the main python installation, they would be used from there and if there is a wrong version or has different dependencies, you might get some errors.

Next step is to modify wsgi.py by adding those lines first (before any import):

python_home='/path/to/the/MyDjangoProjectFolder/venv'
activate_this=python_home+'/bin/activate_this.py'
with open(activate_this) as file_:
        exec(file_.read(), dict(__file__=activate_this))

so that when mod_wsgi is parsing this file, to activate the virtual environment where all your dependecies are, in order to function properly.

Also, in the same file(wsgi.py), add the path to your django project to sys path:

import sys
sys.path.append('/path/to/the/MyDjangoProjectFolder')

Also, do not forget to set
DEBUG = False and edit ALLOWED_HOSTS = ['.mydomainname.org']

1👍

Here is my setup template I use every time for Apache2.

    Apache2:
    sudo apt-get install apache2 libapache2-mod-wsgi-py3
    cd /etc/apache2/sites-available/

    sudo nano 000-default.conf

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        

        ErrorLog /home/user/site/logs/error.log
        CustomLog /home/user/site/access.log combined

        ServerName website.com
            ServerAlias www.website.com

        Alias /static /home/user/django_project/static
        <Directory /home/user/django_project/static>
            require all granted
        </Directory>

        Alias /media /home/user/django_project/media
        <Directory /home/user/django_project/media>
            require all granted
        </Directory>

        Alias /images /home/user/django_project/images
        <Directory /home/user/django_project/images>
            require all granted
        </Directory>

        <Directory /home/user/django_project/folder_containing_settings>
            <files wsgi.py>
                require all granted
            </files>
        </Directory>
        
        WSGIDaemonProcess django_project python-path=/home/user/django_project python-home=/home/user/django_project/env
        WSGIProcessGroup django_project
        WSGIScriptAlias / /home/user/django_project/folder_containing_settings/wsgi.py


    </VirtualHost><span style="font-weight: 400;">


        
    sudo apachectl configtest

1👍

You have to create two configuration file one for django app and another for frontend app

first you can make it by domain and subdomain
frontend example.com
backend api.example.com

or you change
WSGIScriptAlias / to WSGIScriptAlias /base_context_path

and fronted will by default

👤faruk

Leave a comment