Apache - Hosting two Django projects issue - One project is very slow / unaccessible - django

I'm hosting two Django projects on my Ubuntu 20.04 server using Apache, with two different domains.
djangoproject1 is hosted on example1.com:8000
djangoproject2 is hosted on example2.com:8000
However I'm having a strange issue where only one of the two is accessible at a time. Both sites work perfectly when the other one is disabled (using "a2dissite"). But when both are enabled, only one will work and the other one loads forever until time out.
djangoproject1.conf
<VirtualHost *:8000>
ServerAdmin webmaster#localhost
ServerName example1.com
ServerAlias www.example1.com
DocumentRoot /var/www/djangoproject1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /var/www/djangoproject1/static
<Directory /var/www/djangoproject1/static>
Require all granted
</Directory>
Alias /static /var/www/djangoproject1/media
<Directory /var/www/djangoproject1/media>
Require all granted
</Directory>
<Directory /var/www/djangoproject1/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess djangoproject1 python-home=/var/www/djangoproject1/venv python-path=/var/www/djangoproject1
WSGIProcessGroup djangoproject1
WSGIScriptAlias / /var/www/djangoproject1/project/wsgi.py
</VirtualHost>
djangoproject2.conf (similar to djangoproject1.conf)
<VirtualHost *:8000>
ServerAdmin webmaster#localhost
ServerName example2.com
ServerAlias www.example2.com
DocumentRoot /var/www/djangoproject2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /var/www/djangoproject2/static
<Directory /var/www/djangoproject2/static>
Require all granted
</Directory>
Alias /static /var/www/djangoproject2/media
<Directory /var/www/djangoproject2/media>
Require all granted
</Directory>
<Directory /var/www/djangoproject2/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess djangoproject2 python-home=/var/www/djangoproject2/venv python-path=/var/www/djangoproject2
WSGIProcessGroup djangoproject2
WSGIScriptAlias / /var/www/djangoproject2/project/wsgi.py
</VirtualHost>
The apache logs shows the following error multiple times:
(11)Resource temporarily unavailable: mod_wsgi (pid=1498191): Couldn't create worker thread 6 in daemon process 'djangoproject2'.
I would highly appreciate some help with this issue as I have no idea what's causing it and I can't find any solutions to this problem. Any suggestions or advice is welcome.

Related

Host both PHP/JS and Django applications with Apache on Windows

I have a WAMP Server 3.2 (Apache 2.4.46) installed on Windows 10 (64-bits), it is exposed to the local company network. I use it to host ordinary php/js applications. My httpd-vhosts.conf is used to look like this:
<VirtualHost *:80>
ServerName RealNameOfTheServer
DocumentRoot "d:/projects"
<Directory "d:/projects/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Now I got a Django app which preferably needs to be hosted with the same server (since I don't have any other) along with other php applications. I tried to follow the example to configure my virtual hosts, but it uses daemon process which is not available on Windows.
My httpd-vhosts.conf after applied changes makes Django app work correctly but dumps php/js apps.
<VirtualHost *:80>
ServerName RealNameOfTheServer
DocumentRoot "d:/projects"
<Directory "d:/projects/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
WSGIPassAuthorization On
ErrorLog "logs/dashboard.error.log"
CustomLog "logs/dashboard.access.log" combined
WSGIScriptAlias / "d:\projects\dashboard\dashboard\wsgi_windows.py"
WSGIApplicationGroup %{GLOBAL}
<Directory "d:\projects\dashboard\dashboard">
<Files wsgi_windows.py>
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Files>
</Directory>
Alias /static "d:/projects/dashboard/static"
<Directory "d:/projects/dashboard/static">
Require all granted
</Directory>
</VirtualHost>
Is there any way to run both php and Django apps on Windows?
WSGIScriptAlias / "d:\projects\dashboard\dashboard\wsgi_windows.py"
will also catch calls to "d:/projects" - so if you want to avoid that, you need to change to something like
WSGIScriptAlias /my_wsgi_app/ "d:\projects\dashboard\dashboard\wsgi_windows.py"
If you want to avoid that the user can see that, you can use a rewrite rule for certain paths.

How to set the apache2.conf file for a django project?

I am now deploying a django test project on aws-ec2 and the AMI is Ubuntu18.04 with Python 3.6, Django 2.1, Apache2.
The project is under /var/www/Project and I am trying to add the setting to apache.conf.
The project is simply generated by django-admin startproject Project and I want make sure that when hit the public IP provided by the instance, it should show up the django default page.
WSGIDaemonProcess ubuntu processes=2 threads=12 python-path=/var/www/Project
WSGIProcessGroup ubuntu
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /var/www/Project/Project/wsgi.py
<Directory /var/www/Project/Project>
Require all granted
</Directory>
Now i got the internal server error.
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster#localhost to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
I have tried this previously. And it seems like it only works when i use python2.7 with django 1.11.
WSGIScriptAlias / /var/www/Project/Project/wsgi.py
WSGIPythonPath /var/www/Project
<Directory /var/www/Project/Project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
what's wrong with my conf file?
I finally come up with the following solution.
If you are using Apache2, Python2 and Ubuntu18.04 LTS, you can change the apache2.conf file by adding the following:
WSGIScriptAlias / [path_to_wsgi.py]
WSGIPythonPath [path_to_project]
<Directory [path_to_project]>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
If you want to use the latest Django 2+ version, change the /etc/apache2/sites-available/000-default.conf which is the settings for HTTP port 80. (000-default.conf file can't contains WSGIPythonPath in virtualhost)
<VirtualHost *:80>
ServerAdmin webmaster#localhost
Alias /static [path_to_static_folder]
<Directory [path_to_static_folder]>
Require all granted
</Directory>
<Directory [path_to_project]>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess [name] python-home=[path_to_virtualenv] python-path=[path_to_project]
WSGIProcessGroup [name]
WSGIScriptAlias / [path_to_project_wsgi.py]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Deploying Django Project with apache2

Someone can help me why it doesn't work? (my project name is ikh)
i'm using ubuntu 16.04 LTS, Apache2 , Python3
ServerAdmin webmaster#localhost
DocumentRoot /var/www/ikh
WSGIDaemonProcess ikh python-path=/var/www/ikh/ python-home=/var/www/ikh/.env
WSGIProcessGroup ikh
WSGIScriptAlias / /var/www/ikh/ikh/wsgi.py
<Directory /var/www/ikh/ikh>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
i found that some interesting problems :
1 - first should set server to listen the port that i want to deploy on it!
Listen 8000
2 - to use WSGIDaemonProcess should do like this sample that i write it for my apache2 configuration
WSGIDaemonProcess ikh python-path=/var/www/ikh python-home=/var/www/ikh/env/lib/python3.5/site-packages
and thats about it!

Configuring VirtualHost to run a second website, issue with <VirtualHost *:8080>, error with `Listen`

I am trying to figure out how to host a second Django website from my VM and I am wondering if somebody could see where I have made any obvious mistakes.
Currently whichever site is set to <VirtualHost *:80> works. I learned from this answer that I should specify the second website to <VirtualHost *:8080>. However when I try to use Listen I get the below error when I try to reload apache
Job for apache2.service failed. See 'systemctl status apache2.service'
and 'journalctl -xn' for details.
Does anyone understand what might be going wrong?
Why does <VirtualHost *:80> but not <VirtualHost *:8080>?
And why do I get the error when I specify Listen?
I am using Debian 8.5, Apache 2.4.10 and mod-wsgi 4.3.0-1.
Listen 80
<VirtualHost *:80>
ServerName myserver.scss.tcd.ie/bias_experiment/
Alias /bias_experiment/static/ /var/www/bias_experiment/static/
<Directory /var/www/bias_experiment/static>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias /bias_experiment /var/www/bias_experiment/src/bias_experiment/index.wsgi
<Directory /var/www/bias_experiment/src/bias_experiment>
<Files index.wsgi>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
Listen 8080
<VirtualHost *:8080>
ServerName myserver.scss.tcd.ie/bias_experiment_two/
Alias /bias_experiment_two/static/ /var/www/bias_experiment_two/static/
<Directory /var/www/bias_experiment_two/static>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias /bias_experiment_two /var/www/bias_experiment_two/src/bias_experiment/index.wsgi
<Directory /var/www/bias_experiment_two/src/bias_experiment>
<Files index.wsgi>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
Any help is as always, much appreciated.
You can't set ServerName as you are. The ServerName directive must be a host name only else named based virtual hosts will not work when you have multiple VirtualHost definitions. The only reason anything would be handled at all as is is because when name based virtual hosts are not set up correctly, or no host name matches, Apache will send requests to the first VirtualHost found when the configuration was read. What you should be doing is have everything in one VirtualHost if you want them to be access via the same host name. Using different ports could be used, but is less convenient.
<VirtualHost *:80>
ServerName myserver.scss.tcd.ie
WSGIDaemonProcess bias_experiment
Alias /bias_experiment/static/ /var/www/bias_experiment/static/
<Directory /var/www/bias_experiment/static>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias /bias_experiment /var/www/bias_experiment/src/bias_experiment/index.wsgi \
process-group=bias_experiment application-group=%{GLOBAL}
<Directory /var/www/bias_experiment/src/bias_experiment>
<Files index.wsgi>
Order deny,allow
Allow from all
</Files>
</Directory>
WSGIDaemonProcess bias_experiment_two
Alias /bias_experiment_two/static/ /var/www/bias_experiment_two/static/
<Directory /var/www/bias_experiment_two/static>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias /bias_experiment_two /var/www/bias_experiment_two/src/bias_experiment/index.wsgi \
process-group=bias_experiment_two application-group=%{GLOBAL}
<Directory /var/www/bias_experiment_two/src/bias_experiment>
<Files index.wsgi>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
To keep the WSGI applications separate, two separate daemon process groups are declared and each WSGI application delegated to a different process group.
The two WSGI applications would then be accessed as:
http://myserver.scss.tcd.ie/bias_experiment
http://myserver.scss.tcd.ie/bias_experiment_two
If these are Django sites, you likely will have additional setup changes you will need to make in the Django settings file, to allow both to run under the same host name and not interfere with each other.

Deploying Multiple Django projects on apache with wsgi

I am trying to give a Domain name and run multiple django projects on my apache
<VirtualHost first.site.com:80>
ServerName first.site.com
WSGIDaemonProcess first
WSGIScriptAlias / /opt/project/first/first/wsgi.py process-group=first application-group=%{GLOBAL}
ErrorLog ${APACHE_LOG_DIR}/error.log
<Directory /opt/project/first/first>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost second.site.com:80>
ServerName second.site.com
WSGIDaemonProcess second
WSGIScriptAlias / /opt/project/second/second/wsgi.py process-group=second application-group=%{GLOBAL}
ErrorLog ${APACHE_LOG_DIR}/error.log
<Directory /opt/project/second/second>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
but, second website don't work.
You don't have permission to access / on this server.
Any advice is much appreciated.
Thanks
Are all directories from / down to the directory where the WSGI script files are readable to the Apache user? That is the main reason why you get this error.
Updating Virtual Host Settings from Apache 2.2 to Apache 2.4
Apache 2.2
Order allow,deny
Allow from all
Apache 2.4
Require all granted