├── README.md ├── __init__.py ├── deploy.sh ├── sampleSite.conf ├── sampleWsgi.wsgi └── templates └── index.html /README.md: -------------------------------------------------------------------------------- 1 | # FlaskDeploymentUbuntu18 2 | A Minimal Flask App along with deployment instructions 3 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | app = Flask(__name__) 3 | 4 | @app.route('/') 5 | def index(): 6 | return render_template('index.html') 7 | 8 | 9 | if __name__ == "__main__": 10 | app.run(host='0.0.0.0', port=8080) -------------------------------------------------------------------------------- /deploy.sh: -------------------------------------------------------------------------------- 1 | # INITIAL SERVER SETUP 2 | adduser harry 3 | usermod -aG sudo harry 4 | ufw app list # [to check] 5 | ufw allow OpenSSH 6 | ufw enable # [y enter] 7 | ufw status # [to check] 8 | # Now login as harry using putty and ssh 9 | 10 | 11 | # INSTALLING APACHE 12 | sudo apt update 13 | sudo apt --assume-yes install apache2 14 | sudo ufw app list 15 | sudo ufw app info "Apache Full" # [just to see info] 16 | sudo ufw allow in "Apache Full" 17 | 18 | 19 | # INSTALLING MYSQL 20 | sudo apt --assume-yes install mysql-server 21 | sudo mysql 22 | mysql> SELECT user,authentication_string,plugin,host FROM mysql.user; 23 | mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'MyStrongPassword123'; # [This will be mysql password] 24 | mysql> FLUSH PRIVILEGES; 25 | mysql> SELECT user,authentication_string,plugin,host FROM mysql.user; # [just to check] 26 | mysql> exit 27 | 28 | 29 | # INSTALLING PHP 30 | sudo apt --assume-yes install php libapache2-mod-php php-mysql 31 | sudo nano /etc/apache2/mods-enabled/dir.conf # replace index.html by index.php and vice versa 32 | sudo systemctl restart apache2 33 | sudo nano /var/www/html/info.php 34 | 35 | # Create info.php to test as follows: 36 | 39 | 40 | 41 | # INSTALLING PHPMYADMIN 42 | sudo apt install phpmyadmin php-mbstring php-gettext # press space to select apache2 43 | sudo phpenmod mbstring 44 | sudo systemctl restart apache2 45 | sudo mysql 46 | SELECT user,authentication_string,plugin,host FROM mysql.user; # (just to see) 47 | SELECT user,authentication_string,plugin,host FROM mysql.user; # (to check) 48 | CREATE USER 'harry'@'localhost' IDENTIFIED BY 'MyStrongPassword123'; 49 | GRANT ALL PRIVILEGES ON *.* TO 'harry'@'localhost' WITH GRANT OPTION; 50 | exit 51 | 52 | 53 | # DEPLOYING FLASK APP 54 | sudo apt-get install libapache2-mod-wsgi-py3 55 | sudo a2enmod wsgi 56 | cd /var/www 57 | sudo mkdir cwh 58 | cd cwh 59 | sudo mkdir cwh 60 | cd cwh 61 | 62 | 63 | # GITHUB SSH KEYS 64 | # Do sudo su before these steps 65 | ssh-keygen -t rsa -b 4096 -C "name@domain.com" 66 | eval $(ssh-agent -s) 67 | ssh-add ~/.ssh/id_rsa 68 | copy using vim ~/.ssh/id_rsa.pub 69 | 70 | 71 | sudo apt --assume-yes install python3-pip 72 | sudo pip3 install virtualenv 73 | sudo virtualenv venv 74 | source venv/bin/activate 75 | sudo apt --assume-yes install libmysqlclient-dev 76 | pip install flask flask-sqlalchemy requests bs4 pandas mysqlclient 77 | deactivate 78 | sudo nano /etc/apache2/sites-available/cwh.conf # ADD Following 79 | 80 | ServerName server.com 81 | ServerAdmin name@server.com 82 | WSGIScriptAlias / /var/www/cwh/cwh.wsgi 83 | 84 | Order allow,deny 85 | Allow from all 86 | 87 | Alias /static /var/www/cwh/cwh/static 88 | 89 | Order allow,deny 90 | Allow from all 91 | 92 | ErrorLog ${APACHE_LOG_DIR}/error.log 93 | LogLevel warn 94 | CustomLog ${APACHE_LOG_DIR}/access.log combined 95 | 96 | 97 | sudo a2ensite cwh 98 | cd /var/www/cwh 99 | sudo nano cwh.wsgi # ADD 100 | #!/usr/bin/python 101 | activate_this = '/var/www/cwh/cwh/venv/bin/activate_this.py' 102 | exec(open(activate_this).read(), dict(__file__=activate_this)) 103 | import sys 104 | import logging 105 | logging.basicConfig(stream=sys.stderr) 106 | sys.path.insert(0,"/var/www/cwh/") 107 | 108 | from cwh import app as application 109 | application.secret_key = 'Put-your-secret-1234-key!@$$*&&*(' 110 | 111 | 112 | # SSL CERTIFICATE 113 | sudo add-apt-repository ppa:certbot/certbot 114 | sudo apt --assume-yes install python-certbot-apache 115 | sudo certbot --apache -d domain.com -d www.domain.com 116 | sudo certbot renew --dry-run -------------------------------------------------------------------------------- /sampleSite.conf: -------------------------------------------------------------------------------- 1 | 2 | ServerName server.com 3 | ServerAdmin name@server.com 4 | WSGIScriptAlias / /var/www/cwh/cwh.wsgi 5 | 6 | Order allow,deny 7 | Allow from all 8 | 9 | Alias /static /var/www/cwh/cwh/static 10 | 11 | Order allow,deny 12 | Allow from all 13 | 14 | ErrorLog ${APACHE_LOG_DIR}/error.log 15 | LogLevel warn 16 | CustomLog ${APACHE_LOG_DIR}/access.log combined 17 | -------------------------------------------------------------------------------- /sampleWsgi.wsgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | activate_this = '/var/www/cwh/cwh/venv/bin/activate_this.py' 3 | exec(open(activate_this).read(), dict(__file__=activate_this)) 4 | import sys 5 | import logging 6 | logging.basicConfig(stream=sys.stderr) 7 | sys.path.insert(0,"/var/www/cwh/") 8 | 9 | from cwh import app as application 10 | application.secret_key = 'Put-your-secret-1234-key!@$$*&&*(' -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Hello world 4 | 5 | 6 | Hello world 7 | 8 | --------------------------------------------------------------------------------