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']