Keith Irwin

Using web.py on Uberspace

Oct 9, 2015

Web Development


I love my host, Uberspace. They have good instructions (in German) on how to use django on their servers with Gunicorn. I decided to try hosting a web.py project there. The instructions are mostly the same, but it took a little trial and error to figure out what's different.

OK, first, use SFTP to move your site to the server. For this example, we'll be storing it at /home/username/mysite/.

As in the django tutorial, we'll need find a port. Keep repeating this command until "versuch's nochmal" does not appear. It usually doesn't take more than one try.

PORT=$(( $RANDOM % 4535 + 61000)); netstat -tulpen | grep $PORT && echo "versuch's nochmal"

Make sure that worked:

echo $PORT

Now choose a subdomain and create a variable for it. Be sure to replace <SERVERNAME> with the name of your Uberspace server.

SUBDOMAIN=mysite.$USER.<SERVERNAME>.uberspace.de

Create a folder for the subdomain in your /var/www/virtual/$USER folder.

mkdir /var/www/virtual/$USER/$SUBDOMAIN

Copy your static files there. You will need to do this anytime you change a static file.

cp ~/mysite/static -r /var/www/virtual/$USER/$SUBDOMAIN

Set up your .htaccess file.

cat <<__EOF__> /var/www/virtual/$USER/$SUBDOMAIN/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /
RewriteRule ^(.*)$ http://0.0.0.0:$PORT/$1 [P]
RequestHeader set X-Forwarded-Proto https env=HTTPS
__EOF__

Next, you need to set up the correct WSGI settings in your application. Add the following lines to your code.py file.

app = web.application(urls, globals())
wsgi_app = app.wsgifunc()

Great. Now all we have to do is set up the daemon. If you don't have Gunicorn installed, install it.

easy_install-2.7 gunicorn

Now we're ready. Start the service. We'll be calling the service mysite.

test -d ~/service || uberspace-setup-svscan

uberspace-setup-service mysite gunicorn --error-logfile - --reload --chdir /home/$USER/mysite --bind 0.0.0.0:$PORT code:wsgi_app

And look at the logs to make sure it worked.

tail ~/service/mysite/log/main/current | tai64nlocal 

If everything went well, you should be able to see your web.py app running at the subdomain you chose earlier! If you want to set it up at a domain name, see Uberspace's instructions. If you had any problems, I might have missed a step. Let me know by commenting!


How to Replace a Saab Clutch Cable AOL CD Collection