404 error when trying and unable access the sub urls but the server name is running - apache

I am deploying Flask app with httpd 2.4 by using mod_wsgi 4.5 version.
My servername sample.server.com is running fine but when i try with sub url like sample.server.com/app1, am encountering 404 error.
I also tried with localshost/app1 its is working, that means my flask app is tottaly fine but there is some configuration issue which i couldnt figure out.
I am providing with wsgi file and virtualhost config, i tried reading many articles and blogs but i couldnt find the solution.I would appreciate any help, Thanks
app.wsgi-->
activate_this = '/home/nmapi/venv/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
from werkzeug.wsgi import DispatcherMiddleware
import sys
import logging
print(sys.path)
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"home/nmapi/project/dir")
from run import *
my vurtual host config is
#ServerName sample.server.com
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias /test_wsgi
/home/nmapi/project/dir/datascience.wsgi
<Directory /home/nmapi/project/dir/>
#WSGIProcessGroup score
#AllowOverride All
Require all granted
#Allow from all
</Directory>
Alias /static /home/nmapi/project/dir/static
<Directory /home/nmapi/project/dir/static/>
WSGIProcessGroup score
#WSGIApplicationGroup %{GLOBAL}
#AllowOverride All
Require all granted
#Allow from all
</Directory>
ErrorLog logs/error.log
#ErrorLog /tmp/error.log
LogLevel debug
#CustomLog /tmp/access.log combined
CustomLog logs/access.log combined

Related

App not responding after flask application Apache deployment(mod_wsgi)

I'm trying to deploy my flask application on Apache web server using mod_wsgi.
After the deployment, when i go to the 'healthCheck' URL i configured to return a simple text message, the app is not responding and it's timing out.
This is my wsgi file:
<VirtualHost *:5000>
ServerName <My ip address here>
ServerAdmin admin#mywebsite.com
WSGIScriptAlias / /var/www/ReleaseServices/ReleaseServices.wsgi
<Directory /var/www/ReleaseServices/ReleaseServices/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/ReleaseServices/ReleaseServices/static
<Directory /var/www/ReleaseServices/ReleaseServices/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I don't see any error in the apache logs as well. What could be the issue?
Please ask me if any extra details are required.
So i found the solution after searching a lot.
I was using scikit-learn in my init.py script and the import statement was causing issues and making the app unresponsive. After removing the import, it was working fine.
When i searched for solutions, i found some interesting facts related to the WSGI configuration, one of which i had missed out:
http://blog.rtwilson.com/how-to-fix-flask-wsgi-webapp-hanging-when-importing-a-module-such-as-numpy-or-matplotlib/
In that link, check the comment by Graham Dumpleton:
The specific solution is forcing use of the main interpreter by setting the application group to %{GLOBAL}. It is documented in
http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API
Basically you have to add the following line to your .wsgi file:
WSGIApplicationGroup %{GLOBAL}
This will force a specific WSGI application to be run within the very first Python sub interpreter created when Python is initialised and hence solves the problem!

Firebase from Flask deployment server

in development mode with Flask, I am successfully able to read and write data to Firestore using the firebase admin module. However, once I deploy the app on Apache 2 using mod_wsgi, everything works fine (firebase app initialization doesn't throw any error) until I start getting a document, where the code gets stuck and the page loads forever.
Flask function where the problem occurs in __init__.py:
#app.route('/users/<user>')
def login(user=None):
if not user:
abort(404)
userRef = db.collection('users').document(user)
print("1") # This runs.
userDoc = userRef.get() # The code gets stuck here. No error message is ever returned or stored in the error log.
print("2") # This never runs.
flaskapp.wsgi
#!/usr/local/bin/python3.8
import sys
import os
import logging
logging.basicConfig(stream=sys.stderr)
os.chdir("/var/www/html/example.com/FlaskApp/")
sys.path.insert(0,"/var/www/html/example.com/FlaskApp/")
from __init__ import app as application
example.com.conf
LoadModule wsgi_module "/home/username/.local/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so"
WSGIPythonHome "/usr/local"
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin username#example.com
ServerName example.com
ServerAlias www.example.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /var/www/html/example.com/public_html
WSGIScriptAlias / /var/www/html/example.com/FlaskApp/flaskapp.wsgi
<Directory /var/www/html/example.com/FlaskApp/>
Require all granted
</Directory>
Alias /static /var/www/html/example.com/FlaskApp/static
<Directory /var/www/html/example.com/FlaskApp/static/>
Require all granted
</Directory>
# Log file locations
LogLevel warn
ErrorLog /var/www/html/example.com/log/error.log
CustomLog /var/www/html/example.com/log/access.log combined
</VirtualHost>
Do you know how to make Firebase get() works in a deployed Flask app? Thank you for your help!
Seems like WSGI needs to be forced to use the main Python interpreter. Therefore, you'll need to add application-group=%{GLOBAL} to the end of your ``WSGIScriptAlias line on example.com.conf, so that the line now reads:
WSGIScriptAlias / /var/www/html/example.com/FlaskApp/flaskapp.wsgi application-group=%{GLOBAL}
After that, restart Apache with the terminal command systemctl restart apache and your problem should be solved.

using flask virtualenv on remote host (not locally)

I have a CentOS 7 WSGI enables Apache Httpd server (on a VirtualBox machine) and it serves my developing Flask site at http://www.myflask.com on port 80.
Since I can't debug there (even if I tried everything I could), like I can on a local version set in a virutalenv at http://127.0.0.1:5000, I was trying to do this:
setting up a virtualenv (with flask installed) on the VB server, called myflaskv (/var/www/myflaskv)
putting myflask dir into myflaskv (/var/www/myflaskv/myflask)
I did end with the following httpd.conf virtual host section:
<VirtualHost *:80>
DocumentRoot "/var/www/myflaskv/myflask"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/myflaskv/myflask">
Options Indexes FollowSymLinks MultiViews
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /var/www/myflaskv/myflask/index.wsgi
ServerName www.myflask.com:80
</VirtualHost>
I can load the page http://www.myflask.com (even with virtualenv activated) but I can't load the page http://www.myflask.com:5000 as I would suppose to debug.
So I tried to add a virtualhost on port 5000:
<VirtualHost *:5000>
DocumentRoot "/var/www/myflaskv/myflask"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/myflaskv/myflask">
Options Indexes FollowSymLinks MultiViews
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /var/www/myflaskv/myflask/index.wsgi
ServerName www.myflaskv.com:5000
</VirtualHost>
supposing that this way I could load http://www.myfaskv.com, and instead I can't (ERR_CONNECTION_REFUSED).
By the way I temporarly disactivated ip-tables and se-linux.
So is there a way to load from browser the 127.0.0.1:5000 ip address of a remote host?
Or there is a better way to debug flask in an wsgi apache dev environment?
If you want to access the url from outside the box, you will have to run the server at 0.0.0.0:5000.
The url http://127.0.0.1:5000 cannot be accessed from outside the box you are running it in.
When you use wsgi to access your app, the error messages and stack traces are written to the web server error log, by default at /var/log/apache2/error.log. You don't need to use the virtualenv web server. Just write any debug output you need to a local log file (if you have debugging input you want to print) and look for the stack traces in the apache error log.

How to access multiple copies of wsgi site on server

I have a Flask web site providing a service on an apache server, and now would like to run another copy of the code base for testing purposes on the same server (a different svn branch). So, I've installed the codebase in a different place on the server, and added another WSGIScriptAlias entry, referencing the test codebase, to the apache conf file:
# Entry point for the user web interface:
WSGIScriptAlias /mysite /blah/blah/wsgi_entry.py
# Entry point for the test branch of the user web interface:
WSGIScriptAlias /mysiteTEST /blah/blah/testBranch/wsgi_entry.py
I was hoping that sending the browser to "mysiteTEST" instead of the usual "mysite" would have me running the code in the test branch. However it runs the original code, presumably because the wsgi_entry.py simply does something like:
from my_main_module import app as application
and where it's looking for my_main_module is presumably the python path set in the apache config, like so:
WSGIPythonPath /blah/blah/main_code_place
Which is correct for the main site, but I want to mysiteTEST to be running the modules in the test branch location. So perhaps I can override the python path somehow in the testBranch/wsgi_entry.py (and perhaps not?), but is there a simpler way that I can manage this in the apache config? I.e. can I somehow specify one WSGIPythonPath for /mysite, and another for /mysiteTEST?
Set up multiple virtualhosts:
<VirtualHost your.ip:80>
ServerName blahblah
ServerAdmin blahblah#blah.com
WSGIDaemonProcess blahblah user=b group=lah threads=5
WSGIScriptAlias /mysite /blah/blah/wsgi_entry.py
<Directory /blah/blah>
Options Indexes FollowSymLinks Includes ExecCGI
WSGIScriptReloading On
WSGIProcessGroup blahblah
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost your.ip:80>
ServerName blahblahTEST
ServerAdmin blahblah#blah.com
WSGIDaemonProcess blahblahtest user=b group=lah threads=5
WSGIScriptAlias /mysiteTEST /blah/blah/testBranch/wsgi_entry.py
<Directory /blah/blah/testBranch>
Options Indexes FollowSymLinks Includes ExecCGI
WSGIScriptReloading On
WSGIProcessGroup blahblah
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I ended up solving this by adding a single line to modify the path in the wsgi entry point for the alternate code version (i.e. the file referenced in the apache config line "WSGIScriptAlias /mysiteTEST /blah/blah/testBranch/wsgi_entry.py" in my example). The line overrides the wsgi configured python search path. Not the apache config only solution I wanted, but it's only a single line addition and it does the job:
import sys
sys.path.insert(0, '<path for alternate code modules>')
from my_main_module import app as application

symfony 1.4 and Apache access/error logs

Symfony 1.4 is set up as the only virtual host in my Apache config. Should I make use of the Apache access logs and error logs or does symfony take care of both of these for me?
Apache logs are not the same as the logging provided by symfony. Apache logs are useful for other reasons than symfony's. I usually use the following virtual host config for my local development.
<VirtualHost *:80>
ServerName dev.yoursite.com.br
DocumentRoot "/home/you/dev/sfprojects/yoursite/web"
DirectoryIndex index.php
<Directory "/home/you/dev/sfprojects/yoursite/web">
AllowOverride All
Allow from All
</Directory>
Alias /sf /home/you/dev/sfprojects/yoursite/lib/vendor/symfony/data/web/sf
<Directory "/home/you/dev/sfprojects/yoursite/lib/vendor/symfony/data/web/sf">
AllowOverride All
Allow from All
</Directory>
ErrorLog "/home/you/dev/sfprojects/yoursite/log/error.log"
CustomLog "/home/you/dev/sfprojects/yoursite/log/web.log" combined
</VirtualHost>
For symfony, I configure the factories.yml like the following
dev:
logger:
class: sfAggregateLogger
param:
level: notice
loggers:
sf_file_err:
class: sfFileLogger
param:
level: err
file: %SF_LOG_DIR%/%SF_APP%_%SF_ENVIRONMENT%_error.log
sf_file_notice:
class: sfFileLogger
param:
level: notice
file: %SF_LOG_DIR%/%SF_APP%_%SF_ENVIRONMENT%_notice.log