Difference between revisions of "PYTHONAPP"

From rbachwiki
Jump to navigation Jump to search
Line 79: Line 79:


'''Set permissions:'''
'''Set permissions:'''
sudo chown -R www-data:www-data /var/www/html/myapp
'''Enable and start '''
sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp
sudo systemctl status myapp
'''You should see active (running). If not:'''
sudo journalctl -u rb222 -n 50

Revision as of 17:22, 21 April 2026

Karaoke app Deployment

Prerequisite Check

  1. ssh into the server
  2. verify what is installed
apache2 -v
python3 --version
mysql --version
which certbot
  1. if certbot isn't installed
sudo apt update
sudo apt install certbot python3-certbot-apache -y
  1. Install Python venv if needed:
sudo apt install python3-venv python3-pip -y

Create the App Directory

sudo mkdir -p /var/www/html/myapp
cd /var/www/html/myapp

Upload App Files

  1. Upload the .env file (hidden, so it won't be picked up by scp -r)

If you don't see .env in Finder, press Cmd+Shift+. to toggle hidden files.

Set up the Python Environment

cd /var/www/html/myapp
python3 -m venv venv
source venv/bin/activate
pip install flask pymysql cryptography gunicorn DBUtils
deactivate

The cryptography package is required for MySQL caching_sha2_password auth.

Configure the Database

Open phpMyAdmin in your browser. Make sure the user in your .env file has privileges on the karaoke database. If not, in phpMyAdmin run:

GRANT ALL PRIVILEGES ON dbasename.* TO 'YourUser'@'localhost';
FLUSH PRIVILEGES;

Verify your .env file on the server has the correct values:

cat /var/www/html/rb222/.env

It should contain

DB_HOST=localhost
DB_USER=YourUser
DB_PASS=YourPassword
DB_NAME=dbasename
SECRET_KEY=some-random-string
HOST_NAME= name for the host side
PORT=3001

Important: Use a different port from any other apps you have running. I'm using 3001 here — pick whatever's free.

Test App Manually

cd /var/www/html/myapp
source venv/bin/activate
python3 app.py

You should see "Karaoke app running on http://localhost:3001". In another SSH window test

sudo nano /etc/systemd/system/rb222.service

Create the systemd Service

Paste

[Unit]
Description=My App mydir (Gunicorn)
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/html/myapp
Environment="PATH=/var/www/html/myapp/venv/bin"
ExecStart=/var/www/html/myapp/venv/bin/gunicorn --workers 3 --bind 127.0.0.1:3001 app:app
Restart=always

[Install]
WantedBy=multi-user.target

Save (Ctrl+O, Enter, Ctrl+X).

Set permissions:

sudo chown -R www-data:www-data /var/www/html/myapp

Enable and start

sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp
sudo systemctl status myapp

You should see active (running). If not:

sudo journalctl -u rb222 -n 50