Create a web application execution environment of Python3.4 + Nginx + uWSGI + Flask with haste using venv on Ubuntu 14.04 LTS

Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04 http://qiita.com/5t111111/items/e170fead91261621b054

In this 14.04 version, pyenv dependency is stopped and only venv is used.

Character

――I An ordinary high school student who attends a private high school that was a girls' school until last year. I hate humans. It will not appear after this.

Nginx installation

Added Nginx stable repository

$ sudo add-apt-repository ppa:nginx/stable

Usual update / upgrade

$ sudo apt-get update
$ sudo apt-get upgrade

Install and launch Nginx

$ sudo apt-get install nginx
$ sudo /etc/init.d/nginx start

Connect with your browser and check that the Nginx greetings page is displayed.

スクリーンショット 2014-04-19 23.55.07.png

building flask sample application

Create application directory

Create an application directory. All application related files, including the venv virtual environment, are stored in this.

$ sudo mkdir -p /var/www/demoapp

If the user authority remains root, change it to the user you are using

$ sudo chown -R username:username /var/www/demoapp/

Creating a venv for a sample application

Unfortunately, it seems that creating venv with Python 3.4 doesn't work right now. http://qiita.com/5t111111/items/1643ba04104e75589ad4

So you need to create venv with the --without-pip option and then manually install pip.

$ pyvenv-3.4 --without-pip /var/www/demoapp/venv
$ source /var/www/demoapp/venv/bin/activate
$ mkdir ~/src
$ cd ~/src
$ curl -O https://pypi.python.org/packages/source/s/setuptools/setuptools-3.4.4.tar.gz
$ tar xvfz setuptools-3.4.4.tar.gz
$ cd setuptools-3.4.4/
$ python setup.py install
$ cd ..
$ curl -O https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz
$ tar xvfz pip-1.5.4.tar.gz
$ cd pip-1.5.4/
$ python setup.py install

Flask installation

Enable venv

$ source /var/www/demoapp/venv/bin/activate

Flask installation

$ pip install flask

Creating a Flask application

Create /var/www/demoapp/hello.py.

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080)

Flask application launch test

With venv enabled.

$ python /var/www/demoapp/hello.py

Check by connecting to server port: 8080 with a web browser. スクリーンショット 2014-04-19 23.55.19.png

Nginx settings

Removed default site settings (symlinks)

$ sudo rm /etc/nginx/sites-enabled/default

Create a configuration file for the sample application

Create /var/www/demoapp/demoapp_nginx.conf

server {
    listen      80;
    server_name localhost;
    charset     utf-8;
    client_max_body_size 75M;

    location / { try_files $uri @yourapplication; }
    location @yourapplication {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/demoapp/demoapp_uwsgi.sock;
    }
}

The uWSGI server communicates with the web server Nginx via UNIX sockets or TCP. The above settings specify communication on UNIX sockets. uwsgi_pass unix:/var/www/demoapp/demoapp_uwsgi.sock;

Put a symbolic link in the Nginx config file directory

$ sudo ln -s /var/www/demoapp/demoapp_nginx.conf /etc/nginx/sites-enabled/

Nginx restart

$ sudo /etc/init.d/nginx restart

Connect to the server with a web browser and check. ** At this point, the uWSGI server is not started, so you should get a "502 Bad Gateway" error like this. ** ** スクリーンショット 2014-04-20 0.26.56.png

uWSGI installation

Building a venv environment for uWSGI

uWSGI can also be installed from apt, but since the version is a little old, install the latest one with pip.

I think that uWSGI will be shared by multiple applications in the future, so I will prepare it separately instead of installing it in venv for the application, but since I do not want to play with the Python environment of the system, here again uWSGI with venv under / opt Create an environment for.

If you have enabled demoapp venv for your application, deactivate it.

$ deactivate

Installation of packages required to build uWSGI

You will need the following, so install it.

--C compiler --Python3 header

$ sudo apt-get install build-essential python3-dev

Create venv for uWSGI

Since it is created under / opt, work is done as root, but if you are concerned, you can change another location or authority. In that case, it is necessary to modify the path of uWSGI binary with the following settings.

As with the venv for the application, the usual method doesn't work, so install pip manually. (Troublesome ...) The following describes the procedure assuming that the source when creating venv for the application remains.

$ sudo -s
# pyvenv-3.4 --without-pip /opt/venv/uwsgi
# source /opt/venv/uwsgi/bin/activate
# cd ~/src
# cd setuptools-3.4.4/
# python setup.py install
# cd ..
# cd pip-1.5.4/
# python setup.py install

uWSGI installation

$ source /opt/venv/uwsgi/bin/activate
$ pip install uwsgi

uWSGI settings

Create a uWSGI configuration file for your application

Create /var/www/demoapp/demoapp_uwsgi.ini

[uwsgi]
#application's base folder
base = /var/www/demoapp

#python module to import
app = hello
module = %(app)

#virtualenv folder
virtualenv = /var/www/demoapp/venv

pythonpath = %(base)

#socket file's location
socket = /var/www/demoapp/%n.sock

#permissions for the socket file
chmod-socket    = 666

#the variable that holds a flask application inside the module imported at line #6
callable = app

#location of log files
logto = /var/log/uwsgi/%n.log

Creating a log output destination directory

$ sudo mkdir -p /var/log/uwsgi
$ sudo chown -R username:username /var/log/uwsgi

Start uWSGI

$ uwsgi --ini /var/www/demoapp/demoapp_uwsgi.ini

Connect to the server with a web browser and check. If Nginx and uWSGI are able to communicate with the socket without any problem, Hello World! Is displayed. スクリーンショット 2014-04-20 11.05.01.png

uWSGI Emperor

uWSGI Emperor is a function that reads the uWSGI configuration file and starts the uWSGI process. Multiple settings can be read and process startup can be managed collectively.

Creating an Upstart file

Create /etc/init/uwsgi.conf

description "uWSGI"
start on runlevel [2345]
stop on runlevel [06]
respawn

env UWSGI=/opt/venv/uwsgi/bin/uwsgi
env LOGTO=/var/log/uwsgi/emperor.log

exec $UWSGI --master --emperor /etc/uwsgi/vassals --die-on-term --uid www-data --gid www-data --logto $LOGTO

The last line above means to look for the config file that exists in / etc / uwsgi / vassals and start the uWSGI daemon, so Create a symbolic link to the uWSGI configuration file of the sample application in / etc / uwsgi / vassals.

$ sudo mkdir -p /etc/uwsgi/vassals
$ sudo ln -s /var/www/demoapp/demoapp_uwsgi.ini /etc/uwsgi/vassals

Permission settings

The uWSGI daemon is set to start as the www-data user. Therefore, keep the application and log directories owned as www-data.

$ sudo chown -R www-data:www-data /var/www/demoapp/
$ sudo chown -R www-data:www-data /var/log/uwsgi/

Since Nginx and uWSGI work with the same www-data user, change the socket permission to 644.

Edit /var/www/demoapp/demoapp_uwsgi.ini

...
#permissions for the socket file
chmod-socket = 644

Start uWSGI

$ sudo start uwsgi

Connect to the server with a web browser and check. If everything is fine so far, Hello World! Will be displayed.

At the end

Now that you can run your Flask application. I think there are various points to be aware of. Especially around Nginx and uWSGI, please squeeze the settings in each document and google.

reference

http://vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/

Recommended Posts

Create a web application execution environment of Python3.4 + Nginx + uWSGI + Flask with haste using venv on Ubuntu 14.04 LTS
Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04
[Venv] Create a python virtual environment on Ubuntu
Web application created with Python + Flask (using VScode) # 1-Virtual environment construction-
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Create Python + uWSGI + Nginx environment with Docker
Create Nginx + uWSGI + Python (Django) environment with docker
Create a Python execution environment on IBM i
[Raspberry Pi] Publish a web application on https using Apache + WSGI + Python Flask
I made a Python3 environment on Ubuntu with direnv.
Launch a Python web application with Nginx + Gunicorn with Docker
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Web application with Python + Flask ② ③
Web application with Python + Flask ④
Until building a Python development environment using pyenv on Ubuntu 20.04
Creating a web application using Flask ②
Building a Python environment on Ubuntu
Create a Python environment on Mac (2017/4)
Create a virtual environment with Python!
Create a python environment on centos
Creating a web application using Flask ①
Creating a web application using Flask ③
Creating a web application using Flask ④
Create a Python execution environment for Windows with VScode + Remote WSL
Build a Flask / Bottle-like web application on AWS Lambda with Chalice
Build a Python execution environment using GPU with GCP Compute engine
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a USB boot Ubuntu with a Python environment for data analysis
[Pyenv] Building a python environment with ubuntu 16.04
Create a python environment on your Mac
[Python] Create a virtual environment with Anaconda
[Python] Create a Batch environment using AWS-CDK
Steps to create a Python virtual environment with VS Code on Windows
It is good to create an environment with runtime error => venv when using pyplot backends of macosx on a virtual environment created with virtualenv.
[Python] A quick web application with Bottle!
Create a simple web app with flask
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
Run a Python web application with Docker
Create a web service with Docker + Flask
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
I want to make a web application using React and Python flask
Implement a simple application with Python full scratch without using a web framework.
Create an application that inputs, displays, and deletes forms by using an array as a DB with Python / Flask.
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Web application production course learned with Flask of Python Part 2 Chapter 1 ~ JSON exchange ~
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Prepare the execution environment of Python3 with Docker
How to create a Python virtual environment (venv)
Ubuntu18.04.05 Creating a python virtual environment in LTS
Build python environment with pyenv on EC2 (ubuntu)
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
Simply build a Python 3 execution environment on Windows
Build a python environment with ansible on centos6
Vienna with Python + Flask web app on Jenkins
Create a virtual environment with conda in Python
Create a python3 build environment with Sublime Text3
Hello World with nginx + uwsgi + python on EC2
Run Python web apps on NGINX + NGINX Unit + Flask
Create a web map using Python and GDAL
Launch a web server with Python and Flask
Run Flask on CentOS with python3.4, Gunicorn + Nginx.