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.
――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.
$ sudo add-apt-repository ppa:nginx/stable
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install nginx
$ sudo /etc/init.d/nginx start
Connect with your browser and check that the Nginx greetings page is displayed.
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/
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
$ source /var/www/demoapp/venv/bin/activate
$ pip install flask
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)
With venv enabled.
$ python /var/www/demoapp/hello.py
Check by connecting to server port: 8080 with a web browser.
$ sudo rm /etc/nginx/sites-enabled/default
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/
$ 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. ** **
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
You will need the following, so install it.
--C compiler --Python3 header
$ sudo apt-get install build-essential python3-dev
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
$ source /opt/venv/uwsgi/bin/activate
$ pip install uwsgi
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
$ sudo mkdir -p /var/log/uwsgi
$ sudo chown -R username:username /var/log/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.
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.
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
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
$ sudo start uwsgi
Connect to the server with a web browser and check.
If everything is fine so far, Hello World!
Will be displayed.
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.
http://vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/
Recommended Posts