开发者

Deploying a python CGI app

开发者 https://www.devze.com 2022-12-12 09:42 出处:网络
I have developed a python CGI application which works just fine on my development box. My hosting provider however gives me little control of its server: I use a lot of custom stuff in my python envir

I have developed a python CGI application which works just fine on my development box. My hosting provider however gives me little control of its server: I use a lot of custom stuff in my python environment (like sqlalchemy and mako templating) and the server开发者_开发百科s python version is far too old to be used. My question is: how do I set up a isolated, complete, standalone python environment in my home directory and install my required modules to run my app? ...the easiest way ;)


how do I set up a isolated, complete, standalone python environment in my home directory

  1. mkdir /home/me/.local (if it doesn't already exist. You don't have to use .local but it is becoming the normal place to put this)
  2. mkdir /home/me/.local/src (ditto)
  3. cd /home/me/.local/src
  4. wget http://python.org/ftp/python/2.6.4/Python-2.6.4.tgz
  5. gzip -d Python-2.6.4.tgz
  6. tar xf Python-2.6.4.tar
  7. cd Python-2.6.4
  8. ./configure --prefix=/home/me/.local
  9. make
  10. make install

Hopefully you can now run Python:

  • /home/me/.local/bin/python

Install packages you need using the usual setup.py script, but with your version of Python:

  • /home/me/.local/bin/python setup.py install

Set hashbang on CGI files to use your version of Python:

  • #!/home/me/.local/bin/python

Consider migrating your application to WSGI if you can. You can of course still deploy WSGI apps through CGI using a wsgiref.handlers.CGIHandler for now, but in the future when you have a less woeful hosting environment you'll be able to deploy using a much less wasteful server interface such as mod_wsgi.


In your shoes, I'd use pyinstaller to bundle Python, my code, and all my dependencies into one installer executable, upload it, and run it. Just be sure to use the SVN trunk of pyinstaller -- the "released" version is WAY obsolete.

Be aware that with SQLAlchemy and everything else, with CGI you may find out you're really slow, since you're paying the full startup price everytime the page gets visited. But if CGI is all you can afford, I guess that's the way I would try to cope!-)


This looks like a job for virtualenv. From the site:

Also, what if you can't install packages into the global site-packages directory? For instance, on a shared host.

This looks to be right up your alley.


I am on Dreamhost's shared plan. Besides CGI, they also offer FastCGI which makes things much faster than CGI. You should check if your hosting provider offers that. Or maybe they provide Passenger for Ruby that you could piggyback your Python with.

If you compile Python yourself, keep in mind the UCS setting if you try to install precompiled packages and experience failures. See the StackOverflow article. Dreamhost's wiki has some advice on how you could build and deploy Python yourself on their servers; you might want to adapt to your needs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号