Deploy python flask app on Linux Server

After creating a Python App on your local machine you need to deploy your app on the server so that app can be accessed from anywhere. On your local environment, you run your app by using python or a python3 prompt like below.

python myapp.py

The app will stop after you close the terminal. So for deploying the Python app on the server, you need to create a service container that will run permanently your app. We are proving steps to deploy your python app on the server.

Step 1.

Create a python app and upload into a directory on the server. In our example, I have created index.py file that contains entrance point of application.

sudo mkdir /var/www/mypythonapp
sudo touch /var/www/mypythonapp/index.py
sudo vim /var/www/mypythonapp/index.py

 

Step 2.

Install all dependencies for your app using pip. For example:

pip install python-flask

 

Step 3.

Now create a service for your app and open for edit.

sudo vim /etc/systemd/system/mypythonapp.service
sudo nano /etc/systemd/system/mypythonapp.service

Now write the script in this file.

[Unit]
Description=My Python App
After=multi-user.target

[Service]
WorkingDirectory=/var/www/mypythonapp
ExecStart=/usr/bin/python3 /var/www/mypythonapp/index.py
Type=idle

[Install]
WantedBy=multi-user.target

Here /usr/bin/python3 is my python startup path and after that my python file path to start running.

If you want to store all python output in a log file then provide path od log file as below and make sure that log directory is writable.

[Unit]
Description=My Python App
After=multi-user.target

[Service]
WorkingDirectory=/var/www/mypythonapp
ExecStart=/usr/bin/python3 /var/www/mypythonapp/index.py > /var/www/mypythonapp/log.txt
Type=idle

[Install]
WantedBy=multi-user.target

Now save and close this file. - (ctrl+o then ctrl+x)

 

Step 4.

Change this service file permission to 644.

sudo chmod 644 /etc/systemd/system/mypythonapp.service

 

Step 5.

Now apply this service and enable to execute.

sudo systemctl daemon-reload
sudo systemctl enable mypythonapp.service

 

Step 6.

Now start your service.

sudo systemctl start mypythonapp.service

Your app is now running.

To stop the app run following command.

sudo systemctl stop mypythonapp.service

To check the status of service run following command.

sudo systemctl status mypythonapp.service

 

Now your flask server is ready on your given port.

If you want to deploy this server with nginx on a domain then you can configure a domain on nginx with following configuration:

server {
        listen 80;
        root /home/cyberboxer/foodtool/ui-ppg/out;
        index index.html;
        server_name ppg-api.cyberboxer.com;
        location / {
                # Simple requests
                if ($request_method ~* "(GET|POST)") {
                     add_header "Access-Control-Allow-Origin"  *;
                }

                if ($request_method = OPTIONS ) {
                     add_header "Access-Control-Allow-Origin"  *;
                     add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
                     add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
                     add_header 'Access-Control-Allow-Headers' *;
                     return 200;
                }

                proxy_pass http://127.0.0.1:5000/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
        }
}

 

Keywords: