Apache2 error, redirects to defaultsite. Domain hosted by 1&1 Ionos - apache

I am setting up an apache2/flask hello world test site to make sure everything works, but I can't stop it from being redirected to /defaultsite.
I know I've done the setup correctly as I don't get any error messages in my logs, just a 404 url not found for mycocoop.com/defaultsite.
Domain registered with 1&1 Ionos, mycocoop.com. Could this be an issue?
I have given control of the mycocoop directory to www-data.
I've removed all the standard files created by apache2. My sites-available only contains mycocoop.conf. I have disensite'd 000-default, so my sites-enabled only contains mycocoop.conf.
mycocoop.conf contents:
<VirtualHost *:80>
ServerAdmin CensoredFor#StackOverflow
ServerName www.mycocoop.com
ServerAlias mycocoop.com
WSGIDaemonProcess mycocoop user=www-data group=www-data threads=5
WSGIProcessGroup mycocoop
WSGIScriptAlias / /var/www/mycocoop/mycocoop.wsgi
Alias /static/ /var/www/mycocoop/build
<Directory /var/www/mycocoop/build>
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/www/mycocoop/logs/error.log
CustomLog /var/www/mycocoop/logs/access.log combined
</VirtualHost>
file structure:
/var/www/mycocoop:
/api:
__init__.py
app.py
/build:
index.html #basic hello world html, currently not in use
/logs: #not relevant
/venv: #not relevant
mycocoop.wsgi
mycocoop.wsgi contents:
#!/usr/bin/python3.9
import sys
sys.path.insert(0, "/var/www/mycocoop/")
from api import app as application
api/__ init __.py contents:
from .app import app
api/app.py contents:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run

Related

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.

Deploying Multiple Flask Apps On Server

I am attempting to deploy a 2nd flask application to an apache webserver. I have added a 2nd listener to the httpd.conf file for 127.0.0.1:8868 because I am accessing the api through a gateway located on my machine. The first app I deployed was set up in a very similar fashion and works perfectly. The flask app works fine when launched from the .py file. I have attached excerpts from the httdpd.conf file, httpd-vhosts.conf file, and the web.wsgi file. It doesn't record any errors in the apache errors log and or the access logs. It will record it in the access and error logs if I have an error in the .wsgi file. Having both listeners enabled also seems to interfere with the first application listening on port 5000.
httpd listeners:
Listen 127.0.0.1:5000
Listen 127.0.0.1:8868
httpd-vhosts.conf:
<VirtualHost *:5000>
WSGIScriptAlias / C:/Users/me/web_apps/task_manager/app/index/web.wsgi
<Directory C:/Users/me/web_apps/task_manager>
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8868>
WSGIScriptAlias / C:/Users/me/web_apps/template_autoupdate/app/index/web.wsgi
<Directory C:/Users/me/web_apps/template_autoupdate>
Require all granted
</Directory>
</VirtualHost>
web.wsgi
import sys
sys.path.insert(0, "C:/Users/me/web_apps/template_autoupdate/app")
from autoupdater import app as application

run flask on apache on windows

Here's the issue.
I am trying to deploy my flask app in apache2.4 Server using mod_wsgi.After configuration,my apache server start to run on my computer.But when I visit http://127.0.0.1:5000/ the page doesn't display as my wish.
Here's my flask code.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return "Hello World!"
if __name__ == '__main__':
app.run(port=5000)
And here's my virtual host config.
<VirtualHost *:5000>
ServerAdmin example#company.com
DocumentRoot C:\flask
WSGIScriptAlias / C:\flask\test.wsgi
<Directory "C:\flask">
Require all granted
Require host ip
Allow from all
</Directory>
</VirtualHost>
My wsgi code.
import sys
#Expand Python classes path with your app's path
sys.path.insert(0, "C:/flask")
from test import app as application
#Put logging code (and imports) here ...
#Initialize WSGI app object
application = app
The page is like this:
It says 'Internal Server Error'.
Thank everyone!
remove the code
#Initialize WSGI app object
application = app
maybe it works
first remove last line from test.wsgi file.
(i.e application = app)
then check http://127.0.0.1:5000/, if it works then good,
if not then, add the following lines at the bottom of httpd.conf file.
WSGIScriptAlias / C:\flask\test.wsgi
<Directory C:\flask>
Require all granted
</Directory>
then check again http://127.0.0.1:5000/. it will work definitely.

Run multiple independent Flask apps in Ubuntu

I'm trying to run two or more Flask applications in separate virtual directories with Apache, like http://localhost/site1 for /var/www/myapps/app1 and http://localhost/site2 for /var/www/myapps/app2. Each app has its own virtual environment under an env directory.
I started with a fresh install of Ubuntu 14.04 with Apache2 (v2.4.7), removed the default site configuration with sudo a2dissite 000-default and added configuration for my two apps.
Here's the conf file for app1 at /etc/apache2/sites-available/app1.conf. The cofiguration for app2 is identical, replacing app2 for app1 (and site2 for site1.)
<VirtualHost *:80>
ServerName localhost
WSGIProcessGroup site1
WSGIDaemonProcess site1 user=myserviceuser group=myserviceuser threads=5 python-path=/var/www/myapps/app1:/var/www/myapps/app1/env/lib/python2.7/site-packages
WSGIScriptAlias /site1 /var/www/myapps/app1/application.wsgi
WSGIScriptReloading On
<Directory /var/www/mysites/app1>
WSGIApplicationGroup site1
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Then I enabled each site with sudo a2ensite app1 (and app2) then restarted the server with sudo apache2ctl restart.
Each of these apps has the following application.wsgi file in the root dir:
# put a copy of this file in the root dir of each app instance
import os, sys
# activate the virtual environment
app_dir = os.path.dirname(__file__)
# removed next two lines after a comment left below
# activate_this = os.path.join(app_dir, 'env', 'bin', 'activate_this.py')
# execfile(activate_this, dict(__file__=activate_this))
sys.path.insert(0, app_dir)
from myapp import app as application
What happens is that one of the sites runs fine, responding with the correct page when I hit it. The other gives me an Apache 404 page. There are no typos in the conf files.
It seems the configurations for the apps are clobbering each other and one is "winning." I've spent a lot of time tweaking the configurations but the only way I could make it work was if I added localhost2 to my hosts file and changed the ServerName to localhost2 in one of the app configs, which isn't desirable in my case. Can someone point me to the correct way to configure Apache? Or am I going the wrong way about this?
Ideally I'd want the configuration files to not care which host name was used to reach them, probably becoming multiple copies of this server running behind a load balancer.
I spent more time on this and, if I'm starting to understand Apache terminology and configuration a little better, I cannot use virtual hosts for this purpose. VirtualHost sections are intended for serving different host names (multiple domains or subdomains.)
For configuring parallel applications as subdirs I could have used Directory sections instead. I also didn't realize some of the WSGI* directives in the config file could appear more than once. This new knowledge allowed me to produce the following single config file that does what I wanted. So instead of enabling one Apache site for each app, I could enable a single site with the directories configured in it.
# this goes in /etc/apache2/sites-available/
<VirtualHost *:80>
ServerName localhost
# logs configuration
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIDaemonProcess site1 user=myserviceuser group=myserviceuser threads=5 python-home=/var/www/myapps/app1:/var/www/myapps/app1/env/lib/python2.7/site-packages
WSGIScriptAlias /site1 /var/www/myapps/app1/application.wsgi
<Directory /var/www/myapps/app1>
WSGIApplicationGroup site1
WSGIProcessGroup site1
Order deny,allow
Allow from all
</Directory>
WSGIDaemonProcess site2 user=myserviceuser group=myserviceuser threads=5 python-home=/var/www/myapps/app2:/var/www/myapps/app2/env/lib/python2.7/site-packages
WSGIScriptAlias /site2 /var/www/myapps/app2/application.wsgi
<Directory /var/www/myapps/app2>
WSGIApplicationGroup site2
WSGIProcessGroup site2
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
EDIT:
I later followed Graham Dumpleton's suggestion and removed the activate_this stuff from application.wsgi and changed the WSGIDaemonProcess directive lines to:
WSGIDaemonProcess site1 user=myserviceuser group=myserviceuser threads=5 python-home=/var/www/myapps/app1/env

mod_wsgi with Flask app: ImportError: No module named flask

In CentOS 6.4, I created python virtual environment in /var/www/html/venv folder. Then after activating the virtual environment, I installed all necessary python libraries for my flask application. I checked, that Flask libraries were in /var/www/html/venv/lib/python2.7/site-packages folder. I've already installed and loaded mod_wsgi. Now in my flask app, which is in /var/www/html/truckman/wsgi folder, I created truckman.wsgi file with the following content:
activate_this = '/var/www/html/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import sys
sys.path.insert(0, '/var/www/html/truckman/wsgi/')
from app import app as application
import config
application.config.from_object(config.Dev)
Also, in /etc/httpd/conf/httpd.conf I added:
<VirtualHost *>
WSGIScriptAlias / /var/www/html/truckman/wsgi/truckman.wsgi
<Directory /var/www/html/truckman/wsgi>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Now, in /var/www/html/truckman/wsgi folder, I created run.py file with the following content:
from app import app as application
import config
application.config.from_object(config.Dev)
if __name__ == "__main__":
application.run(port=5001)
Now I tested my app with flask's development server; if I execute "python run.py", my app works as expected. I can browse to localhost:5001, and the app's initial page shows up.
Then I tested my app with mod_wsgi: First killed run.py process, and restarted httpd service, and after that browsed to localhost; but it returned: "500 Internal Server Error". In /etc/httpd/logs/error_log file I found the following error message: "ImportError: No module named flask". What's wrong with my settings?
Try adding the path to your python 2.7 folder in your virtual environment.
sys.path.insert(1, '/path/to/virtualenv/lib/python2.7')
The .conf should be something like this:
<VirtualHost *>
ServerName example.com
WSGIScriptAlias / /var/www/firstapp/hello.wsgi
WSGIDaemonProcess hello python-path=/var/www/firstapp:/var/www/firstapp/env/lib/python2.7/site-packages
<Directory /var/www/firstapp>
WSGIProcessGroup hello
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Preferably in /etc/apache2/sites-available/hello.conf, so this part can be the root of the problem.
You can check a working example code in: Hello world from flask.