Deploying - Aimlackies/Reporter GitHub Wiki
To host the Flask app we are using Google Cloud but the same set-up instructions can be applied with minor modifications to any Virtual Private Server (VPS) and MySQL host.
VPS set-up
- Click on
create instancein Google Compute Engine - Select the type of server required for hosting. At present the Flask App only uses a single server so the size of this will set how much load the application can handle. For testing the smallest size will work fine. Make a note of the
regionandzoneas these should be the same for the MySQL server to minimise latency. - The image should be set to Ubuntu with the version set to the latest Long Term Support (LTS). Access scopes can either be
Allow default accessorAllow full access to all Cloud APIs - The firewall should allow both HTTP and HTTPs traffic if testing but HTTP traffic should be disabled at a later date when the site is made live or is used in production.
- Click
create. Once this is done make a note of the external IP address. - Start the VPS
- Create a Firewall rule to allow tcp on port 8080 (If the site should be accessable on that port)
MySQL set-up
- Click on
create instancein Google SQL - Click
MySQL - Set an instance ID and password. Write the password down
- Set the database version to 8
- Select the
regionthat matches the VPS, zone can be multiple or the same as the VPS (selecting a single zone should only be selected when testing) - Select an appropriate machine type and storage amount
- click
create instance - Inside the instance navigate to
Databasesand create a database for the Flask app. Give it an appropriate name. - Inside the instance navigate to
Usersand create a use for the Flask app to access the database. Note down the username and password - Inside the instance navage to
Connectionsand add the compute VPS toAuthorized networkswith the IP address you saved when setting up the VPS (you can find this again by navigating back to theCompute Engine). If the Flask app does not have an SSL certificateAllow only SSL connectionswill need to be disabled but this should NOT be done on production. - Start the MySQL server
Configuring the Flask app on the VPS
-
Make sure both the VPS and MySQL servers are running
-
Connect to the VPS via SSL
-
Download the latest version of Anaconda e.g.
wget https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh -
Install Anaconda:
bash Anaconda3-2021.05-Linux-x86_64.sh -
Restart the VPS and reconnect once this is complete
-
Update `~/.bashrc to load conda for non interactive users. This allows cron jobs to use it
i. Open
~/.bashrcnano ~/.bashrcii. Cut the content for Conda to the top of the file. This will be marked by comments stating when Conda in initilised. Once done the original Conda commands can be deleted. Save and exit nano.
iii. Reload .bashrc (This will also need to be re-run on every reboot of the system / reconnect via SSH)
source ~/.bashrc -
Install postfix and configure to allow local messaging. This is used for logging cron jobs. If it is not installed cron jobs will still function but logging will not.
sudo apt-get install postfix -
Set the timezone on the VSP to
Europe/Londonsudo timedatectl set-timezone Europe/London -
Clone repository to your local machine.
git clone https://git.cardiff.ac.uk/aimlackies/reporter.git -
Create a Conda environment and install Python packages. The Conda enviroemtn is set to use Python 3.8 as, at the time of creation, that was the latest version PyTorch supported. Ensure compatibility with dependencies before upgrading.
conda create -n aimlacReporter python=3.8 conda activate aimlacReporter pip install -r requirements.txt -
Set-up environmental variables for
config.py
For some aspects of the web application to function we will need to provide a variable value that should NOT be added to the repository. We will therefore set these up as environmental variables that the web application can access, but is not set as part of the application. i.nano ~/.bash_profileii. Enter the following variables
export AIMLAC_CC_MACHINE=34.72.51.59 export AIMLAC_API_KEY=AIMLACkies275001901 export FLASK_APP=reporter.py export AIMLACKIES_REPORTER_SECRET_KEY= # Enter your generated secret key (e.g. openssl rand -base64 64) export AIMLACKIES_REPORTER_SECURITY_PASSWORD_SALT= # Enter your generated password salt (e.g. secrets.SystemRandom().getrandbits(128)) export AIMLACKIES_REPORTER_DATABASE_URL=mysql+pymysql://reporter_db_user:reporter_db_password@MySQL_IP/local_reporter # reporter_db_user = database user name # reporter_db_password = database user password # MySQL_IP = MySQL server public IP address # local_reporter = local database name # generate secure random varables on your local machine. This can be done use command line utilities such as `openssl rand -base64 64` # or in python with `secrets.SystemRandom().getrandbits(128)`iii. Save the variables
source ~/.bash_profile -
Initiate, create and seed your local database with a default admin user. The commands to
migrateandupgradethe database will also be used to make updates to the database schema. The default admin user will have the email[email protected]and the passwordpassword. The password should be changed straight away.sh flask db init flask db upgrade flask seed -
Load in cron tasks from the backup file
crontab ./crontab.bak -
Start the application You done! The web application will now run. The first command will start the Flask app but will only keep it running as your terminal is running. The second command will start this in a blind process which will stay running until it is stopped.
gunicorn -b 0.0.0.0:8080 reporter:app