Apache server : Get a virtual file distribute by a script - python

My problem is the following :
I have build an apache server (with python cgi). I have client that try to get a file like http://mysever/file (which is virtual, it does not exist). Depending of the user agent of the client I would like to serve a different file like http://mysever/type1/file or http://mysever/type2/file.
I have tried with ScriptAlias file myscript.py ad I got error 403 Forbidden (my script has been chmoded +x)
I have tried with with Script or Action and I got error 400 Bad Request.
The only thing that works is with Redirect but it seems very ugly.
Can you help me ?
Thank you ! :)

Okay, I have found the solution.
In .htaccess file, in the <Directory> part, I have used :
RewriteEngine On
RewriteBase /
RewriteRule ^file$ myscript.py
Aaaaaaaand it's gone !

Related

500 Server Error With Running Flask App With CGI Script

I keep receiving a 500 Internal Server Error with my Python Flask/CGI program.
I am running it on shared hosting, so I followed this tutorial:
https://medium.com/#dorukgezici/how-to-setup-python-flask-app-on-shared-hosting-without-root-access-e40f95ccc819
This is my main python script: (~/website/mainApp.py)
from flask import Flask
app = Flask(__name__)
#app.route("/")
def home():
return "123 :)"
if __name__ == "__main__":
app.run()
This is my CGI script (~/website/main.cgi)
#!/usr/bin/python
import sys
sys.path.insert(0, "~/.local/lib/python3.7/site-packages")
from wsgiref.handlers import CGIHandler
from mainApp import app
CGIHandler().run(app)
and this is my .htaccess file (~/website/.htaccess):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /main.cgi/$1 [L]
This is basically a file tree of it:
This is the error I am getting:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
Does anyone see where an error may be?
Thanks!
Edit: It now has a weird .pyc file in it. I didn't add it though.?
The contents of your .cgi and .py files look fine.
The main problem is that the permissions on your .cgi file are not correct. Possibly also, the same goes for the website directory -- its permissions are not visible in the file view you posted.
You need execution (and read!) permission on the CGI file, and on any directories leading up to it.
In theory, the following, when run from inside the website directory, should be sufficient:
chmod a+rx . main.cgi
Notice the . to also apply the command to the current (website) directory. This adds read permission and execute permission to the owner, group and others.
If you don't want any permissions applied for the group, then this is sufficient:
chmod uo+rx . main.cgi
As for the .htaccess file, its also valid and would work -- assuming you have mod_rewrite enabled on your server. Check out this post for instructions on enabling that in case you haven't already.
I changed the shebang at the top of the main.cgi file, and it worked.
Before:
#!/usr/bin/python
After:
#!/usr/bin/python3.7

Target WSGI script not found or unable to stat for Flask Apache

I am trying to load a Flask app in a subdirectory on my domain. Below is my virtual host configuration. the Flask app is kept at /var/www/FlaskApps/Recommender/
Apache's error log keeps saying "Target WSGI script not found or unable to stat: /var/www/FlaskApps/FlaskApp.wsgi" when i try to reach the endpoint /recommender
From online other people said it might be a permission issue, but I'm not sure if it's the case per snapshot below.
Thanks for your help
You have FlaskApps.wsgi in directory, but FlaskApp.wsgi in WSGIScriptAlias directive. So they don't match.

Django and Gunicorn: 403 Forbidden

I have a django application inside /home//my_app that I am trying to deploy using gunicorn:
sudo gunicorn --workers=2 -b :8081 tutorial.wsgi:application
After deploying the application with the command above, I log into another ssh instance (on the same server) and run the following command:
wget 127.0.0.1:8081
This returns a 403 FORBIDDEN.
Things I have tried:
1. Tried to chmod 755, and even 777, in app directory (Did not work)
2. Tried to move app directory to /etc/www/myapp (Did not work)
3. Tried to run all commands using root access (Did not work)
It is worth noting that I am not that familiar with linux and that this error is literally driving me crazy.
SOLVED IT:
after downloading cURL, in order to see the http header, it turned out that the service worked, but returned a 403 because a missing token authorization. Oops.
Please make sure you have coded views.py and urls.py to server GET requeat at /.

What determines the applicative log file location in Apache/Django?

(Cross posted to Server Fault some time ago)
I have a django app running on Apache/ubuntu, and I have evidently misconfigured it.
When I start apache, I'm getting this error in the apache log:
...
IOError: [Errno 2] No such file or directory: '/home/osqa/sites/log/django.osqa.log'
Now, my site is supposed to be running in 'home/osqa/sites/foobar/'. Why is django/apache looking for a log file in a folder above that folder? Where is this configured? How to resolve/analyze?
The following lines in your httpd.conf file is what is causing your problem:
CustomLog ${APACHE_LOG_DIR}/beta-meta-d3c.access.log common
ErrorLog ${APACHE_LOG_DIR}/beta-meta-d3c.error.log
This logging is set up for apache as a whole, not just your application which is set up in a subdirectory within apache.
${APACHE_LOG_DIR} should evaluate to /home/osqa/sites/log/. Fully expanded with your log names it will be:
/home/osqa/sites/log/beta-meta-d3c.access.log
Which is exactly what it is telling you. Either create that directory and make it writeable, or change your httpd conf to append the name of your application to the logging path directive. I'd probably not change the path though, as other applications might want to log also, but not to your directory.
My hunch is that it's something in your apache2 config. Go to sites-available/your-site.com.conf, and look in its file. Perhaps you'll find that it is logging things?

Mod_python produces no output

Just installed and configured mod_python 3.2.8 on a CentOS 5 (Apache 2.2.3) server with Python 2.4.3. It is loaded fine by Apache.
I activated the mpinfo test page and it works. So I wrote a simple "Hello World" with the following code:
from mod_python import apache
def handler(req):
req.content_type = 'text/plain'
req.write("Hello World!")
req.flush()
return apache.OK
It outputs a blank page, with no text and no source. If I consciously create a syntax error I get the error output on the URL, for example (when I put a space before "def"):
Mod_python error: "PythonHandler mod_python.cgihandler"
Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 299, in HandlerDispatch
result = object(req)
File "/usr/lib/python2.4/site-packages/mod_python/cgihandler.py", line 96, in handler
imp.load_module(module_name, fd, path, desc)
File "/var/www/vhosts/localhost/httpdocs/mptest.py", line 3
def handler(req):
^
SyntaxError: invalid syntax
I have spent about five hours browsing different tutorials, faqs and trouble shooting guides but can't find a description of this exakt issue.
What do you think could be the issue/cause?
EDIT: Here is the Apache configuration for the site...
<Directory />
Options FollowSymLinks
AllowOverride None
AddHandler mod_python .py
PythonHandler mptest
PythonDebug On
</Directory>
EDIT 2: Ah, another thing I forgot to mention is that I intend to use mod_python to write Apache extensions. The application itself is written in PHP but I need to make some security tweeks on the server :)
Don't use mod_python.
A common mistake is to take mod_python as "mod_php, but for python" and that is not true. mod_python is more suited to writing apache extensions, not web applications.
The standartized protocol to use between python web applications and web servers (not only apache) is WSGI. Using it ensures that you can publish your application to any wsgi-compliant webserver (almost all modern web servers are wsgi-compliant)
On apache, use mod_wsgi instead.
Your example rewritten using the wsgi standard and mod_wsgi on apache:
mywebapp.py:
def application(environ, start_response):
start_response('200 OK', [('content-type', 'text/plain')])
return ['Hello World']
Apache configuration:
WSGIScriptAlias /myapp /usr/local/www/wsgi-scripts/mywebapp.py
<Directory /usr/local/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>
Now just go to http://localhost/myapp and the script will run. Additionally, any access under this root (i.e. http://localhost/myapp/stuff/here) will be handled by this script.
It's a good idea to choose a web framework. CherryPy. Pylons. Django. They make things even easier.
A good website to look at is wsgi.org
Your original problem is that mod_python.cgihandler is being called to handle the request. This means your Python script file is being interpreted as a CGI script. Thus, no wonder it doesn't return anything.
You likely have conflicting definition in your Apache configuration which is enabling the mod_python.cgihandler.
I make a complete new answer for clarity...
I decided to install mod_wsgi instead. So I've set it up and when I go to my testfile I just see the page source. I haven't been spending any time on finding the issue yet, so I'll get back to you when I either solve the problem or decide that I need more help :)
Thank you :)

Categories