This article describes how to use Virtualenv, Uwsgi, nginx to flask the steps of the deployment.
Upgrade Packages
Execute the following command to ensure that your machine has the latest packages installed.
sudo apt-get update
sudo apt-get upgrade
Installing Python and Virtualenv
sudo apt-get install build-essential python-dev python-pip
sudo pip install virtualenv
Create a directory to store your Web site
For example, create a MySite directory under the/home/gzlihao/webservice directory
mkdir MySite
Configuring Virtualenv and installing Flask, UWSGI
Virtualenv. env
source. env/bin/activate
Pip Install flask
Pip Install Uwsgi
Create hello.py under the MySite directory
The contents of hello.py are as follows:
fromimport Flaskapp = Flask(__name__)@app.route("/")def hello(): return"Hello World!"if"__main__": app.run(host=‘0.0.0.0‘, port=8080)
Note that the app.run needs to be placed under and the if __name__ == "__main__"
host needs to be set to 0.0.0.0so that UWSGI does not execute it.
Configure Uwsgi
Add the configuration file Uwsgi.ini, which reads as follows:
[Uwsgi]chdir=/home/gzlihao/webservice/mysite home=/home/gzlihao/webservice/mysite/.env module=Hello callable=app master=true processes=2 socket=% (chdir)/mysite.sock chmod-socket=666 logfile-chmod=644 daemonize=% (chdir)/uwsgi.log uid=Gzlihao gid=Gzlihao procname-prefix-spaced=MySite
Start Uwsgi
Uwsgi–ini Uwsgi.ini
You can see the following files added under the MySite directory
mysite.sock
Anduwsgi.log
Installing Nginx
Deactivate
sudo apt-get install Nginx
Configure Nginx
Create the mysite.conf file under the/etc/nginx/sites-availabel directory, as follows:
server { 80; server_name www.mysite.com; charset utf-8; 5M; location / { include uwsgi_params; uwsgi_pass unix:/home/gzlihao/webservice/mysite/mysite.sock; } location /static { alias /home/gzlihao/webservice/mysite/static; }}
In order to use this configuration file to take effect, create a mysite.conf link file:
Cd/etc/nginx/sites-enabled
Ln-s. /sites-available/mysite.conf mysite.conf
Nginx Restart
Syntax checking of nginx configuration prior to reboot
Nginx-t
Then a smooth restart of the Nginx
Kill-hup 27198
Where 27198 is the Nginx master process number, you can use the ps -efl | grep nginx | grep master
get
Visit website
Edit C:\Windows\System32\drivers\etc\hosts
the file to add a
[Deployment MySite's IP address] www.mysite.com
For example
192.168.229.166 www.mysite.com
Open the browser, input www.mysite.com
, enter, you can see the output results.
Resources
- Running flask with Virtualenv, Uwsgi, and Nginx
- Serving Flask with Nginx
- Flask Uwsgi
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Use Virtualenv, Uwsgi, nginx to Deployment flask