Serving Django with apache2 fails with ModuleNotFoundError: No module named 'djangoProject' - ubuntu-20.04

I am trying to setup a Django server running on Ubuntu 20.04 using Python 3.10.6 and Django 4.1.1
When Django tries to run get_wsgi_application() it throws the error: ModuleNotFoundError: No module named 'djangoProject'
I'm NOT running in a virtual environment and I'm using the following in /etc/apache2/conf-available/mod-wsgi.conf
WSGIScriptAlias /pages /var/www/django_root/djangoProject/djangoProject/wsgi.py
WSGIPythonPath /var/www/django_root/DjangoProject
<Directory /var/www/django_root/DjangoProject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias /test_wsgi /var/www/django_root/test_wsgi_script.py
my original file only had lines 1 and 8, same results.
I am using the default wsgi file:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangoProject.settings')
application = get_wsgi_application()
I know it's something quite simple, but I have exhausted all of the things I can think of, and I'm not quite sure where get_wsgi_application() is getting it's path info from or exactly what it's looking for a path to...

Related

justpy app don't work on AWS Lightsail; no error but also no page visible

A python file with this code:
import justpy as jp
def hello_world():
wp = jp.WebPage()
jp.Hello(a=wp)
return wp
jp.justpy(hello_world)
A justpy.env file with this parameters:
HOST = '0.0.0.0'
PORT = 8000
A Lightsail Ubuntu VM with static URL runs the file with output on console:
ubuntu#ip-xy:~$ python3 run.py
/home/ubuntu/.local/lib/python3.8/site-packages/justpy
Module directory: /home/ubuntu/.local/lib/python3.8/site-packages/justpy, Application dire
ctory: /home/ubuntu
JustPy ready to go on http://0.0.0.0:8000
Page not shown. What is missing?
What is missing? I suffer with this.

How do you use an HTTP/HTTPS proxy with boto3?

On the old boto library is was simple enough to use the proxy, proxy_port, proxy_user and proxy_pass parameters when you open a connection. However, I could not find any equivalent way of programmatically define the proxy parameters on boto3. :(
As of at least version 1.5.79, botocore accepts a proxies argument in the botocore config.
e.g.
import boto3
from botocore.config import Config
boto3.resource('s3', config=Config(proxies={'https': 'foo.bar:3128'}))
boto3 resource
https://boto3.readthedocs.io/en/latest/reference/core/session.html#boto3.session.Session.resource
botocore config
https://botocore.readthedocs.io/en/stable/reference/config.html#botocore.config.Config
If you user proxy server does not have a password
try the following:
import os
os.environ["HTTP_PROXY"] = "http://proxy.com:port"
os.environ["HTTPS_PROXY"] = "https://proxy.com:port"
if you user proxy server has a password
try the following:
import os
os.environ["HTTP_PROXY"] = "http://user:password#proxy.com:port"
os.environ["HTTPS_PROXY"] = "https://user:password#proxy.com:port"
Apart from altering the environment variable, I'll present what I found in the code.
Since boto3 uses botocore, I had a look through the source code:
https://github.com/boto/botocore/blob/66008c874ebfa9ee7530d944d274480347ac3432/botocore/endpoint.py#L265
From this link, we end up at:
def _get_proxies(self, url):
# We could also support getting proxies from a config file,
# but for now proxy support is taken from the environment.
return get_environ_proxies(url)
...which is called by proxies = self._get_proxies(final_endpoint_url) in the EndpointCreator class.
Long story short, if you're using python2 it will use the getproxies method from urllib2 and if you're using python3, it will use urllib3.
get_environ_proxies is expecting a dict containing {'http:' 'url'} (and I'm guessing https too).
You could always patch the code, but that is poor practice.
This is one of the rare occasions when I would recommend monkey-patching, at least until the Boto developers allow connection-specific proxy settings:
import botocore.endpoint
def _get_proxies(self, url):
return {'http': 'http://someproxy:1234/', 'https': 'https://someproxy:1234/'}
botocore.endpoint.EndpointCreator._get_proxies = _get_proxies
import boto3

Why is Django missing the custom context processor?

My django 1.6 project is structured:
cg1
cg1
settings.py
cont_proc.py
inti, etc.
app
app
manage.py
templates
cont_proc.py reads:
from django.conf import settings
def misc(request):
return {'SITE_URL': settings.SITE_URL,'BALANCED_API_KEY':settings.BALANCED_API_KEY}`
in settings.py I have:
import django.conf.global_settings as DEFAULT_SETTINGS
TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + ( os.path.join(BASE_DIR, 'cg1.cont_proc.misc'),)
BALANCED_API_KEY = os.environ.get('BALANCED_API_KEY')
SITE_URL = 'www.mysite.com' #but set up
python manage.py shell:
>>> from django.conf import settings
>>> settings.TEMPLATE_CONTEXT_PROCESSORS
['django_balanced.context_processors.balanced_library','django_balanced.context_processors.balanced_settings', 'django.contrib.auth.context_processors.auth']
>>>>import os
>>>>os.environ.get('BALANCED_API_KEY')
'correct key from a local .env file'
I've tried quite a few so question, especially: Where is template context processor in Django 1.5?
also: Python/Django is importing the wrong module (relative when it should be absolute)
but django doesn't seem to see my custom context processor, cont_proc, in the shell. And when I use render in views my templates do not receive the variables.
I had installed django-balanced. Apparently this was a mistake. I removed from installed apps and all was good.

How to setup mod_lua in Apache to access third party Lua modules?

I'm attempting to set up mod_lua module for Apache, but have encountered difficulty regarding accessing third party Lua modules. Say I have a hello_world.lua in Apache's htdocs folder that has something like this:
require "apache2"
function handle(r)
r.content_type = "text/html"
r:write "Hello World from <strong>mod_lua</strong>."
return apache2.OK
end
And I go to "http://localhost/hello_world.lua", that will function as expected. But if I try to add a line such as:
require "socket"
Or
require "cgilua"
I get the following output:
Error!
attempt to call a nil value
However, some modules do work, such as:
require "base"
That functions as expected.
If I navigate to base.lua in the filesystem (c:\program files\lua\5.1\lua\base.lua) and remove this file, then attempt to run my script I get the same error as stated above. So this must be the directory that mod_lua is checking for modules. Modules dlls are not in this folder, instead they are in c:\program files\lua\5.1\clibs\, which I set up the environment variable LUA_CPATH to point to.
Luasocket and cgilua are both present in this folder, yet they cause an error when I try to require them in my script.
From what I can gather, it works fine with any pure lua modules, but anything that has cmodules as well (socket, etc) causes problems.
Additional info:
OS: Windows 7 Home Premium
LUA_PATH = c:\program files\lua\5.1\lua\
LUA_CPATH = c:\program files\lua\5.1\clibs\
Apache version: 2.2.22
mod_lua version: http://www.corsix.org/content/mod-lua-win32#comment-3214
What needs to be done to be able to require modules in scripts run by mod_lua?
It looks like you need to add LuaPackageCPath and/or LuaPackagePath directives to your site configuration (in the global configuration file, or .htaccess, ...).
In your case, I'd assume that
LuaPackagePath c:\program files\lua\5.1\lua\
LuaPackageCPath c:\program files\lua\5.1\clibs\
should do the trick.

Apache2 doesn't execute .rb files (mod_ruby)

I want to make apache2 run ruby scripts. I managed to do this by using CGI, but now I want to go one step further and do it with mod_ruby. I installed mod_ruby through apt-get so it's supposed to be enabled, but when I visit http://localhost/test.rb my browser downloads the file instead of displaying it's output.
If I open /etc/apache2/mods-enabled/ruby.load there is a line containing the path do this mod.
I also added the following lined to my apache2.conf:
<IfModule mod_ruby.c>
<Directory /var/www/>
Options +ExecCGI
</Directory>
RubyRequire apache/ruby-run
#RubySafeLevel 0
<Files *.rb>
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>
<Files *.rbx>
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>
</IfModule>
Can you please help me?
PS. Please don't suggest using ROR. I'm familiar with that. Now I just want to use ruby for educational reasons. Also please keep things simple I'm a total newbie to apache.
See this article - it has some instructions. Basically you need to tell Apache to pass your Ruby files through the handler.
Also, consider trying Sinatra - it's a micro web framework that's as simple as plain ruby files. And it runs with Passenger which is the well-documented Ruby module for Apache.
The article on HowtoForge covers almost the whole story, but I needed to add one line inside the <Directory /var/www> directive to prevent browsers from downloading the file:
AddType text/html .rb .rbx
Here are a couple more sites that I found helpful:
https://defuse.ca/blog/2012/07/how-to-install-mod_ruby-on-debian-squeeze/
http://modruby.net/en/doc/
Debian Stretch 9.
I tried installing mod_ruby with apt-get install libapache2-mod-ruby but there is no package by that name. In the end, all I needed to do was add this line to the apache-config to get the server to run my scripts.
AddHandler cgi-script .rb

Resources