Preparing a Linux server to run a web application in Python

March 11, 2020

Lang: cs en de es

There are many technologies that can be used to develop web applications. One of them is the Python language and the Django framework. In this article, I will describe how to prepare a Linux server (VPS) to run a web application that is programmed in Python.

In this article I will describe installing a Linux server with the Debian 9 distribution on which I will run a web application written in Python. First, however, I will need to get the web server and Python environment up and running and connect the web server to the Python application. The result will be a physical server or VPS to run the Python/Django application. Of course, the application will also be connected to SQL database, but that is not the subject of this article - this information is in the PostgreSQL and MySQL/MariaDB databases article.

Background

The installation and configuration procedure is as follows:

  1. Python installation
  2. Installing and getting the Nginx web server up and running
  3. Setting up the Python environment
  4. Setting up Nginx
  5. Installing Django
  6. Setting up uwsgi
  7. Connecting Python application and webserver

Python

The first thing to choose is whether you need Python version 3 or Python version 2.

Installing Python version 3:

apt-get install python3

Install Python version 2:
apt-get install python2

WSGI HTTP

WSGI is a kernel of Web Server Gateway Interface. It is a convention for calling web servers and forwarding requests to web applications. Which is supported by the Django framework written in the Python programming language. With this framework, I'll show you how to get a Python webserver up and running. Details what is WSGI.

In addition to the Django framework, there are other frameworks written in Python such as Flask.

As an implementation of WSGI, we can use Gunicorn or UWSGI. I use UWSGI somewhere and Gunicorn somewhere.

Installing uwsgi:

apt-get install uwsgi
We don't even need to install uwsgi on the system, as we can use the version we install with the libraries for the application.

As I mentioned earlier, we will use Nginx as the web server. Therefore, if you already have apache web server installed, it is a good idea to clean the system of it with the command:

apt-get purge apache2

To install the Nginx web server, use the command:

apt-get install nginx

Install virtualenv, which is a tool for creating an isolated environment for running Python applications. To install, use the command:

apt-get virtualenv

Python web server general tutorial

Let's create an environment for web applications in Python version 2. For Python version 3, it's almost the same.

Create a directory where we will keep the environment and project data:

DIR=/var/www/test2/mysite
mkdir $DIR
cd $DIR
Create an isolated environment:
virtualenv --python=python2 --always-copy venv
Switching to an isolated environment:
source venv/bin/activate

We will install all the libraries for the isolated environment using pip technology.

We don't need to have UWSGI on the system, but we can install it directly into this isolated command:

pip install uwsgi

Virtual enviroment Python 3

Creating an isolated environment:

virtualenv --python=python3 --always-copy venv
Switching to the environment:
source venv/bin/activate

Setting up an Nginx web server

Create a /etc/nginx/sites-available/mysite_nginx.conf file with the following contents:

# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen 8000;
    # the domain name it will serve for
    server_name example.com; # substitute your machine's IP address or FQDN
    charset utf-8;

    # max upload size
    client_max_body_size 75M;
    # adjust to taste

    # Django media
    location /media {
        alias /path/to/your/mysite/media;
        # your Django project's media files - amend as required
    }

    location /static {
        alias /path/to/your/mysite/static;
        # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass django;
        include /path/to/your/mysite/uwsgi_params;
        # the uwsgi_params file you installed
    }
}

It is good to connect the web server to the application via socket. You also need to set uwsgi to start automatically when the server boots. On old systems these are init scripts, but on today's systems (2019) you will definitely use systemd startup. Here's how to set that up uwsgi starts with sysmted. And don't forget to turn on the auto-start service command:
systemctl enable webapp.service

The result will be a server with an environment for web applications written in the Django framework.
Don't forget to also set up web security with certificate and HTTPS encrypted connection.

Who needs Customized Linux server installation and configuration with Python feel free to contact me.

Video demonstration of setting up a Python webserver

Video demonstration with installation and setup of Linux server for running Python application with Django framework. The web server used is nginx and uwsgi.

    Video description:
  1. Python installation
  2. Nginx web server
  3. Setting up the Python environment
  4. Setting up Nginx
  5. Installing the Django framework
  6. Setting up uwsgi
  7. Connecting the Python application and the webserver

Links

Running Python as a web application:
Django and Nginx
nginx + Django + python 3

List of UWSGI parameters: uwsgi_params

Články na podobné téma

VMware licensing change
Running Microsoft SQL Server on Linux
Backup: the Proxmox Backup Server
Linux as a router and firewall
How to upload a docker image to the Docker Registry
Linux: logical volume management
Linux Software RAID
Running a web application behind a proxy
Mailbox migration
Docker multistage build
Backing up your data by turning on your computer
Podman
Importing Windows into Proxmox virtualization
Docker and PHP mail
Proxmox virtualization
Docker and Cron
Lenovo ThinkPad X1 Carbon: LTE modem EM7544 commissioning
Yocto Project: Build custom operating system for embedded devices
How to address poor file share performance in Docker
How to get started using Docker correctly
Installing Linux on a dedicated HPE ProLiant DL320e server
How to stress test a web application
Why use the JFS filesystem
How to boot from a 4TB drive with GTP using UEFI
Btrfs file system
Raspberry PI
WINE - running Windous programs under Linux
GNU/Linux operating system

Newsletter

If you are interested in receiving occasional news by email.
You can register by filling in your email news subscription.


+