Use Virtualenv, Uwsgi, nginx to Deployment flask

Source: Internet
Author: User
Tags virtualenv

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.sockAnduwsgi.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
    1. Running flask with Virtualenv, Uwsgi, and Nginx
    2. Serving Flask with Nginx
    3. Flask Uwsgi

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use Virtualenv, Uwsgi, nginx to Deployment flask

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.