Im trying to host a flask app with an apache2 server. The server works but I'm only seeing a list of files, the wonderful "index of" page. My code is pretty simple. This is my hello.py file in /var/www/flask_dev:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(host='0.0.0.0')
I also created an apache config file located in /etc/apache2/sites-available/flask_dev.conf:
ServerName example.com
<VirtualHost *:80>
ServerAdmin webmaster#localhost
WSGIDaemonProcess hello user=www-data group=www-data threads=5 python-path=/var/www/flask_dev
WSGIScriptAlias / /var/www/flask_dev/start.wsgi
<Directory /var/www/flask_dev>
WSGIProcessGroup hello
WSGIApplicationGroup %{GLOBAL}
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And the needed wsgi file also located in /var/www/flask_dev/start.wsgi:
from hello import app as application
import sys
sys.stdout = sys.stderr
I'm not sure what I did wrong, I just followed a simple tutorial.
Thanks for your help :)
You probably did not install mod_wsgi module for Apache.
http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/
Apache needs to import the mod_wsgi module for it to work with python. Further instructions for installation can be found at.
https://code.google.com/p/modwsgi/wiki/QuickInstallationGuide
Once installed, edit your httpd.conf with LoadModule wsgi_module modules/mod_wsgi.so
If you are on Windows, you will have to download the appropriate mod_wsgi.so for the python version and architecture. Rename the file to mod_wsgi.so if it has any python specific version naming and set conf to LoadModule.
Related
I'm trying to run a dash app on our website in a sub-directory. I'm getting a 403 forbidden when trying to access to website.
In the /etc/httpd/logs/ssl_error_logs i get the error:
[Tue Jan 03 13:53:42.973046 2023] [autoindex:error] [pid 50576] [client 134.94.71.5:60550] AH01276: Cannot serve directory /var/www/Typesetter/appname/: No matching DirectoryIndex (index.html,index.php,index.php) found, and server-generated directory index forbidden by Options directive
I think i might have something wrong with either the permissions or the virtualhost setup. But i can't figure out what is going wrong.
I want to run the dashboard on: our-group.our-uni.com/appname
The server is running on centos7.
I got a data folder with biochemical information to provide to the users in /var/www/html/appname/data/ from which i can download the data if i go to our-group.our-uni.com/appname/data/datapoint1.pt.
Since this works and i can also put a index.html/index.php page there without issues, i think my virtualhost setup pointing to WSGI is incorrect?
The error says cannot serve directory /var/www/Typesetter/appname/, but this directory is a symbolic link to /var/www/html/appname/.
The virtualhost file appname.conf located in /etc/httpd/conf.d/:
<VirtualHost *:80>
ServerName our-group.our-uni.com
DocumentRoot /var/www/html/appname
Errorlog /var/www/html/appname/appname-error.log
Alias /topenzyme /var/www/html/topenzmye
WSGIDaemonProcess appname user=apache group=apache threads=5 python-path=/var/www/html/appname/appname-env
WSGIScriptAlias /appname /var/www/html/appname/testapp.wsgi
<Directory /var/www/html/appname>
WSGIProcessGroup appname
WSGIApplicationGroup %{GLOBAL}
Options Indexes FollowSymLinks ExecCGI
Require all granted
</Directory>
</VirtualHost>
I also tried changing to <Directory /var/www/typesetter/appname>, but this also did not work.
The contents of testapp.wsgi in /var/www/html/appname/:
import sys, os
sys.path.insert(0, '/var/www/html/appname')
from testapp import app as application
The contents of testapp.py in /var/www/html/appname/:
from flask import Flask
app = Flask(__name__)
#app.route('/topenzyme/')
def main():
return 'Hello world'
if __name__ == '__main__':
app.run()
Permission setup:
-rwxr-xr-x. 1 apache apache 148 Jan 3 13:37 testapp.py
-rwxr-xr-x. 1 apache apache 100 Jan 3 13:38 testapp.wsgi
I also tried 777, but from what i've read 755 permissions should be sufficient?
If i do httpd -M i get wsgi_module (shared).
The wsgi module should be available.
I tried various virtualhost configs from other posts with similar issues.
I'm really unsure why this is not working.
Is there anything wrong with my configuration settings or could it be something else?
Found out the issue. I was miss-configuring my Apache setup.
As we are already hosting a website on the domain, i should not have tried to setup a virtualhost for the sub-directory. From what i understand now this only works for setting up (sub)domains. With this structure of setting it up the appname.conf file gets added the already existing virtualhost config.
Instead the /etc/httpd/conf.d/appname.conf is now:
WSGIDaemonProcess appname user=apache group=apache threads=5 python-path=/var/www/html/appname/appnameenv
WSGIProcessGroup appname
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias /appname /var/www/html/appname/testapp.wsgi
<Directory /var/www/html/appname>
Options FollowSymLinks
AllowOverride All
Require all granted
allow from all
</Directory>
This corrently links the wsgi application to my sub-directory location i wanted (our-group.our-uni.com/appname)
I have a Django app which is deployed in local network using Apache + mod_wsgi under Windows. When I run python manage.py runserver, everything works fine. But when I start the Apache Service, I cannot access the app. The only response I get from the access.log is the error code 408. Below is my httpd.conf:
LoadFile "c:/users/felix/appdata/local/programs/python/python37/python37.dll"
LoadModule wsgi_module "c:/users/felix/appdata/local/programs/python/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/users/felix/appdata/local/programs/python/python37"
ServerName localhost
WSGIScriptAlias / "D:/dev/test_server/django/django/wsgi_windows.py"
Listen 8000
<VirtualHost *:8000>
WSGIPassAuthorization On
ErrorLog "logs/django.error.log"
CustomLog "logs/django.access.log" combined
Alias /static "D:/dev/test_server/staticfiles"
<Directory "D:/dev/test_server/staticfiles">
Require all granted
</Directory>
<Directory "D:/dev/test_server/django/django">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
<Directory />
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
And below is the wsgi_windows.py file:
# Activate the virtualenv
activate_this = 'D:/dev/test_server/.venv/Scripts/activate_this.py'
exec(open(activate_this).read(), dict(__file__=activate_this))
import os # noqa
import sys # noqa
import site # noqa
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('D:/dev/test_server/.venv/Lib/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('D:/dev/test_server/django')
sys.path.append('D:/dev/test_server/django/django')
os.environ['DJANGO_SETTINGS_MODULE'] = 'django.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django.settings")
from django.core.wsgi import get_wsgi_application # noqa
application = get_wsgi_application()
I'd appreciate any ideas or hints on the issue.
start apache at command line with 'httpd' and look for error messages. If Apache has a problem at startup there is no message in the log files.
check error.log
You can place 'print('xyz') even in the settings.py and elsewhere and this way by checking error.log see how your app is setup and how far a request is processed. If your app get stuck somewhere like this you find the code where it is stuck
I have the same error after install scipy library and use it in some scrip in django. I found that some libraries like "numpy" and "scipy" only work in the Python main interpreter and you have to force the WSGI to run in the global app group to run it. Adding this line in my conf file work for me.
WSGIApplicationGroup %{GLOBAL}
So, i've been working on this little project in apache2 using python and Flask microframework. I have this problem.
mod_wsgi (pid=11251): Target WSGI script '/var/www/FlaskApp/flaskr/wsgi.wsgi' cannot be loaded as Python module.
Exception occurred processing WSGI script '/var/www/FlaskApp/flaskr/wsgi.wsgi'``
File "/var/www/FlaskApp/flaskr/wsgi.wsgi", line 8, in <module>
from flaskr import app as application
ImportError: cannot import name 'app'
This is what my wsgi.wsgi file looks like.
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,'/var/wwww/FlaskApp')
from flaskr import app as application
and this what my .conf files looks like:
<VirtualHost *>
ServerName (here goes my ip)
ServerAdmin "me"
WSGIDaemonProcess wsgi
WSGIScriptAlias / /var/www/FlaskApp/flaskr/wsgi.wsgi
<Directory /var/www/FlaskAppr/flaskr>
WSGIProcessGroup wsgi
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
Alias /static /var/www/FlaskApp/flaskr/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
so, far been roaming internet, got no answers. Flask local works ok.
Yet, i still get the 500 error whenever i try to run apache2
Btw, i am using a raspberry pi as server. Stretch version OS.
any ideas?
I have an Apache (ver. 2.2.15) server (running on Linux CentOS), where I have a lot of .cgi scripts located in /var/www/cgi-bin, aliased:
ScriptAlias /cgi-bin "/var/www/cgi-bin"
And it works fine when I enter mydomain/cgi-bin/something.cgi.
Now I want to have also Flask application running within Apache server on 80 port. The app is located in /var/www/cgi-bin/app. So, I created simple Flask app - a.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
and a.wsgi file:
import sys
import site
sys.path.insert(0, '/var/www/cgi-bin/app')
site.addsitedir('/var/www/cgi-bin/app:/<my_python_path>/Lib/site-packages')
from app import app as application
Also in /etc/httpd/conf/httpd.conf I have created required virtualhost:
<VirtualHost *:80>
ServerName app.<mydomain>:80
WSGIDaemonProcess app python-path=/var/www/cgi-bin/app:/<my_python_path>/Lib/site-packages user=user1 group=user1 threads=5
WSGIScriptAlias /cgi-bin/app /var/www/cgi-bin/app/a.wsgi
<Directory /var/www/cgi-bin/app>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/www/cgi-bin/app/logs/error.log
CustomLog /var/www/cgi-bin/app/logs/custom.log combined
</VirtualHost>
But everytime I try to open mydomain/cgi-bin/app/ in the error.log file I see this error:
(...) attempt to invoke directory as script: /var/www/cgi-bin/app/
Do you have an idea what have I done wrong here?
You should not put your Flask app under cgi-bin, in fact it should not be under the DocumentRoot at all. Move it somewhere else entirely - I like /srv/, but it's up to you - and change the WSGI alias appropriately.
I am new to working with apache and mod_wsgi. But I do have little experience in django, so from some tutorials I tried to run django app through apache webserver using mod_wsgi.
I created mysite in /var/www/
then in mysite/application I created application.wsgi ...
import os
import sys
sys.path.append('/var/www/mysite/application')
os.environ['PYTHON_EGG_CACHE'] = '/var/www/mysite/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
and in /etc/httpd/sites-available I created file named mysite.conf ...
<VirtualHost *:80>
ServerName mysite.com
ServerAdmin id#somewhere.com
ServerAlias mysite.com
DocumentRoot /var/www/mysite/
<Directory /var/www/mysite>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
</Directory>
WSGIDaemonProcess mysite processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup mysite
</Virtualhost>
Then I ran a2ensite mysite.conf, didn't showed any error.
Then in /etc/httpd/hosts/ I added one line my-ipddress mysite.com
I gave permission chmod 777 to all the above files and to folder /var/www/mysite. Now when I open mysite.com on browser I see apahce's default page nothing from django.
I am using fedora 21.
You haven't put in anything in that configuration to serve your Django site via WSGI: you're missing the WSGIScriptAlias line (see the documentation:
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
Note that you shouldn't really be putting things in /var/www; and also you shouldn't need to create your own WSGI file, Django creates one for you when you create a project.