Deploy Django Website to AWS (EC2) ================================== To deploy our website, we chose to use AWS for reasons described in the technical memo, but it's primarily because popularity and funding options. To do that, I have decided to use AWS EC2, which for all practical purposes, is just a remote computer. What we need to setup to run this website in production is a webserver, which for safety, efficiency and other reasons I'm not sure of is the best way to run a website. Examples of webservers include Apache, and more recently, nginx. So to deploy our website, we need to a) find a place to run it on b) set up a webserver, and c) set up the django environment. Here, we do that on AWS EC2 with an Ubuntu instance with an Apache web server and mod_wsgi for connection. See the following steps to deploy. To do this, we're using the `Django Configuration `_ with Apache2 and WSGI (whatever that is) somewhat following `this tutorial `_. You don't need to look at it though. #. Preprocessing #. We're using the whitenoise package to collect all the static files. I know very minimally about how it works or why we need it, but it collects static files (which we need to deploy) in the "staticfiles" folder under MLDjango/mldjango. To start with, and make sure we're up to date, we need to update those files. Run the following. .. code-block:: bash $ python manage.py collectstatic #. Push all updates to the Github. We'll transfer over the repo that way. .. code-block:: bash $ git add . $ git commit -m "Final Updates" $ git push #. Create and launch AWS EC2 computer #. Login to AWS, go to EC2. #. Click Launch instance, name the instance to your hearts content #. Pick the Ubuntu Application and OS Image (mod_wsgi is not easily accessible on amazon linux, so make sure this is right) #. Pick t2.micro for cheapest free tier price #. Allow SSH traffic (and https and http if you can) #. Create Key Pair (you shouldn't need this unless SSHing) #. That's it, click Launch Instance #. Once it's been created, we need to make sure the EC2 instance allows inbound traffic for websites #. Go to Actions -> Security -> Change Security Groups. You need to have the `linked`_ rules. Figure out how to create a security group and add it if not.They allow http, https,and ssh traffic. The 8000 port allows to run the debug server to test. #. Make a note of the Public IPv4 adress, that's the one that we'll use. #. Set up EC2 computer #. Click Connect on the instance and do ec2 instance connect. Open it. #. Run the following commands. .. code-block:: bash $ sudo apt-get update # To make sure it is up to date. $ sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3 python3.12-venv # This installs python pip, the webserver, and mod_wsgi $ cd ~ # This is where we place all files for the webserver before moving to /var/wwww $ sudo python3 -m venv ~/mldjango-venv # Create Virtual Environment $ source ~mldjango-venv/bin/activate $ Activate Environment $ sudo git clone [REPO-LINK] # Clone repo, I use https because it was easier $ cd MLDjango # Enter repo $ pip install -r requirements.txt # Download packages $ cd mldjango # get to manage.py folder $ python manage.py migrate # Migrate databases $ python manage.py ingest_data LakeLevels StaticQuestionProducts [ANYOTHERs] # Ingest tables as needed see tables docs for more info $ python manage.py createsuperuser # Don't forget to recreate admin. $ python manage.py runserver 0.0.0.0:800 # Test if django is working in debug environment and then go to the Public IPv4 address:8000 to see if it shows up. Remember it's http not https! $ ^C # To exit server $ cd ~ #move both virtual environment and django to /var/www $ sudo mv MLDjango /var/www/ $ sudo mvmldjango-venv /var/www/ $ cd /var/www # Change permissions so apache can use it $ sudo chown -R www-data: MLDjango $ sudo chown -R www-data: mldjango-venv $ cd /etc/apache2 # Add the following the apache2.conf or httpd.conf file $ sudo vim apache2.conf # Add it here Alias /static /var/www/MLDjango/mldjango/staticfiles Require all granted Require all granted WSGIScriptAlias / /var/www/MLDjango/mldjango/mldjango/wsgi.py WSGIDaemonProcess mldjango python-home=/var/www/mldjango-venv python-path=/var/www/MLDjango/mldjango WSGIProcessGroup mldjango $ sudo service apache2 restart # Restart the webserver. That's it! Go to http://PublicIPv4Adress #. Set up Https: See the following link: https://www.arubacloud.com/tutorial/how-to-enable-https-protocol-with-apache-2-on-ubuntu-20-04.aspx .. _linked: https://i.imgur.com/RcAvLlc.png