0

I have multiple django projects and i want to host them under same domain eg: example.com/one<br> example.com/two

I have searched for various solutions and found the below given link which helped me alot. Is it possible to host multiple django projects under the same domain?

From the above reading , I get to know that I need mod_wsgi for this but I am confused that where to install this mod_wsgi - Do i need to install under every project folder (seperate for every myenv) or it should be installed only once . Please help me in how and where to install this mod_wsgi and finally how to host multiple projects under same domain name.

Some Code Tried By Another User With Same Problem But Also Not Working

<VirtualHost *:80>

        ServerAdmin admin@my_domain.com
        ServerName my_domain.com
        ServerAlias www.my_domain.com

        DocumentRoot /var/www/my_domain.com

        ErrorLog ${APACHE_LOG_DIR}/my_domain.com_error.log
        CustomLog ${APACHE_LOG_DIR}/my_domain.com_access.log combined

        # site_1
        Alias /site_1_project/static /var/www/my_domain.com/site_1_project/static
        <Directory /var/www/my_domain.com/site_1_project/static>
                Require all granted
        </Directory>

        Alias /site_1_project/media /var/www/my_domain.com/site_1_project/media
        <Directory /var/www/my_domain.com/site_1_project/media>
                Require all granted
         </Directory>

        <Directory /var/www/my_domain.com/site_1_project/site_1_project>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

        WSGIDaemonProcess site_1_project python-path=/var/www/my_domain.com/site_1_project python-home=/var/www/my_domain.com/site_1_project/django_env_site_1
        WSGIProcessGroup site_1_project
        WSGIScriptAlias /site_1_project/ /var/www/my_domain.com/site_1_project/site_1_project/wsgi.py

        # site_2
        Alias /site_2_project/static /var/www/my_domain.com/site_2_project/static
        <Directory /var/www/my_domain.com/site_2_project/static>
                Require all granted
        </Directory>

        Alias /site_2_project/media /var/www/my_domain.com/site_2_project/media
        <Directory /var/www/my_domain.com/site_2_project/media>
                Require all granted
        </Directory>

        <Directory /var/www/my_domain.com/site_2_project/site_2_project>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

        WSGIDaemonProcess site_2_project python-path=/var/www/my_domain.com/site_2_project python-home=/var/www/my_domain.com/site_2_project/django_env_site_2
        WSGIProcessGroup site_2_project
        WSGIScriptAlias /site_2_project/ /var/www/my_domain.com/site_2_project/site_2_project/wsgi.py


#RewriteEngine on
#RewriteCond %{SERVER_NAME} =my_domain.com [OR]
#RewriteCond %{SERVER_NAME} =www.my_domain.com
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
DragonFire
  • 3,722
  • 2
  • 38
  • 51
  • Firstly, mod_wsgi has nothing at all to do with hosting multiple projects in a single domain. Secondly, if you do want to use it to serve your projects, you follow its install instructions; what is unclear? – Daniel Roseman Jun 14 '19 at 07:57
  • what i was saying is I have surfed alot and after that i get to know about mod_wsgi way to host multiple projects on single host. I have read the way also to install mod_wsgi also and now I know how to install but the thing is where to install it because I have created seperate virtual environment for every single project .So Do i need to install it in every single virtual environment. – Aarohi kaur Jun 14 '19 at 08:13
  • And like I said, you read the instructions. mod_wsgi is installed for Apache, it is not associated with virtual environments. – Daniel Roseman Jun 14 '19 at 08:18
  • I ended up using subdomains which is easier method of solving this problem – DragonFire Oct 20 '22 at 04:41

2 Answers2

2

Installing mod_wsgi depends on what your host OS is. Check the instructions. If you're using CentOS or RedHat, I'd recommend looking at IUS Community; they provide a repository with yum installable packages for Python 3.6 and mod_wsgi. The version of mod_wsgi you install has to be compiled against the same version of Python you are running into your virtual environment.

Then you need to configure your VirtualHost properly. If you have a host at the root, it has to come last in your definition. Here's an example:

<VirtualHost *:443>
  TimeOut 300
  SSLEngine On

  ServerName mysite.example.com

  # Set to the lobal Application Group
  WSGIApplicationGroup %{GLOBAL}
  # Pass Authorizations through to the WSGI app for Django REST Framework Token Auth
  WSGIPassAuthorization On

  WSGIDaemonProcess subsite-develop-https python-home=/web/subsite-develop/venv request-timeout=300 user=apache group=apache
  WSGIProcessGroup subsite-develop-https
  WSGIScriptAlias /subsite /web/subsite-develop/config/wsgi.py process-group=subsite-develop-https
  <Directory /web/subsite-develop/config>
    Require all granted
  </Directory>
  Alias /subsite/static/ /web/subsite-develop/static/
  <Directory /web/subsite-develop/static>
    Header always set Access-Control-Allow-Origin "*"
    Require all granted
  </Directory>

  WSGIDaemonProcess django-mysite-develop-https python-home=/web/django-mysite-develop/venv request-timeout=300 user=apache group=apache
  WSGIProcessGroup django-mysite-develop-https
  WSGIScriptAlias / /web/django-mysite-develop/config/wsgi.py process-group=django-mysite-develop-https
  <Directory /web/django-mysite-develop/config>
    Require all granted
  </Directory>
  Alias /static/ /web/django-mysite-develop/static/
  <Directory /web/django-mysite-develop/static>
    Header always set Access-Control-Allow-Origin "*"
    Require all granted
  </Directory>
  Alias /media/ /var/media/mysite-www/
  <Directory /var/media/mysite-www>
    Require all granted
  </Directory>
</VirtualHost>

This example will host one site at /subsite/ and another at the root, /. Notice that the root site comes last. It also means that you won't be able to use the route /subsite/ within the root project, since Apache will have diverted it to via the WSGIScriptAlias definition.

This is also for a site with TLS; you may have to switch the 443 to 80, and remove SSLEngine On if you're not using TLS. The WSGIPassAuthorization is for Django REST Framework tokens, you can probably remove it as well, but I've left it for a more complete example. This is for Apache 2.4+, when they switched to the Require all granted syntax.

IUS Community, if on RedHat/CentOS: https://ius.io/

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
  • Your explanation was the best I found. But I have a question: Do we have to put it in a single file `.conf`? I made it into two separate files and it didn't work. – Taciano Morais Silva Jul 08 '21 at 13:34
  • 1
    @TacianoMoraisSilva I can't confirm 100%, but I've only gotten it to work in a single file as well. It may be necessary to keep it in a single file for the context of a single `ServerName`. – FlipperPA Jul 08 '21 at 21:51
0

I'll tell you how we did in our project. We have a single Django project with different routes. For example /players, /tablet. We hosted our project in two Docker containers. We have NGINX as our reverse proxy. NGINX redirects the request to the appropriate container based on the route. NGINX is exposed to the world. But, I'm not sure if it is useful for you or not.

piet.t
  • 11,718
  • 21
  • 43
  • 52
  • Hi, thanks for reply but I am totally a beginner and really dont know how to use this docker containers and nginx – Aarohi kaur Jun 14 '19 at 08:15
  • In other words, we are running two servers, one for each route. We run another server (NGINX) which will decide the above two servers based on the route. Where you want to host actually? How many tiers do you follow? – Dharanidhar Reddy Jun 14 '19 at 08:56
  • Thanks, I understand the concept now. Will try this way. – Aarohi kaur Jun 14 '19 at 09:16