Full-scale server made with Nginx + uWSGI + Flask + Ubuntu (installation)

From installing a server with Flask, which tends to be complicated

Implementation is in Implementation

Characters

Nginx

Web server Basically, this is the connection from the client Processed after passing So to speak, the prime contractor

uWSGI

Application server This time web server (Nginx) and web application (Flask) A bridge that connects with So to speak, an intermediary

What is WSGI in the first place? Abbreviation for WebServerGatewayInterface (probably read as whiskey) An agreement between a web application (Flask) and a web server (Nginx) Protocol (specification)

Without this For example, when using a certain Web server (A) Web application (A') that hijacked the specification Will have to be used (actually it seems to have been done in the past) And vice versa

WSGI is a standardized form of that specification. Obviously, Flask to be used from now on is also hijacking the WSGI specifications

Flask

Web application framework This guy does the actual processing So to speak, subcontracting

Django is the main Python framework Compared to Django, it has only the minimum necessary functions Super lightweight and easy to implement

Used by Netflix, Reddit, etc.

Virtualenv

Python virtual environment Prevents the system from getting crowded with packages It is better to be able to implement it without it

Preparation Update repository
$ sudo apt update

Package update

$ sudo apt upgrade

Install development tools such as gcc and gdb (required to build uwsgi)

$ sudo apt install build-essential

install openssl

$ sudo apt install openssl

Install pip3

$ sudo apt install python3-pip

Firewall Firewall settings Set the firewall on ubuntu side as well If you don't need it, skip it.

install ufw

$ sudo apt install ufw

Close all ports

$ sudo ufw default deny

Keep the required ports open In my case SSH (22), http (80)

$ sudo ufw allow 22
$ sudo ufw allow 80

Enable firewall

& sudo ufw enable

Check status

$ sudo ufw status

By the way, when connecting with SSH If you do not follow this order, you will not be able to connect Be careful as there is a possibility

Nginx

How to install the latest version of nginx https://nginx.org/en/linux_packages.html https://xn--o9j8h1c9hb5756dt0ua226amc1a.com/?p=3100 From

Until installation if you do not need to install the latest version separately Skip it and OK

Download PGP key

$ wget https://nginx.org/keys/nginx_signing.key

Import key

$ sudo apt-key add nginx_signing.key

Register repository

$ sudo vi /etc/apt/sources.list

#Add to the end bionic part is ubuntu development code
deb http://nginx.org/packages/ubuntu/ bionic nginx
deb-src http://nginx.org/packages/ubuntu/ bionic nginx

The bionic part of ~ / ubuntu / bionic nginx is Rewrite according to Ubuntu version

version Development code
16.04 xenial
18.04 bionic
19.10 eoan
20.04 focal

How to check ubuntu version

$ cat /etc/lsb-release
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic

Update repository information

sudo apt update

Check version

$ apt show nginx
Package: nginx
Version: 1.18.0-1~bionic

At the time of writing the article, 1.18.0 seems to be a stable version

http://nginx.org/en/download.html You can check if it is the latest version with

--- This is how to install the latest version --- </ b>

Installation

$ sudo apt install nginx

Version confirmation

$ nginx -v
nginx version: nginx/1.18.0

Register for the service in advance

$ sudo systemctl enable nginx
$ sudo systemctl start nginx

Success if accessible

$ curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...

If port 80 is open From allowed IP

$ curl http://<Public IP>

But check

Virtualenv

Install with pip3
$ sudo pip3 install virtualenv

Version confirmation

$ virtualenv --version

Create environment

$ virtualenv py36_flask_server --python=python3.6

By the way, if the specified version of Python is not included Install it in advance as it will fail to create

OK if you put it in the environment below

$ . <Environment name>/bin/activate
(py36_flask_server) ubuntu@ip-10-0-0-14:~$

uWSGI

Install with pip3 from the environment created with virtualenv
$pip3 install uwsgi

Check version

$uwsgi --version

By the way, there was also a way to use Anaconda instead of Virtualenv. However, please note that errors occur frequently at the stage of installing uWSGI.

If you want to use Anaconda It will take time, but it will be solved, so refer to the following article https://katsuwosashimi.com/archives/300/python-conda-install-uwsgi-failed/

Flask

Install with pip from the environment created with virtualenv
$pip3 install flask

that's all

Summary It has become quite long at the installation stage

Next is Implementation

Recommended Posts