Flask app on non-root url of website with Apache2 - flask

I have a Flask app that I would like to host on a subdomain / non-root url of our lab website. For example, I want mylab.com/portal to lead to the flask app. I followed numerous guides but I keep getting a 404 error.
My Directory structure:
/var/www/
-html/
-Stuff for mylab.com
-FlaskApp/
-FlaskApp.wsgi
-FlaskApp/
-__init__.py
-static/
-templates/
-...
FlaskApp.wsgi:
#!/usr/bin/python3
activate_this = '/home/cogsci-cnddcollab/FlaskApp/venv/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/var/www/FlaskApp')
from FlaskApp import app as application
/etc/apache2/sites-available/000-default.conf
WSGIRestrictStdout Off
WSGIScriptReloading On
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName mylab.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
Redirect permanent / https://mylab.com/
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
<Directory "/var/www/html">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
## FlaskApp
WSGIDaemonProcess FlaskApp_wsgi user=cogsci-cnddcollab group="domain users" threads=5
WSGIScriptAlias /portal /var/www/FlaskApp/FlaskApp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
WSGIProcessGroup FlaskApp_wsgi
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
Alias /portal/static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static>
Require all granted
</Directory>
Alias /portal/templates /var/www/FlaskApp/FlaskApp/templates
<Directory /var/www/FlaskApp/FlaskApp/templates>
Require all granted
</Directory>
</VirtualHost>
Now, when I restart the apache2 service and go to mylab.com/portal, I am hit with a 404 error. I have added APPLICATION_ROOT=/portal to my config.py file.
Any help will be greatly appreciated.

Related

Django - No video supported format and MIME type found

When I run python manage.py runserver the videos on a webpage work.
I deployed the web with apache2 and instead of the videos I see
No video with supported format and MIME type found.
Did I omit something when deploying the web? When I deployed it the first time it worked,
but now it is not working.
Here is my /etc/apache2/sites-available/001-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerName video.xyz.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /static /home/pd/pd_videowebapp/static
<Directory /home/pd/pd_videowebapp/static>
Require all granted
</Directory>
Alias /media /home/pd/pd_videowebapp/media
<Directory /home/pd/pd_videowebapp/media>
Require all granted
</Directory>
<Directory /home/pd/pd_videowebapp/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess django-app2 python-path=/home/pd/pd_videowebapp python-home=/home/pd/pd_videowebapp/env lang='en_US.UTF-8' locale='en_US.UTF-8'
WSGIProcessGroup django-app2
WSGIScriptAlias / /home/pd/pd_videowebapp/mysite/wsgi.py
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Django Image is not added to media folder

So the thing is that in production code with media files is not working (image of model is not added there). If you need some details please write! Thank you for your help )
project configuration
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /static /home/artashes/multi/static
<Directory /home/artashes/multi/static>
Require all granted
</Directory>
Alias /media /home/artashes/multi/media
<Directory /home/artashes/multi/static>
Require all granted
</Directory>
<Directory /home/artashes/multi/multisad>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/artashes/multi/multisad/wsgi.py
WSGIDaemonProcess django_app python-path=/home/artashes/multi python-home=/home/artashes/multi/venv
WSGIProcessGroup django_app
</VirtualHost>
settings
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
By the way I am using apache2 with linode server

Django site on Ubuntu 18.04 with Apache2 not working after installing SSL

I'm trying to deploy my Djang(1.10) application on Ubuntu 18.04 with Apache2 using mode_wsgi, the site was working perfectly well before setting up the SSL, but when I install the SSL certificate from Let'sEncrypt using certbot the is not loading anymore.
Here's my configurations:
Project folder path:
/home/abdul
Http configuration:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName www.orderfetchers.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /static /home/abdul/Fetchors/static
<Directory /home/abdul/Fetchors/static>
Require all granted
</Directory>
Alias /media /home/abdul/Fetchors/media
<Directory /home/abdul/Fetchors/media>
Require all granted
</Directory>
<Directory /home/abdul/Fetchors/Fetchors>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
#WSGIScriptAlias / /home/abdul/Fetchors/Fetchors/wsgi.py
#WSGIDaemonProcess django_app python-path=/home/abdul/Fetchors python-home=/home/abdul/Fetchors/venv
#WSGIProcessGroup django_app
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.orderfetchers.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Here's Https Configuration:
<IfModule mod_ssl.c>
<VirtualHost *:443>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName www.orderfetchers.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /static /home/abdul/Fetchors/static
<Directory /home/abdul/Fetchors/static>
Require all granted
</Directory>
Alias /media /home/abdul/Fetchors/media
<Directory /home/abdul/Fetchors/media>
Require all granted
</Directory>
<Directory /home/abdul/Fetchors/Fetchors>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/abdul/Fetchors/Fetchors/wsgi.py
WSGIDaemonProcess django_app python-path=/home/abdul/Fetchors python-home=/home/abdul/Fetchors/venv
WSGIProcessGroup django_app
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.orderfetchers.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.orderfetchers.com/privkey.pem
</VirtualHost>
</IfModule>
tl;dr: Check your firewalls (port 443), and use online tutorials on configs (below).
Ok so I had the exact same error that you did - my site had been working fine on port 80 with Apache2 and Django 1.10 on an Ubuntu box, and when I followed a guide for LetsEncrypt, my site would take forever to load before timing out. I am assuming that the question in your post is how to stop your site from just timing out and actually get it to load.
In my case, I was lucky because I was merely an idiot who had forgotten to open port 443 on the AWS Security Group for my Ubuntu box. However, I had some additional config that may help you:
settings.py
# SSL support
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
# session expire at browser close
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
# wsgi scheme
os.environ['wsgi.url_scheme'] = 'https'
wsgi.py
os.environ['HTTPS'] = "on"
These configs were found and compiled with the help of these reference sites (which may also help you):
https://simpleisbetterthancomplex.com/tutorial/2016/05/11/how-to-setup-ssl-certificate-on-nginx-for-django-application.html
https://www.pdxpixel.com/blog/2014/02/04/setting-up-django-site-ssl-apache-mod_wsgi-mod_ssl/
https://docs.djangoproject.com/en/dev/topics/security/#ssl-https
Without more understanding of what your system is, it's difficult to help more precisely, but this is what I found useful, so hopefully it helps.

Django, Apache2 and Nginx : Long loading time

I use Django as a technology for my website. Let's say that the domain name I use is example.com.
I use Apache2 as my main web server and Nginx to serve static files.
I first installed Apache and tested it. It worked perfectly (Without css and other static files, just the html).
I then installed Nginx (I have never used it before today) and this is where I encountered problems. Indeed when I want to access my website I get the html well as before. But the loading continues and the static files are not recovered.
I do not see any error in the Mozilla terminal.
Here is "Site-available" configuration :
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin contact#example.com
DocumentRoot /var/www/my_site
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
apache.conf :
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
#<Directory /srv/>
# Options Indexes FollowSymLinks
# AllowOverride None
# Require all granted
#</Directory>
# Configuration for Django application
WSGIScriptAlias / /var/www/my_site/site/wsgi.py
WSGIPythonPath /var/www/my_site
<Directory var/www/my_site/my_site>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives. See also the AllowOverride
# directive.
#
AccessFileName .htaccess
#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
# FQDN
ServerName example.com
My Nginx config :
server {
listen 8000;
server_name localhost;
access_log /var/log/nginx/aa8000.access.log;
error_log /var/log/nginx/aa8000.error.log;
location / {
index index.html index.htm;
}
location ^/static/ {
autoindex on;
root /var/www/my_site/allstatic/;
}
}
And to finish my Django setting for static files :
STATIC_URL = 'http://example.com:8000/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/var/www/my_site/static/',
]
STATIC_ROOT = '/var/www/my_site/allstatic'
Nothing in Nginx error log or even in access log.
Edit : Finally I only use apache and it works.
There is what I added in apache conf:
Alias /favicon.ico /var/www/my_site/allstatic/favicon/favicon.ico
Alias /media/ /var/www/my_site/media/
Alias /static/ /var/www/my_site/allstatic/
<Directory /var/www/my_site/allstatic>
Require all granted
</Directory>
<Directory /var/www/my_site/media>
Require all granted
</Directory>
I suggest you add static (and if you need it, media) sections to your apache file like the example at https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#serving-files in your apache.conf
And are you hosting more than one site? If not, don't use the sites-available; just turn off virtual hosting and don't enable anything in the sites-enabled directory.

Rewriting URLs within the webpage with a subdirectory

I've setup a reverse proxy and using mod_rewrite to rewrite the URLs. The issue I'm facing is that all the URLs in my webpage go to http://domainname.com/pagename
However, I want it to go to http://domainname.com/dir/pagename
I've tried a bunch of Rewrites but none seem to work.
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
<VirtualHost *:*>
ProxyPreserveHost On
# Servers to proxy the connection, or;
# List of application servers:
# Usage:
# ProxyPass / http://[IP Addr.]:[port]/
# ProxyPassReverse / http://[IP Addr.]:[port]/
# Example:
ProxyRequests Off
ProxyPass / http://0.0.0.0:8080/
ProxyPassReverse / http://0.0.0.0:8080/
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
<Directory /var/www/>
AllowOverride All
Order allow,deny
allow from all
</Directory>
<Directory /var/www/html>
AllowOverride All
Order allow,deny
allow from all
</Directory>
<VirtualHost *:*>
# Proxy specific settings
ProxyRequests Off
<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
</Proxy>
ServerName do.domain.com
ProxyPassReverse blog.domain.com do.domain.com/blog/
RewriteEngine On
RewriteRule ^blog.(.*)$ do.cdomain.com/blog/$1 [P]
</VirtualHost>