Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04

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.

pyenv settings and Python installation

I don't touch Python on the system with one finger. Furthermore, I will not directly mess with Python installed with pyenv. Use virtualenv for all package installations. Also, I think that the pyenv directory is usually created under $ HOME, but Since it is not easy to use from the system under $ HOME, prepare it under / opt here. Therefore, work related to pyenv is often done as root.

I need git for the time being.

$ sudo apt-get install git

Package installation for building Python

The following packages are required to build Python, so install them.

C compiler etc.

$ sudo apt-get install build-essential

SQLite library for SQLite support, etc.

$ sudo apt-get install libsqlite3-dev
$ sudo apt-get install sqlite3
$ sudo apt-get install bzip2 libbz2-dev

SSL library required by pip

$ sudo apt-get install libssl-dev openssl

The readline library required by the readline extension

$ sudo apt-get install libreadline6 libreadline6-dev

Installation of pyenv itself and pyenv-virtualenv plugin

I will do it as root.

sudo su
cd /opt
git clone git://github.com/yyuu/pyenv.git pyenv

echo 'export PYENV_ROOT="/opt/pyenv"' >> ~/.bashrc
echo 'if [ -d "${PYENV_ROOT}" ]; then' >> ~/.bashrc
echo '    export PATH=${PYENV_ROOT}/bin:$PATH' >> ~/.bashrc
echo '    eval "$(pyenv init -)"' >> ~/.bashrc
echo 'fi' >> ~/.bashrc

exec $SHELL -l

cd $PYENV_ROOT/plugins
git clone git://github.com/yyuu/pyenv-virtualenv.git

Installation of Python 3.4.0

sudo su
pyenv install 3.4.0

Easy to use pyenv

As a memo.

--Show list of installable versions

  pyenv install --list

--Install 3.4.0

  pyenv install 3.4.0

--List of installed versions

  pyenv versions

--Creating a virtualenv environment

  pyenv virtualenv 3.4.0 my-virtual-env-3.4.0

--List of virtualenv environments

  pyenv virtualenvs

--Enable virtualenv

  pyenv activate my-virtual-env-3.4.0

--Delete the installed environment

  pyenv uninstall my-virtual-env-3.4.0

Nginx installation

Install the python-software-properties package to use the ʻadd-apt-repository` command

sudo apt-get install python-software-properties

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.

building flask sample application

Create application directory

Create an application directory. All application related files are stored here. _ * However, the virtualenv environment is managed collectively by pyenv under / opt. _

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 virtualenv for the sample application

sudo -s
pyenv virtualenv 3.4.0 demoapp

Flask installation

Install Flask on virtualenv

sudo -s
pyenv activate demoapp
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

pyenv activate demoapp
python /var/www/demoapp/hello.py

Check by connecting to server port: 8080 with a web browser.

uWSGI installation

Building a virtualenv environment for uWSGI

I think uWSGI will be shared by multiple applications, so Prepare separately from the virtualenv for the application.

If you have enabled the demoapp virtualenv for your application, deactivate.

deactivate

virtualenv for uWSGI

sudo -s
pyenv virtualenv 3.4.0 uwsgi-3.4.0

uWSGI installation

sudo -s
pyenv activate uwsgi-3.4.0
pip install uwsgi

Nginx settings

Remove default site settings

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;
    }
}

Put a symbolic link in the Nginx config file directory

sudo ln -s /var/www/demoapp/demoapp_nginx.conf /etc/nginx/conf.d/

Nginx restart

sudo /etc/init.d/nginx restart

Connect to the server with a web browser and check. ** At this point you should get a "502 Bad Gateway" error. ** **

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
home = /opt/pyenv/versions/demoapp

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

pyenv activate uwsgi-3.4.0
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.

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/pyenv/versions/uwsgi-3.4.0/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 Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04
Create a web application execution environment of Python3.4 + Nginx + uWSGI + Flask with haste using venv on Ubuntu 14.04 LTS
Until building a Python development environment using pyenv on Ubuntu 20.04
[Pyenv] Building a python environment with ubuntu 16.04
Create Python + uWSGI + Nginx environment with Docker
Web application created with Python + Flask (using VScode) # 1-Virtual environment construction-
Build python environment with pyenv on EC2 (ubuntu)
Create Nginx + uWSGI + Python (Django) environment with docker
[Venv] Create a python virtual environment on Ubuntu
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
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
Build a Python development environment using pyenv on MacOS
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Web application with Python + Flask ② ③
Web application with Python + Flask ④
How to create a Python 3.6.0 environment by putting pyenv on Amazon Linux and Ubuntu
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
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
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
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Create a python environment on your Mac
[Python] Create a virtual environment with Anaconda
[Python] Create a Batch environment using AWS-CDK
[Python] A quick web application with Bottle!
Create a simple web app with flask
Run a Python web application with Docker
Build a python virtual environment with pyenv
Create a web service with Docker + Flask
Steps to create a Python virtual environment with VS Code on Windows
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
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.
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
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.
Create a Python console application easily with Click
Create a Python virtual development environment on Windows
Using Flask with Nginx + gunicorn configuration [Local environment]
Build a python execution environment with VS Code
[ES Lab] I tried to develop a WEB application with Python and Flask ②