Hello and welcome to a tutorial for setting up Flask with Python 3 on a VPS. For the purposes of this tutorial, I will be using Digital Ocean. For $10 in credit to start with them, you can use my referral code, but you can use any VPS provider that you'd like. The point of being able to setup Flask yourself is so that you can run it from any full-service hosting provider that you want to. A special thanks to Daniel Kukiela for helping with this writeup.
To begin, you need your VPS and your sudoer
account. If you are using Digital Ocean, create a server (big green "create") button in the top right, choosing "droplet." Since 18.04 isn't quite yet out, I am going with Ubuntu 16.04. I will go with the smallest server available, since this is merely a simple setup demonstration. You can resize later quickly and easily, so just go with whatever you think you need right now. Next, just pick a region, change host name to something more appropriate for your project, and that's all you need. You can setup SSH keys, backups, and other things if you like, but this isn't necessary. Click create when you're ready. Within minutes, you should be emailed the IP address and root user password to your server.
If you are on Mac or Linux, you can interface with your server by opening a terminal and doing something like ssh root@192.168.0.1
, where the 192.168.0.1 is your server's ip address. From here, you will be asked for a password, which is included in the email you were sent. Once you log in, you will be asked to change your password. Do that. If you are on Windows, you will need an SSH client. I use PuTTy. With PuTTy, you just fill in the Host Name with the IP address, make sure the port is 22, and hit enter to connect.
Once you are in the server, let's start with an update and upgrade:
sudo apt-get update && sudo apt-get upgrade
Since we'll be using mysql in this series:
sudo apt-get install apache2 mysql-client mysql-server
Once you do that, you'll get the start up page for MySQL, where you will need to set your root user for MySQL. This is the specific MySQL root user, not your server root user.
sudo add-apt-repository ppa:deadsnakes/ppa
Hit enter to add this repository
sudo apt-get update
Get python3.6:
sudo apt-get install python3.6 python3.6-dev
Get pip3.6:
curl https://bootstrap.pypa.io/get-pip.py | sudo python3.6
Now that we have Python 3.6, let's get our webserver. For this tutorial, we'll be using Apache:
sudo apt-get install apache2 apache2-dev
In order for our apps to talk with Apache, we need an intermediary, a gateway inferace
...enter: WSGI (Web Server Gateway Inferface). Let's install WSGI for Python 3.6:
pip3.6 install mod_wsgi
Let's get the shared object file and the home dir for python's wsgi:
mod_wsgi-express module-config
You should see something like:
LoadModule wsgi_module "/usr/local/lib/python3.6/dist-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so" WSGIPythonHome "/usr"
...but your's might be different. Copy these lines into a notepad or something. Now:
nano /etc/apache2/mods-available/wsgi.load
Paste those two lines in here. Save and exit (ctrl+x
, y
, enter
). Now let's enable wsgi:
a2enmod wsgi
Restart Apache with:
service apache2 restart
Great, now that we have our web server and the interface, we just need our web app. Since this is for Flask, let's get that!
pip3.6 install Flask
We need to have an Apache configuration file for this application. To do this:
nano /etc/apache2/sites-available/FlaskApp.conf
Inputting:
<VirtualHost *:80> ServerName 192.168.0.1 ServerAdmin youremail@email.com WSGIScriptAlias / /var/www/FlaskApp/FlaskApp.wsgi <Directory /var/www/FlaskApp/FlaskApp/> Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/FlaskApp-error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/FlaskApp-access.log combined </VirtualHost>
Replace the 192.168.0.1
with your server's IP address or your domain name (only if you've set this up to work), save and exit (ctrl+x
, y
, enter
).
Now let's enable the site:
sudo a2ensite FlaskApp
Reload Apache:
service apache2 reload
Now we need to start preparing our Flask application. Let's set up some directories:
mkdir /var/www/FlaskApp
cd /var/www/FlaskApp
nano FlaskApp.wsgi
Now let's setup WSGI to interface with our application:
In here, put:
#!/usr/bin/python3.6 import sys import logging logging.basicConfig(stream=sys.stderr) sys.path.insert(0,"/var/www/FlaskApp/") from FlaskApp import app as application
Save and exit. This WSGI script is in python. Our FlaskApp.conf
file points to this WSGI file. As you can see, this WSGI script imports app
from FlaskApp
. Since that 2nd FlaskApp
doesn't yet exist, let's begin our actual FlaskApp package.
mkdir FlaskApp
cd FlaskApp
Now let's build our simple Flask Application:
nano __init__.py
from flask import Flask import sys app = Flask(__name__) @app.route('/') def homepage(): return "Hi there, how ya doin?" if __name__ == "__main__": app.run()
Save and exit.
Now run:
service apache2 reload
Now you should be able to visit your IP address to see your "Hi there, how ya doin?" message displayed proudly.
There you have it, Flask on Python 3.6, hosted on your VPS wherever. Hope that's helped!