Apache2 and contextual path for virtual host with Django and AngularJS

I had the following working Django configuration:

WSGIScriptAlias / /MyDjangoProjectFolder/MyDjangoProject/wsgi.py WSGIPythonPath /MyDjangoProjectFolder <Directory /MyDjangoProjectFolder/MyDjangoProject> <Files wsgi.py> Order deny,allow Require all granted </Files> </Directory> Alias /base_context_path/static/ /MyDjangoProjectFolder/static/ <Directory /MyDjangoProjectFolder/static> Require all granted </Directory> 

Django responds to <ip>/base_context_path/rest (for the <ip>/base_context_path/rest APIs invoked by the interface) and <ip>/base_context_path/admin (for administration that uses base_context_path / static). So, all that Django requires is on <ip>/base_context_path/* .

Now I need to deploy the site developed in Angular on the same Apache2, so I'm trying to figure out how to make it work. I have a domain name for this site (mydomainname.org), but not specific to the django application. When visiting my domain name, I expect my site to appear.

This is my attempt for my site:

 <VirtualHost *:80> ServerName mydomainname.org DocumentRoot /MyWebSiteFolder DirectoryIndex index.html <Directory "/MyWebSiteFolder"> # redirect rules for managing AngularJS </Directory> </VirtualHost> 

Doesn't work (403).

For my Django application, this is the virtual host I created that does not work (403):

 <VirtualHost *:80> ServerName mydomainname Alias /base_context_path/static/ /MyDjangoProjectFolder/static WSGIScriptAlias / /MyDjangoProjectFolder/MyDjangoProject/wsgi.py WSGIDaemonProcess MyDjangoProject python-path=/MyDjangoProjectFolder:/usr/local/lib/python2.7/site-packages WSGIProcessGroup MyDjangoProject <Directory /MyDjangoProjectFolder/static> Options -Indexes Require all granted </Directory> 

So, I’m kind of stuck.

+9
python django apache virtualhost
source share
2 answers

For the directory you need to add the following directive:

  Require all granted 

or some equivalent for directory access. Apache may not have access to view this directory or configure it to access it. You must also ensure that Apache has read, write, and execute permissions.

Also check the apache logs for more information on the (403) error that occurs.

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

0
source share

In order for your Django application to work under Apache, you must have mod_wsgi installed (it is recommended to use the CMMI method to make sure that you compile it for the same version of Python that you use to create a virtual environment).

If you do not have the httpd.conf file, edit /etc/apache2/apache2.conf and write the following line:

Include /etc/apache2/httpd.conf

And get down to creating httpd.conf and add the following content to it:

 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> 

(note): You can also include 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 this:

 <VirtualHost *> WSGIDaemonProcess MyDjangoProject WSGIProcessGroup MyDjangoProject WSGIScriptAlias / /MyDjangoProjectFolder/MyDjangoProject/wsgi.py <Directory /MyDjangoProjectFolder/MyDjangoProject> Require all granted </Directory> <VirtualHost> 

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

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

You might want to do this because when you activate a virtual environment from a WSGI file, only the site-packages directory from the python virtual environment is used. If you accidentally skip installing the package from requirements.txt , but they are installed in the main Python installation, they will be used from there, and if you have the wrong version or other dependencies, you may get some errors.

The next step is to modify wsgi.py by adding these 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 parsing this mod_wsgi file, activate the virtual environment in which all your dependencies are located for proper operation.

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

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

Also remember to set DEBUG = False and edit ALLOWED_HOSTS = ['.mydomainname.org']

0
source share

All Articles