We’re going to use Digital Ocean to host our app. I also considered Heroku. If we were better with Docker, I’d also consider using Azure or Google Cloud. So don’t be afraid to look around at other options besides the stuff I spell out here. You really can figure this stuff on your own and chart a different path if you're willing to grind it out
Register a domain and point your domain’s to DigitalOcean’s nameservers: https://www.digitalocean.com/community/tutorials/how-to-point-to-digitalocean-nameservers-from-common-domain-registrars https://www.digitalocean.com/community/tutorials/an-introduction-to-digitalocean-dns
Setup your server’s basic settings: https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04 This includes our ufw firewall: https://www.digitalocean.com/community/tutorials/how-to-setup-a-firewall-with-ufw-on-an-ubuntu-and-debian-cloud-server
Setup python (check on your version of ubuntu). You're not trying to get a demo app online, we're mostly using commands like
sudo apt install python3.7-dev: https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-programming-environment-on-ubuntu-18-04-quickstart
Make sure we have the basics for our database (install, install, install!):
Once you get your venv activated, don't forget
pip install --upgrade pip wheel
Install and configure nginx, your webserver (remember the overview from the beginning of class): https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-16-04 -[ at this point you should be able to go to http://your-domain and see a blank, nginx page ]-
Update your git repository and your requirements.txt (video guide). We’ve got to package your app ready to ship. Calling the deployment of software “shipping” is really cool and people think you’re cool when you say it. So instead of "setting up my app on a remote server" we can say, "shipping a new version." Nice.
Learn GitHub if you haven’t yet. You’ll be super glad you made the time. Codecademy and other GIT-related courses are great, too.
Pull your app’s repo and setup your dependencies before serving up your app: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-16-04 Notes to complement the Digital Ocean guide:
If admin rights are needed, you may need a command like this:
sudo -H /home/myuser/venv/bin/python -m pip install -r requirements.txt
My wsgi.py file:
from myapp import app
if __name__ == "__main__":
There are many critical files in this process, but the one that ties them all together is:
sudo nano /etc/systemd/system/flaskinni.service
Helpful articles if you’re running into some trouble: https://stackoverflow.com/questions/39937071/uwsgi-configuration-with-flaskapp
sudo cat /var/log/syslog frequently to see the errors being reported by the uWSGI service
sudo less /var/log/nginx/error.log to see the errors just from nginx (they'll otherwise be mixed in with the syslog)
If everything is working up to this point, your project will load a 500 error. Track down the error and it should be reporting that the connection to the database has failed.
Setup postresql on your server (adjust to the version of Ubuntu you're using)
Postgres starts with a super user with the somewhat unoriginal name of postgres. You can use your admin powers to switch to that user by typing
sudo -iu postgres and now you can use
psql and you'll have full control to setup a new user. Read up on how to add a new user in psql. The lazy, slightly dangerous way is to
createuser -P -s -e user_name
Once you've created your user in psql, update your
private.py file with the user and password.
We've got to test to make sure your database user can log into the database you've created. If your database user also exists in Linux, you can do the same
sudo -iu trick we did before and then open up
psql. This command will allow you to be more specific:
psql user_name -h 127.0.0.1 -d db_name
You may end up using
ALTER USER user_name WITH PASSWORD 'new_password'; from the postgres user.
Change all the passwords used in private.py Open the python interpreter (by typing python). Assuming you're running 3.6, you can just run (enter one line at a time):
import random, string''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(40))
Secure your HTTP connection: https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04
sudo apt install python-certbot-nginx
sudo certbot --nginx -d mysite.net
Practice deploying updates. Add, commit, and push from your development environment and pulling over SSH. Make a change to the database and use migrations to update your models without losing data.
Making changes to your application
After building a cool new feature on Cloud9
git add .
git commit -m “fixed the thing”
git push origin master
Then SSH to your server, navigate to your folder and git pull origin master