Welcome to part 5 of my getting started guide. If you missed part 4 you can find it here.
I will assume you have a Ubuntu server somehwere and that you have access and permissions to set up the needed packages for this article. If you do not there are many helpful places online to figure that out and help you get started. You will also need to have a domain and DNS set up if you intend to use this as a server on the internet.
First we will get our dependancies installed then set up the virtual environment that will allow us to run our Python Flask project from an Apache worker thread.
Ubuntu
michael@ubuntu:~$ sudo apt install apache2 mysql-server phpmyadmin libapache2-mod-wsgi python3-dev libmysqlclient-dev python3-venv
Don't forget to set a MySQL password you can remember!
Now lets create and set up our virtual environment. Navigate to your project's directory and follow the commands.
Ubuntu (Python 3.6)
michael@ubuntu:~$ cd /var/www/blog michael@ubuntu:/var/www/blog$ pip install venv michael@ubuntu:/var/www/blog$ python -m venv my-project-env michael@ubuntu:/var/www/blog$ source my-project-env/bin/activate (my-project-env) michael@ubuntu:/var/www/blog$ pip install flask flask_sqlalchemy flask_migrate flask_login mysqlclient
We can check the database is ready for us. Lets first navigate to our server's phpmyadmin and add the user and database as you did in Part 2. Make sure you update the `app.config['SQLALCHEMY_DATABASE_URI']` value in `app.py`. If you coppied the migrations folder delete it as that will prevent the database from being created.
You can start from a fresh database here or import your database from your local mysql database. If you want to import follow guides on the internet then skip to Apache and WSGI.
Ubuntu
(my-project-env) michael@ubuntu:/var/www/blog$ flask db init (my-project-env) michael@ubuntu:/var/www/blog$ flask db migrate -m "Initial setup of the database" (my-project-env) michael@ubuntu:/var/www/blog$ flask db upgrade
We don't want to be left without a user to login with so lets do that now if everything went ok.
Ubuntu
(my-project-env) michael@ubuntu:/var/www/blog$ python # >>> from app import db, User # >>> from werkzeug.security import generate_password_hash # >>> user = User(name='Michael', email='email@domain.com', password=generate_password_hash('password')) # >>> db.session.add(user) # >>> db.session.commit() # >>> exit() (my-project-env) michael@ubuntu:/var/www/blog$ deactivate michael@ubuntu:/var/www/blog$
Create a file called `app.wsgi` inside your blog directory and lets set up.
import logging import sys logging.basicConfig(stream=sys.stderr) sys.path.insert(0,'/var/www/blog') from app import app as application
Now lets create the config we will use for Apache to run our WSGI Flask app. Create a confi for the site, mine is named `/etc/apache2/sites-available/blog.conf` and open it up.
Ubuntu
michael@ubuntu:/var/www/blog$ sudo nano /etc/apache2/sites-available/blog.conf
The contents
Ubuntu
<VirtualHost *:80> ServerName blog.com ServerAlias blog.com www.blog.com ServerAdmin michael@blog.com DocumentRoot /www/var/blog WSGIDaemonProcess blog python-home=/var/www/blog/my-project-env WSGIProcessGroup blog WSGIScriptAlias / /var/www/blog/app.wsgi <Directory /home/blog> Options FollowSymLinks AllowOverride None Require all granted </Directory> ErrorLog /var/www/blog/error.txt CustomLog /var/www/blog/access.txt combined </VirtualHost>
We're almost there. If you do not have any errors in typing you can run the next command to enable and reload Apache to see your new blog!
Ubuntu
michael@ubuntu:/var/www/blog$ sudo a2ensite blog.conf michael@ubuntu:/var/www/blog$ sudo systemctl reload apache2
That is it for now. I'll revisit this if I see any improvements.
In our final installment we will set up Apache and WSGI to make our app run in a production environment.