How to host Flask website using Nginx on Ubuntu server in AWS

Learn to host Flask website using Nginx on Ubuntu server in AWS.

· 4 min read
How to host Flask website using Nginx on Ubuntu server in AWS

Hello everyone in this article, I am going to demonstrate to you how you can host the Flask website on AWS using Nginx.

Pre-requisite

So before moving to the steps, there are some pre-requisite that you must have

  1. Ubuntu server — You must have the Ubuntu server launched in your AWS account. If you have one then it’s good and if you don’t have one you can go through this article to launch your Ubuntu server in AWS.
How To Create An Linux Server Using Aws For Free
Learn how to launch a Linux server using AWS for free.

2. Ubuntu server with basic Nginx configuration — Once you complete the above requirement, you can move forward to set up your Nginx server. Here’s the link to the article to set up the Nginx server.

How To Install And Configure Nginx On Your Linux Server
Learn to install and configure Nginx on your Linux server.

So if you have completed the all above pre-requisite, you can move to the steps.

Steps

  1. Firstly install all the required packages on your Ubuntu server.
sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools

2. Now install the miniconda3 Python environment. We are going to use miniconda to create Python virtualenv.

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

After installing miniconda reload the terminal to get the changes to take place and move back to the base directory. Use this command to do so.

source ~/.bashrc
cd ~

3. Now create the virtual environment required for your project. Use the command given below to create the environment for your project.

conda create -n <environment-name> python flask gunicorn <other-libraries>

In the above command be sure to remove <environment-name> with your environment name and <other-libraries> with all the other libraries required by your project.

4. Now get your project on the server. I recommend you to simply clone it from GitHub or any other git client that you are using. Like I have used GitHub.

git clone https://github.com/<username>/<project-name>

Replace the <username> with your GitHub username & <project-name> with your project name.

Your project should look like

project-name/
    all-files
    wsgi.py

And the content in the wsgi.py will be

from <main-python-file> import app

if __name__ == "__main__":
    app.run()

5. Now move to the project directory, activate the project environment and run the application to test it.

cd <project-name>
conda activate <environment-name>
gunicorn --bind 0.0.0.0:5000 wsgi:app

To test it on the browser,

  • Allow port 5000 from terminal
sudo ufw allow '5000'
  • And also add an inbound rule in AWS console for port 5000.
  • Here you will get the result

6. Now let us add gunicorn to the systemd so that it runs automatically when the system boots up.

sudo vim /etc/systemd/system/<project-name>.service

The content in the file will be

[Unit]
Description=Gunicorn instance to serve myproject
After=network.target

[Service]
User=<username>
Group=www-data
WorkingDirectory=/home/<username>/<project-name>
Environment="PATH=/home/<username>/miniconda3/envs/<environment-name>/bin"
ExecStart=/home/<username>/miniconda3/envs/<environment-name>/bin/gunicorn --workers 3 --bind unix:<project-name>.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target

And remember to replace the variables inside <> with their original values.

7. Now start the systemdservice that you have created in the earlier step.

sudo systemctl start <project-name>
sudo systemctl enable <project-name>
sudo systemctl status <project-name>

8. Most of the steps have been completed, now only Nginx configuration is left. So let’s do it.

Create the Nginx configuration file.

sudo nano /etc/nginx/sites-available/<project-name>

And put the following content in it.

server {
    listen 80;
    server_name <your_domain> www.<your_domain>;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/<username>/<project-name>/<project-name>.sock;
    }
}

Now create a symbolic link of this file to sites-enabled the directory

sudo ln -s /etc/nginx/sites-available/<project-name> /etc/nginx/sites-enabled

Check if everything is correct in the configuration file

sudo nginx -t

Restart the Nginx - sudo systemctl restart nginx

9. !!Done!!. That’s all you can now copy your domain name in your browser to open your web application.

So just follow these simple steps you can easily host your flask application using the Nginx server.

Note — Point your domain name A record to the server public IP4 address to connect through your domain name.

Note — Please remove all the text inside <> with the proper one to get the required result.

So I think that you all have successfully hosted your application by following this article, and if you have any problems you can ask me in the comment section.