――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.
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
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
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
sudo su
pyenv install 3.4.0
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
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.
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/
sudo -s
pyenv virtualenv 3.4.0 demoapp
Install Flask on virtualenv
sudo -s
pyenv activate demoapp
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)
pyenv activate demoapp
python /var/www/demoapp/hello.py
Check by connecting to server port: 8080 with a web browser.
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
sudo -s
pyenv activate uwsgi-3.4.0
pip install uwsgi
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. ** **
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
sudo mkdir -p /var/log/uwsgi
sudo chown -R username:username /var/log/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.
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
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