Apache 2 + mod_wsgi + WSGIScriptAlias - python

I am currently doing research whether Python and Django are fit for a project that I'm going to work on (so far it looks good). As a means of a test, I want to get python running on an actual server (apache2 on ubuntu), using mod_wsgi, but I just can't make it work. Here is my httpd.conf (located at /etc/apache2/httpd.conf):
WSGIScriptAlias /test/tc-test/ /var/www/stage/hello/tc-test/django.wsgi
WSGIPythonPath /var/www/stage/test/tc-test/
<Directory /var/www/stage/test/tc-test>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
My django.wsgi looks as follows:
import os
import sys
sys.path.append('/var/www/stage/test/tc-test')
os_environment['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
The url for this directory will be http://stage.website.com/test/tc-test
Strangely enough, when I reboot the apache server, I keep getting 500 internal server errors. Not just for that subdirectory, but for all other subdirectories on the server. I looked at the apache error log, but for some reason no error is being logged at all. I managed to narrow down the culprits to WSGIScriptAlias and WSGIPythonPath: when I remove both of them, the 500 internal server errors disappear, and when I add any of them, they appear again. I have confirmed that mod_wsgi is working like it should, and it's probably something very simple that I'm looking over. I just can't find out what. Any help on this?

Try:
WSGIScriptAlias /test/tc-test /var/www/stage/hello/tc-test/django.wsgi
You shouldn't have trailing slash on first argument.

Related

Unable to connect to django 1.11 web app on Apache server

I am no expert on Django. I am able to run the app on my local machine well. I want to deploy this app on our internal server.
Versions I am using: Python: 2.7, Django 1.11, Apache: 2.4
Ports Open: 9991
When I access the port from any machine, it gives me no error. For e.g. xxx.xx.xxx.xxx:9991 shows Apache page.
But when I add the URL to the app
xxx.xx.xxx.xxx:9991/WebApp1Url
it gives error.
Below is my configuration of httpd.conf
Listen 9991
<VirtualHost *:9991>
Alias /static "C:/xxx/DjangoSite/WebApps/WebApp1/static"
<Directory "C:/xxx/DjangoSite/WebApps/WebApp1/static">
Require all granted
</Directory>
<Directory "C:/xxx/DjangoSite/WebApps/WebApps">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Below is my wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "WebApps.settings")
application = get_wsgi_application()
from WebApp1.wsgi import WebApp1
application = WebApp1(application)
settings.py contains:
WSGI_APPLICATION = 'WebApps.wsgi.application'
I can assure that the paths to static and wsgi.py are correct. I can't figure out what is not right. Can someone please provide pointers as to which step is missing? Any help is appreciated. Pardon any typos above. Thank you in advance.
The first stop for any problems, new user or experienced, should be Django's very detailed, extensive, up-to-date, versioned documentation. There's even a section for deploying a Django project with Apache HTTPD.
Some stuff quoted below, but broadly:
Install Apache HTTPD with mod_wsgi (installing the module on Windows is probably the hard part, the mod_wsgi docs link to this guide)
Add WSGI directives to a conf file
The official mod_wsgi documentation is your source for all the details about how to use mod_wsgi. You’ll probably want to start with the installation and configuration documentation.
Basic configuration
Once you’ve got mod_wsgi installed and activated, edit your Apache server’s httpd.conf file and add the following. If you are using a version of Apache older than 2.4, replace Require all granted with Allow from all and also add the line Order deny,allow above it.
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonHome /path/to/venv
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

How to keep python server running

I need a bit of help here. I am new to python and django. For my project i am developing an system which deals with api fetching and insertions. I need this system to be online 24 * 7. so i need to the server to be running all the time.
Using the traditional method of:
python manage.py runserver
runs perfectly.
I even tried :
nohup python manage.py &
but when i access my project with the url. Its showing that it project is not online. How do i fix this?
If you are using Apache then mod_wsgi should be used along with Django for deployment. This is covered here in great detail.
If you want step by step guide then you can have a look at Digital Ocean's Guide.
Your Apache configuration will look like following:
<VirtualHost *:80>
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
<Directory /home/user/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/user/myproject python-home=/home/user/myproject/myprojectenv
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py
</VirtualHost>
You will need to create a wsgi.py file which looks like following(the default one):
"""
WSGI config for myproject project.
It exposes the WSGI callable as a module-level variable named
```application```.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Hope this helps.

404 when trying to deploy basic Flask application with WSGI on Raspberry Pi [duplicate]

This question already has answers here:
Add a prefix to all Flask routes
(15 answers)
Closed 6 years ago.
I've been following the instructions on http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/ to get my Flask application to be deployed on Apache through WSGI on a Raspberry Pi running Raspbian, but whatever I try, I keep getting a 404-error on the location I've specified. I used the info on various webpages I could find (especially the instructions on http://www.ashokraja.me/post/Serving-a-Python-Flask-Web-Application-via-Apache-Webserver-in-Raspberry-Pi.aspx), but nothing seems to work out.
Logged in as root, I created a directory /var/www/flasktest in which I put two files:
flasktest.py, containing:
#!/usr/bin/python
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
and flasktest.wsgi containing:
#!/usr/bin/python
import sys
sys.path.insert(0, '/var/www/flasktest')
from flasktest import app as application
I then did a chmod ugo+x on both files (don't know if that's necessary, though).
I also created /etc/apache2/sites-available/001-flasktest.conf containing
<VirtualHost *:80>
ServerName localhost
WSGIDaemonProcess flasktest threads=5
WSGIScriptAlias /flasktest/ /var/www/flasktest/flasktest.wsgi
<Directory /var/www/flask/flasktest>
WSGIProcessGroup flasktest
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
I executed a2enmod wsgi, a2ensite 001-flasktest.conf and service apache2 reload. Every command was executed as root user.
When I go now go to http://localhost/flasktest I get a 404-error. http://localhost gives me the default Apache-page, so apparently Apache is running correctly. As far as I can see, Apache does not generate any error messages at all.
I really don't know what goes wrong: what makes the 404 come up? Can somebody help me out here? Thanks in advance!
Double-check your WSGIScriptAlias variable- That should be (according to the docs)
WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi
Whereas you have
WSGIScriptAlias /yourapplicaiton/ /var/www/yourapplication/yourapplication.wsgi
As Philip Tzou also mentioned, your Directory path may not be correct, either.
Further, sometimes the server name localhost can give you problems. Use 127.0.0.1:80 instead.
It looks like you passed a wrong path to <Directory /> accidentally. Try change /etc/apache2/sites-available/001-flasktest.conf to this:
<Directory /var/www/flasktest>
...
</Directory>
Also you can take a look of apache error log to see what exactly happened.

Apache set-up with Flask and wsgi

I have a small web application that I have built using Flask and python. With the internal server that I used for developing everything runs fine. However now I want to use apache to start using it. But it doesn`t work. Keep in mind that I have never worked with apache or web based stuff before.
I used this guide as my starting point:
http://flask.pocoo.org/docs/deploying/mod_wsgi/
right now I have my application which is in the file called "/rg/server.py" and looks like this:
app=Flask(__name__)
# all app routes...
if __name__ == '__main__':
app.run(
debug=True,
host="127.0.0.1",
port=80
)
than I have a wsgi file as "/rg/wsgi/minerva.wsgi"
import sys
sys.path.insert(0, /rg)
from server import app as minerva
and finally I have an apache config file in "etc/apach2/sites-available/minerva.com":
<VirtualHost *>
ServerName minerva.test
WSGIDaemonProcess minerva threads=10
WSGIScriptAlias / /rg/wsgi/minerva.wsgi
<Directory /rg>
WSGIProcessGroup minerva
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Then I updated apache with a2ensite minerva.com which succeded. Then I releaded Apache and no errors. However I cannot acces minerva.test in any way...
If I type in apache2ctl -S it does list minerva.test
I have no idea what is going wrong...
system information:
OS: debian 64bit
python 2.7
The WSGI application entry point must be called 'application' for mod_wsgi. You have:
from server import app as minerva
It should be:
from server import app as application
You aren't even getting that far though, else the line:
sys.path.insert(0, /rg)
would give a syntax error.
Going back further, instead of:
<VirtualHost *>
you should have:
<VirtualHost *:80>
and finally, if 'minerva.test' isn't actually a resolvable host, you will not get anywhere.
So fill out your question with the actual URL you are using in the browser and also indicate whether 'minerva.test' is even listed in local hosts file.
The first thing I would check would be to make sure mod_wsgi is installed and loaded by apache. If that's fine, your setup looks pretty similar to mine with a few minor differences:
I had to add WSGISocketPrefix /var/run/wsgi above my VirtualHost definitoin. See here.
I included user and group values on the WSGIDaemonProcess line.

Python Linking Problem while running Django on Apache with mod_wsgi: symbol not found __cg_jpeg_resync_to_restart

I get this error when I try to run my django site on apache. The site works on the development server:
ViewDoesNotExist at /
Could not import myproject.modulename.views. Error was: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_tkinter.so, 2): Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /Applications/MAMP/Library/lib/libjpeg.8.dylib
I am not sure how to resolve the issue. Other django sites work on this apache installation. The directory is on the path specified in my apache.conf file (see bottom of the post).
The three files referenced in the error message exist in the locations indicated.
_tkinter.so - /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_tkinter.so
ImageIO - /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/ImageIO
libjpeg.8.dylib - /Applications/MAMP/Library/lib/libjpeg.8.dylib
I am not sure what __cg_jpeg_resync_to_restart is.
Below is the original message I put up when I was trying to figure out what the error message meant. Thanks to sacabuche for pointing me in the right direction.
I am trying to get a Django Site to run on apache. It works on the django development server, but I get this error when I try to run it on apache with mod_wsgi. I know mod_wsgi works because I had a small trial site (that was a scaled down version of this stie) working on my mamp development server. A feat I managed with the help of others via this post: Django/mod_wsgi and PHP as Virtual Hosts on same Apache Server using MAMP
The new site uses a different database and is now at the localhost root, but otherwise is very similar, so this error is baffling me.
Here is the code for my apache conf (note: the php site works, and if I redirect the WSGIDaemonProcess to the old site, it loads without problem):
ServerName localhost
UseCanonicalName Off
DocumentRoot "/Applications/MAMP/htdocs"
Alias /phpsite /Applications/MAMP/htdocs/phpsite
<Directory "/Applications/MAMP/htdocs/phpsite">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess site1 display-name=%{GROUP}
WSGIProcessGroup site1
Alias /media/ /Applications/MAMP/htdocs/media/
<Directory /Applications/MAMP/htdocs/media>
Options ExecCGI
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /Users/sequoia/djangoprojects/myproject/apache/django.wsgi
<Directory /Users/sequoia/djangoprojects/myproject/apache>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Thanks.
Well it seems that a symlink is broken, so i looked and i found something in this forum
delete the symlinks "libpng.dylib" and "libjpeg.dylib" in "<installation directory>/lib"
Create new symlinks to the right libs.
ln -s /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libPNG.dylib /Applications/MAMP/Library/lib/libPNG.dylib
ln -s /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libJPEG.dylib /Applications/MAMP/Library/lib/libJPEG.dylib
I have no Mac rigth here, but first i would verify that the origin of the symlink exist, i hope this will solve your problem.
Don't you miss the + or - sign in your options statement?
Example of statement:
Options -Indexes +FollowSymLinks

Categories

Resources