Activity #21: Python Anywhere Deployment

Step 1: Fork the Repository

Visit the repository URL: https://github.com/thirdygayares/keriderya-api.

In the top-right corner, click the Fork button to create a copy of the repository under your GitHub account.

Step 2: Set up a PythonAnywhere Account

  1. Go to pythonanywhere.

  2. Sign up for a new account if you don’t already have one.

  3. Once logged in, go to your account settings and note down your PythonAnywhere username and password (make sure you store these in Excel for future reference).

Step 3. List Your Credentials on Excel

Create an Excel sheet with your credentials for the MySQL database and other necessary configurations like environment variables.

Step 4. Add New Bash Consoles

  1. Go to the Consoles tab in PythonAnywhere.

  2. Click on the Bash option to open a new Bash console.

Step 5. Clone the Repository

In the Bash console, clone your forked repository using Git. You will need to replace the URL with the one for your fork.

git clone https://github.com/RomelUsigan/keriderya-api.git
cd keriderya-api

Step 6. Set Up the Database in MySQL Console

  1. Go to the Databases tab.

  2. Create a new MySQL database. For example, name it keriderya_db.

  3. Once the database is created, note the database name, username, and password for later use.

  4. In the Consoles tab, open a new MySQL console to manage the database:

mysql -u your_mysql_user -p

Then, enter your password and create tables if needed by running any SQL migrations required by the Flask app.

Step 7. Create a Virtual Environment

In the Bash console, create a virtual environment for the project.

python3.10 -m venv venv

Step 8. Activate Virtual Environment

Activate the virtual environment.

source venv/bin/activate

Step 9. Edit config.py and Remove the Certificate

Open config.py using nano editor:

nano config.py

Look for any SSL certificate or configuration references and remove them, as they may not be needed on PythonAnywhere’s default configuration.

10. Configure the Environment Variables

Create a .env file in the root of your project and add the necessary environment variables such as database credentials and other configuration details.

nano .env

11. Install requirements

pip install -r requirements.txt

Step 12. Run Flask DB Upgrade

python -m flask db upgrade

Run the following Flask command to apply database migrations:

Step 13. Check MySQL Tables

Check that the database tables were created successfully by entering the MySQL console:

Step 14. Go to the Web Tab

In your PythonAnywhere dashboard, go to the Web tab.

Step 15. Create a New Web App

Click Add a new web app.

Choose Flask for the web framework.

Select Python 3.10 for the Python version.

Step 16. Path Virtual Environment

Under the Virtualenv section, enter the path to your virtual environment (for example, /home/your-username/keriderya-api/venv).

This will ensure that PythonAnywhere uses your virtual environment for the web app.

Step 17. Edit WSGI File

In the Web tab, scroll down to the WSGI configuration file section and click the link to open the WSGI configuration file.

Edit the file to include the correct paths to your Flask app and virtual environment. Example WSGI setup:

Step 18. Reload the Web App

Once you've made the changes, go back to the Web tab and click Reload to apply the changes.

Step 19. Test the Web App Using Postman

Step 20. Add dummy data

POST/users

GET/users

GET/user<UUID>

Check Database

UPDATE/users

Check Database

Delete/users

Check Database

romel25.pythonanywhere.com

RomelUsigan/keriderya-api