Because of uwsgi unfamiliar, from the contact with the flask and deployed uwsgi 30 hours.
Use an isolated environment
You can use virtualenv, conda create a new environment. I used here virtualenv.
(1) installation virtualenv
pip3 install virtualenv
(2) created under the project directory virtual environment
directory project: / home / simoe / Documents / flask_uwsgi
change to the directory:
cd /home/simoe/Documents/flask_uwsgi
Create a file called venv, use python3 environment
virtualenv -P usr/bin/python3 venv
where there is a pit, if used directly virtualenv venv
Creating the environment, default python2.7. -p represents the current path, if the directory is not switched to the project under, to be used
virtualenv /home/simoe/Documents/flask_uwsgi usr/bin/python3 venv
to enable the ambient
source 环境名/bin/activate
environment here is called venv, so source venv/bin/activate
close the Environmentdeactivate
Installation flask
Installation flask will be activated in the virtual environment venv the flask is installed in this environment.
pip install flask
According to official guidelines, the establishment of a very simple application.
Test.py create a new file:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'hello world ! '
if __name__ == '__main__':
app.run()
Directly run the file and then open the browser, enter 127.0.0.1:5000
when you see hello world! Proving installed.
Installation uwsgi
uwsgi use the default python2.7, when you use the flash python3 written application deployed to default uwsgi 2.7, it will be like to address bug I encountered endless.
Very troublesome when modifying uwsgi of Python, but installation directly pip3 to put the default language uwsgi changed the Python3. After a lot of pits across to know.
Here you may encounter some errors, such as what version of GCC, first environment-dependent solve
sudo apt-get install build-essential python-dev
then be used directly
pip3 install uwsgi
View version
uwsgi --version
View the version of Python
uwsgi --python-versioin
test
Uwsgi_test.py create a new file in the project directory, write:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
At the command line, run:
uwsgi --http :8000 --wsgi-file uwsgi_test.py
directly run the file and then open the browser, enter 127.0.0.1:8000
proved installed the time when you see the Hello World.
Deploy flask used in uwsgi
As already verified the flask and uwsgi alone can run successfully, then how to deploy applications to the flask uwsgi it?
The principle does not describe in detail here, a simple command can be achieved, provided that your environment and various configurations have been correct.
Uwsgi must pay attention to the default version of Python version and your virtual environment. The above also describes how to view.
Configuration uwsgin_config.ini
Create a file called config.ini in the project directory:
[uwsgi]
http = 127.0.0.1:5000
virtualenv = /home/simon/Documents/flask_uwsgi/venv # 虚拟环境的路径
wsgi-file = /home/simon/Documents/flask_uwsgi/app.py # 前面创建的app.py
processes = 2
threads = 8
buffer-size = 32768
master = true
The parameters can be found here uwsgi configuration in detail
Start / View / stop
Start command
Switch to the directory of the project and use the virtual environment activated, otherwise an error
uwsgi config.ini
I modified the content app.py inside is:
@app.route('/')
def sys_status():
return 'server starts-up successful!'
Open your browser and enter the address 127.0.0.1:5000, If you do not see something like the following screen, it means the configuration or where there are problems.
View the process
ps -a
stop
(1) Ctrl + c
(2) the kill -9 process ID