Django app hangs when attempting to connect to database via Apache - python

I am having difficulty troubleshooting this issue. I have a Django app running on an Ubuntu 14.04 server (with Apache 2.4 and mod_wsgi for Python 3.4). It connects to SQL Server via pymssql.
In development, the app works fine. I query the database, and the database returns the expected results.
In production (under the Apache user), however, the script hangs at the exact point that a database query is made. My browser (Chrome or Firefox) shows a spinning wheel that continues to spin as long as the browser window is open.
I have the following in my apache2.conf file:
ServerName localhost
# WSGIDaemonProcess application
WSGIPythonPath /home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
# WSGIProcessGroup application
WSGIScriptAlias / /home/production_code/school/school/wsgi.py
# Python virtualenv home
WSGIPythonHome /home/production_code/python3env
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
And the following in my sites-enabled/000-default.conf file:
<VirtualHost *:80>
ServerAdmin *****#school.edu
ServerName localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static/ /home/production_code/school/static/
<Directory /home/production_code/school/>
Require all granted
</Directory>
<Directory /home/production_code/school/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /home/production_code/school/static>
Require all granted
</Directory>
</VirtualHost>
Does anyone have any idea what might be causing this or how I might troubleshoot this? The Apache error logs and access logs are not particularly helpful in this situation, since a response to the request is never rendered. Similarly, Django debugging is also not useful here.

Instead of:
# WSGIDaemonProcess application
WSGIPythonPath /home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
# WSGIProcessGroup application
use:
WSGIDaemonProcess application python-path=/home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
WSGIProcessGroup application
WSGIApplicationGroup %{GLOBAL}
A key part of this is the WSGIApplicationGroup directive, with it being set to %{GLOBAL}.
This is to get around faulty third party extension modules for Python that don't work in sub interpreters and which can fail with a dead lock or crash.
See:
https://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API
It is also recommend you go back to using daemon mode. It is generally not a good idea to use embedded mode.
http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html

Related

Using Apache XAMPP on Windows 10 to create 1 Django website and one normal website

I have created a Django protect that works perfectly fine on windows Apache with Xampp. However, if I try to create a virtual host for a non-Django website, it doesn't work.
If I then put my Django website into a virtual host it doesn't work, but then my non-Django website does work.
By doesn't work I mean it takes me to this https://i.stack.imgur.com/DS0a5.png
Here is all my code for my Django website inside a virtual host and my other non-project in a virtual host.
#Django Website
<VirtualHost *:443 _default_:443 neostorm.us.to:443>
ServerName neostorm.us.to
ServerAlias neostorm.us.to
Alias /static "C:/xampp/htdocs/neostorm/static"
<Directory "C:/xampp/htdocs/neostorm/static">
Require all granted
</Directory>
WSGIScriptAlias / "C:/xampp/htdocs/neostorm/neostorm/wsgi_windows.py" application-group=neostorm
<Directory "C:/xampp/htdocs/neostorm/neostorm">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
ErrorLog "C:\xampp\apache\logs\neostorm_error.log"
CustomLog "C:\xampp\apache\logs\neostorm_custom.log" common
</VirtualHost>
#Non Django Website
<VirtualHost *:443 mail.neostorm.us.to:443>
ServerName mail.neostorm.us.to
DocumentRoot "C:/xampp/htdocs/webmail"
<Directory "C:/xampp/htdocs/webmail">
Require all granted
</Directory>
</VirtualHost>
Any help would be appreciated.
The problem was there was a virtual host inside the httpd-ssl.conf file. Simply delete the virtual host inside that file and create your virtual hosts inside your virtual host file or where ever you want it.
The reason you may see your index of files is because there is no index.html to open.
This is an example of a virtual host
<VirtualHost *:443>
ServerName example.com
DocumentRoot "C:/xampp/htdocs/example"
<Directory "C:/xampp/htdocs/neostorm/webmail">
AllowOverride All
Require all granted
Options +Indexes
</Directory>
SSLEngine on
SSLCertificateFile "conf/example/example.com-chain.pem"
SSLCertificateKeyFile "conf/example/example.com-key.pem"
</VirtualHost>
If there is no index file inside that document root, you will see the index/ page.
Just a footnote, Options +Indexes tells your web server to show an index page like seen in the photo. Use Options -Indexes to prevent showing the index/ page, this is beneficial if you have a static folder.

Deploying Django app : Forbidden You don't have permission to access this resource

I was deploying my django app on my VPS using the apache web server.
It works totally fine on http://192.46.209.82:8000/ but when I try to access the same using my IP address, 192.46.209.82. I get
Forbidden
You don't have permission to access this resource.
Apache/2.4.41 (Ubuntu) Server at 192.46.209.82 Port 80
Here is my conf file
nano /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#example.com
DocumentRoot /root/django/myproject
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /root/django/myproject/static
<Directory /root/django/myproject/static>
Require all granted
</Directory>
<Directory /root/django/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/root/django/myproject python-home=/root/django/myprojectenv
WSGIProcessGroup myproject
WSGIScriptAlias / /root/django/myproject/myproject/wsgi.py
</VirtualHost>
I am using python's virtual environment, where myprojectenv is my virtual env path and the pwd is /root/django
Note : I tried the answer from the question that I was getting as suggestion to this question but that did not work for me.
EDIT : Updated permission
In my case even though I set up all the permission correctly, I still was getting error so I moved my entire project and virtual environment to /opt and it worked for me for now. But I would like to know the reason why it was not working under /home even when I gave all permissions

Issue when setting-up wsgi as an alias path in apache

NOTE: This question is different from 'Add a prefix to all Flask routes' as I am trying to resolve this at apache level. Additional, the suggested fix for flask routes did not work!
Following on from this post, I'm trying to set up apache to serve PHP files by default, but point a given alias (i.e. /flaskapp) to a wsgi path. The wsgi file in turn routes requests to a python flask app.
Here's the apache config that I'm trying (under 000-default.conf):
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster#localhost
Alias / /var/www/html/
<Directory "/var/www/html">
Order Deny,Allow
Allow from all
Require all granted
</Directory>
WSGIScriptAlias /flaskapp "/var/www/flaskapp/deploy.wsgi"
<Directory /var/www/flaskapp>
Options +ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
After doing a service apache2 restart I find that requests to http://myip/flaskapp result in a 404 error. Everything else works fine.
Things I've tried so far:
Double checking all the file and folder paths (no issues found)
Using the wsgi part of the above code to set up the wsgi app as a standalone virtualhost (works fine)
Adding app.config['APPLICATION_ROOT'] = '/flaskapp' to my app.py file, as suggested the question 'Add a prefix to all Flask routes' (Didn't have any effect)
Where could I be going wrong?
Instead of:
Alias / /var/www/html/
<Directory "/var/www/html">
Order Deny,Allow
Allow from all
Require all granted
</Directory>
use:
DocumentRoot /var/www/html/
<Directory "/var/www/html">
Order Deny,Allow
Allow from all
Require all granted
</Directory>
Using '/' with Alias takes precedence over everything else including mod_wsgi's ability to intercept requests at a sub URL. So for stuff at root of the site you need to use DocumentRoot directive.

Apache Conf file issue -WSGIPythonPath error

So i'm trying to deploy a simple website using django, Apache, and wsgi. I wrote my conf file using the django guide (https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/modwsgi/) and this previous answer (Deploying Django on Apache and WSGI) but when I restart Apache I get an syntax error on line 3 stating WSGIPythonPath cannot occur within the VirtualHost section, once removed Apache restarted successfully but still does not redirect to the appropriate django website I created even after changing the hosts files so that it redirects back to my server.
Apache Version: 2.4.7
Python Version: 2.7.6
Django Version: 1.8.3
<VirtualHost *:80>
WSGIScriptAlias / /var/www/mywebsite/mywebsite/wsgi.py
WSGIPythonPath /var/www/mywebsite
ServerAdmin my_email#gmail.com
ServerName mywebsite.com
ServerAlias www.mywebsite.com
<Directory /var/www/mywebsite/mywebsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
move the line with WSGIPythonPath to the httpd.conf location

Multiple Django Apps on one Server

How is it possible to serve out multiple Django apps on multiple domains?
For example I have
djangoapp1.com and djangoapp2.com
I then have two separate apps in two separate locations
/srv/www/djangoapp1
/srv/www/djangoapp2
I am running Apache2 with mod_wsgi and I currently have the following in its httpd.conf
WSGIScriptAlias / /srv/www/app1/app1/wsgi.py
WSGIPythonPath /srv/www/app1
<Directory /srv/www/app1/system>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
I also then obviously have the virtual host and I get the django default install page, but now I want to serve up my second app, can anybody point me in the right way to do this?
Django V: 1.4.1
There are many approaches you can take here, and no simple answer - it depends on your requirements and constraints.
The Simplest Thing That Could Possibly Work is very likely the approach suggested by #Hedde - to define the WSGI configuration per-site inside a virtualhost.
A second and possibly more flexible approach would be to run each Django application inside it's own containing application server e.g. gunicorn (hopefully in a virtualenv to isolate application specific dependencies) on different ports and then use Apache or even Nginx as a proxy for application traffic.
This involves a more complicated server environment to manage, but gives you the advantage of being able to manage your applications individually.
You can reconfigure your available workers, upgrade application versions, make changes to settings.py etc for one application at a time rather than having to restart a single monolithic process.
In addition, although it is, of course possible, monitoring virtualhosts within the same Apache process is more complex than monitoring individual application server instances separately.
YMMV
You can use Apache's VirtualHosts
There's plenty examples for Django, e.g. here
One of the ways to run multiple django apps on a single server is to run one app on one port each.
How to run two apps on two different ports?
As recommended by Django, I am using wsgi to interface between apache and Django code. Tricky thing here is that you cannot use wsgi in "embedded" mode. In embedded some os resources are shared and hence leads to race conditions. Solution is to use wsgi in daemon mode. In daemon mode, as the name suggests, wsgi runs as separate processes and hence no shared resources. Your two django apps will be unaware of each other.
This is how my configuration looks. I am running apps on port 8082 and 8083. Notice the lines with WSGIDaemonProcess and WSGIProcessGroup and process-group=pas
Listen 8082
<VirtualHost *:8082>
WSGIDaemonProcess djangoapp1 processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup djangoapp1
WSGIScriptAlias /apis /home/apis/djangoapp1/xyz/config.wsgi process-group=djangoapp1
WSGIApplicationGroup %{GLOBAL}
<Directory /home/apis/djangoapp1>
Options +ExecCGI
<Files config.wsgi>
Require all granted
</Files>
</Directory>
</VirtualHost>
Listen 8083
<VirtualHost *:8083>
WSGIDaemonProcess djangoapp2 processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup djangoapp2
WSGIScriptAlias /apis /home/apis/discovery_api/nykaa/config.wsgi process-group=djangoapp2
WSGIApplicationGroup %{GLOBAL}
<Directory /home/apis/djangoapp2>
Options +ExecCGI
<Files config.wsgi>
Require all granted
</Files>
</Directory>
</VirtualHost>
How to serve both the apps on port 80 ?
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName example.com
ServerAlias localhost
ProxyPassMatch "^/(apis/v1/hello$.*)" "http://127.0.0.1:8082/$1"
ProxyPassMatch "^/(apis/v1/hi$.*)" "http://127.0.0.1:8082/$1"
ProxyPassMatch "^/(apis/v1/wassup$.*)" "http://127.0.0.1:8083/$1"
ProxyPassMatch "^/(apis/v1/howdy$.*)" "http://127.0.0.1:8083/$1"
</VirtualHost>
Two ways to access wassup and hello API:
http://example.com:8083/apis/v2/wassup?q=howsitgoing
http://example.com/apis/v2/wassup?q=howsitgoing
http://example.com:8082/apis/v2/hello?q=how are you
http://example.com/apis/v2/hello?q=howareyou
Django Code
As recommended on Django website I have replaced
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")
to
os.environ["DJANGO_SETTINGS_MODULE"] = "{{ project_name }}.settings"
Please see if this is helpful
in apache2.conf or htppd.conf
# Virtual hosts setup
NameVirtualHost *
<VirtualHost *>
ServerName example1.com
............
WSGIScriptAlias / /..../path/to/wsgi1.py
</VirtualHost>
<VirtualHost *>
ServerName example2.com
............
WSGIScriptAlias / /..../path/to/wsgi2.py
</VirtualHost>
in wsgi1.py
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings1' # or projectnaame.settings1
in wsgi2.py
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings2' # or projectname.settings2
In settings1.py & settings2.py you can make necessary databases and other configurations

Categories

Resources