How To Host Flask Website Using Apache On Ubuntu Server In AWS

Learn to host Flask website using Apache on Ubuntu server in AWS

· 3 min read
How To Host Flask Website Using Apache On Ubuntu Server In AWS

Hello everyone in this article, I am going to demonstrate how you can host the flask website on an Ubuntu server in AWS.

Prerequisite

  1. To get started with this article you must have a running ubuntu server in AWS. If you don’t have one then follow this article.
How To Create An Linux Server Using Aws For Free
Learn how to launch a Linux server using AWS for free.

2. If you have fulfilled the first requirement, then you can set up and configure apache on your Linux server. Here’s the link to the article if you don’t know how to set up apache on a Linux server.

How to install and configure apache on your Linux server
Learn how to configure apache on your Linux server

So, if you get completed with all the above prerequisites, move to the following steps

Steps

  1. First, download and set up the python environment using Miniconda for your project.
mkdir downloads && cd Downloads wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh chmod 777 Miniconda3-latest-Linux-x86_64.sh ./Miniconda3-latest-Linux-x86_64.sh

Then load the remote terminal again so that the changes take place

source ~/.bashrc

Now create the python environment for your project.

conda create -n <project_name> python flask <other_libraries>

Replace <project_name> with your project name and <other_libraries> with the libraries, you want to install as per your project requirement.

So you are all set up with the python environment.

2. Now install the mod_wsgi package that is required to host flask application.

sudo apt-get install libapache2-mod-wsgi

3. Now set up your project and your project tree structure must be like

project_name/
    project_name.wsgi
    project_name/
        app.py
        static/
        templates/

So now if you have arranged your project structure like above, then move to the next part to create the project_name.wsgi file

4. So insert these lines in the project_name.wsgi file and remember to replace them project_name with your original project name.

#!/home/ubuntu/miniconda3/envs/<project_name>/bin
import sys import logging
logging.basicConfig(stream=sys.stderr) sys.path.insert(0,"/home/ubuntu/<project_name>/<project_name>/") from app import app as application
application.secret_key = 'your-secret-key'

If you are done with the WSGI configuration file, move toward the next step.

5. In this step, we are going to create the configuration file for our project. Use the below command to create the file

sudo vim /etc/apache2/sites-available/<project_name>.conf

Replace the <project_name> with your project name. Now insert the following content in the file.

<VirtualHost *:80>
                ServerName <your-server-name-or-domain-name>
                WSGIDaemonProcess <project_name> python-path=/home/ubuntu/<project_name>:/home/ubuntu/miniconda3/envs/<project_name>/lib/python<version>/site-packages
                WSGIProcessGroup <project_name>
                WSGIScriptAlias / /home/ubuntu/<project_name>/<project_name>.wsgi
                <Directory /home/ubuntu/<project_name>/>
                        Require all granted
                        Order allow,deny
                        Allow from all
                </Directory>
                <Directory /home/ubuntu/<project_name>/<project_name>/>
                        Require all granted
                        Order allow,deny
                        Allow from all
                </Directory>
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Here replace the <project_name> with your project name and <version> with the python version in the python miniconda environment. And also <your-server-name-or-domain-name> with your domain name or the IP address of your server.

You are just about to host your flask website, only some of the steps are remaining.

6. Now enable the site and reload apache server.

sudo a2ensite <project_name>.conf sudo systemctl reload apache2

Now you are all set up. You have successfully hosted your flask website. To check open the domain name or IP address (that you used in the configuration file ) in the browser.

Hope you have successfully hosted your flask application by using these steps and if you have any problem or if you are unable to host the flask application you can ask me in the comment section for help.