Running web.py on Dreamhost - python

I am trying to set up web.py on Dreamhost using this guide:
Web.py - Dreamhost Wiki
My problem is that many pages on this topic refers to fcgi.py and links to a svn repository that no longer exists. So I got flup instead, and I can run
import web
from flup.server import fcgi
without errors in Python, but not import fcgi
The flup.server version of fcgi can't stand alone, so I can't simply copy the one file somewhere into the PYTHONPATH.
When I try open index.py 500 Internal server error. I tried to check out the logs, but they are note very useful - all I get there is "GET / HTTP/1.1" 200.
I have run out of ideas how to debug this thing.

Related

Server Error 500 When Using gcloud to deploy my Python Web App

After following freecampcode's web application tutorial, creating my own Google Cloud SDK project and deploying my sample social network website using Python and Flask (which I found mostly thought this tutorial), I am getting this same error: Internal/Unknown Server Error 500, which looks like:
Error: Server Error
The server encountered an error and could not complete your request.
Please try again in 30 seconds.
Checking the logs, I get the errors:
-2020-07-06 01:51:29 default[20200705t214944] "GET / HTTP/1.1" 500
-2020-07-06 01:51:32 default[20200705t214944] "GET /favicon.ico HTTP/1.1" 500
Any help would be greatly appreciated. I've been stuck on this issue for a while. If you need additional information on the project, please let me know. This is my first gcloud project.
EDIT: I checked the error logs and saw the error:
ImportError: No module named main
at LoadObject (/base/alloc/tmpfs/dynamic_runtimes/python27g/174489198b39d9fb/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py:85)
at _LoadHandler (/base/alloc/tmpfs/dynamic_runtimes/python27g/174489198b39d9fb/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py:311)
at Handle (/base/alloc/tmpfs/dynamic_runtimes/python27g/174489198b39d9fb/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py:240)
So I added the file main.py with the contents:
from app.wsgi import main as app
entrypoint: gunicorn -b :$PORT app.wsgi:main
and I still get the same internal error.
What I noticed is that tutorial 1 (this one) contain how-to for python2, while the 2nd tutorial (this one) is how-to build with python3.
What more python2 not supported any more, so there is no point to learn how to use it. Here it is the reference.
I suppose you have to change environment in app.yaml file. Here is reference to python3 environment. I would start with that.
What I recommend you as well is to go to Google Cloud Platform and open App Engine (direct link). On the right you have a possibility to go with tutorial called "App Engine Quickstart" or "Hello World". You can choose Python and go trough whole process of creating HelloWorld app, which by the way is Flask as well. I would try it even before the once you are trying.
I hope it will help!
one way is to Try with DEBUG mode off
still if it not works then try this below way with this you will get exact details why you got this error
Try this it will return exact error
#app.errorhandler(500)
def internal_server_error(e):
return jsonify(error=str(e)), 500

Django 2.0.7 with fastcgi gives 404 in browser but works on command line

I'm trying to set up a small django project on a bluehost shared server and am having trouble on what I think should be the last step - getting it running with fastcgi.
I installed python 3.7.0 and django 2.0.7 using miniconda and was able to create a project/app but i can't get it to display in a browser (FWIW, I have successfully done this with another bluehost site on a similar plan, though that site is under the "shared plus" plan while this one is just the basic, but I don't know if that's the issue)
In my ~/public_html/myproject/.htaccess file I have:
AddHandler fastcgi-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /home/username/public_html/myproject/myproject.fcgi/$1 [QSA,L]
And in myproject.fcgi I have:
#!/home/username/miniconda3/bin/python
# -*- coding: utf-8 -*-
import sys, os
project_name = "myproject"
sys.stdout=open('/home/username/public_html/myproject/test.out','w')
print(project_name)
# Add a custom Python path.
sys.path.insert(0, "/home/username/miniconda3/bin/python")
sys.path.insert(0, "/home/username/public_html/myproject")
sys.path.insert(0, os.getcwd() + "/" + project_name)
os.environ['DJANGO_SETTINGS_MODULE'] = project_name + '.settings'
from django_fastcgi.servers.fastcgi import runfastcgi
from django.core.servers.basehttp import get_internal_wsgi_application
wsgi_application = get_internal_wsgi_application()
runfastcgi(wsgi_application, method="prefork", daemonize="false", minspare=1, maxspare=1, maxchildren=1)
This configuration worked for me on the other site, but here I am only able to run ./myproject.fcgi successfully on the command line, but I get a 404 in the browser.
I'm unable to access my server logs so I added a line to the fcgi script to direct stdout to a file and find that the file is not produced, so I'm not sure if something's wrong with accessing my python install when I try to load the page in the browser, or what (I'm pretty new to this!)
I found practically my exact question here, unfortunately without an answer:
Django with FastCGI gives 404 in browser but works on command line
Thanks!!
Update: I ended up determining that the issue I was facing was due to the limitations of my shared hosting environment. For starters, installation and troubleshooting was difficult without root access or access to apache error logs, but I was able to get an installation working, and made additional progress when I realized that my bluehost plan required cgi scripts to run from the cgi-bin dir (rather than adding handlers in .htaccess as most tutorials suggested).
However, the final hurdle I couldn't overcome seemed to be a limit on the number of spawned threads - when I tried to load the site (django test page) in the browser, it would just hang and I would get an error in my shell session: -jailshell: fork: Resource temporarily unavailable, as described here. … it's possible that someone with more experience could have figured it out but I finally decided it would be easier to switch. It took me longer to give up than it should have, since I have successfully done the exact same thing on another bluehost shared server account, but I finally learned that bluehost has updated their plans and the other one only worked since it was on an older version of the plan.
I switched to dreamhost (with their shared server plan) and was able to get everything running easily!

How do I use Apache to just basically fundamentally run a python script?

There's multiple ways, and not clear tutorials or anything online on how to run a fundamentally basic python script with apache.
mod_python? cgi? wsgi?
Does something need to be installed with python?
It's really confusing. I've been working on it for hours.
Flow
Client accesses a web page, say http://example.com/mangojuice (issue 1). Your server (apache) should know mangojuice referes to /etc/www/superhightechjuice/mango.py. Now this python file should be executed (issue 2). Apache should know .py is supposed to be executed and not served plain. But Python does not know when to wakeup and run. So another entity is needed (issue 3).
Solutions to the three issues (interfaces) are handled by mod_wsgi:
WSGI is inherited from the CGI concept (which was used for executing perl scripts and php scripts) to extend to Python scripts (with additional features). mod_wsgi is how Graham Dumpleton implemented it in Apache and is now a standard.
Issue 1 - Conf file which does the mapping - /etc/apache2/conf-available/wsgi.conf
Issue 2 - Install mod_wsgi - during installation, it updates apache configuration files to inform that .py should be executed; along with a host of other things
Issue 3 - Function - application
Code
def application(environ, start_response):
status = '200 OK'
message = b"Hello World" #Prefix b is useful to convert string to bytestring, which is needed by HTTP
response_header = [('Content-type', 'text/html')]
start_response(status, response_header)
return [message]
Reason
When you setup mod_wsgi, it solves issue 1 by having a mapping file wsgi.conf. It solves issue 2 by updating .conf file of apache and also other OS based settings. For Issue 3, it tells apache, I am a middleware whenever I see reference to 'application', I will inform you and you both can talk (Apache and Python). This is how mod_wsgi helps.
Steps to setup mod_wsgi
Install mod_wsgi for Apache2
sudo apt install libapache2-mod-wsgi
Restart Apache to refresh all updates
apachectl restart
Create the mapping file
vi /etc/apache2/conf-available/wsgi.conf
Update the mapping file with url and local path
WSGIScriptAlias /mangojuice /etc/www/superhightechjuice/mango.py
Create the actual code at /etc/www/superhightechjuice/mango.py
Code sample same as mentioned above. start_response tells mod_wsgi that requests are incoming. mod_wsgi talks with Apache and lets your code through.
Reference:
Official mod_wsgi documentation

Download and setup a Angular/Django site from server to localhost?

Hi I am new to Django and have been googling for a whole day without any success.
Basically there is a live/working website built with AngularJS, Django and PostgreSQL (Ubuntu 14.04) and I am trying to download all the files and clone the site into my localhost (Ubuntu 14.04).
After I downloaded the folder and finished install the required packages on my localhost, I run:
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
The server runs without reporting any errors.
However, some of the links are directed to a 404 page, such as this:
http://127.0.0.1:8000/city/chicago
Whereas in the live site, it would direct to the correct working page.
Can someone tell me what may have gone wrong in the process?
Thanks.
Try editing the settings.py file set the DEBUG value to True. Instead of a 404 you should get a full debug of the actual request and see what exactly is missing (I suspect missing data as #NightShadeQueen pointed).
Also you might have a look at django debug toolbar which prints the executed sql queries as well and run them against your local database to see what exactly they return.

Flask - WSGI process keeps crashing

I get strange errors when trying to deploy a Flask app (v0.8). I'm using apache's mod_wsgi, as suggested in the docs (almost to the letter). What's strange is that on some requests (simply accessing / via browser), the application loads fine, but very often I get a resource load errors (can't fetch some css or image), and the error log shows something like Premature end of script headers: myapp.wsgi. Sometimes apache crashes with Internal Server Error. I'm trying to chase the problem but could not figure out what's the cause. My wsgi file looks simple:
from myapp import app as application
Any clues where to start looking?
If you are getting Premature end of script headers, you are using daemon mode and the daemon process is crashing with a seg fault or similar.
Make sure you aren't still loading mod_python.
Also try setting:
WSGIApplicationGroup %{GLOBAL}
to work around issues with Python C extension modules which aren't safe for sub interpreters.
See:
http://code.google.com/p/modwsgi/wiki/ApplicationIssues
and look for various reasons for crashes listed.

Categories

Resources