Skip to content

Instantly share code, notes, and snippets.

@luyaor
Last active November 9, 2018 11:48
Show Gist options
  • Save luyaor/f63e18123bd7f47bfe2a1f586cae02ba to your computer and use it in GitHub Desktop.
Save luyaor/f63e18123bd7f47bfe2a1f586cae02ba to your computer and use it in GitHub Desktop.
A Simple Tutorial for deploying your Flask application with uWSGI + nginx on server without root permission

replace $your_home_path as your home path like /home/renluyao

it will create two folders under $your_home_path: /test and /nginx.

install flask using pip

pip install flask

create a flask program

mkdir -p ~/test
cd ~/test
vi hello.py

modify hello.py as following:

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=5000)

install nginx without root

# create the directory structure for compiling source
mkdir -p ~/.local/src
cd ~/.local/src

# download pcre (could be in repository if root will install for you)
wget http://kent.dl.sourceforge.net/sourceforge/pcre/pcre-7.8.tar.gz
tar -xzvf pcre-7.8.tar.gz

# download nginx
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar -xzvf nginx-1.8.1.tar.gz

# compile and install it
cd nginx-1.8.1
./configure --prefix=$your_home_path/nginx
make && make install

open conf/nginx.conf

cd ~/nginx
vi conf/nginx.conf 

modify conf/nginx.conf as following:

worker_processes 1;
error_log stderr;
daemon off;
pid nginx.pid;

events {
  worker_connections  1024;
}

http {
  server {
    listen            8080;

    location / {
      include uwsgi_params;
      uwsgi_pass unix:$your_home_path/test/hello.sock;
    }
  }
}

run nginx:

cd ~/nginx
./sbin/nginx

Here, if can't use 8080 port, you can change it like 12345 first.

(open a new terminal)

install uwsgi using pip

pip install uwsgi

open config.ini

cd ~/test
vi config.ini

modify config.ini as following:

[uwsgi]
module = hello:app
master = true
processes = 4
chdir = $your_home_path/test
socket = $your_home_path/test/hello.sock
chmod-socket = 666
vacuum = true

run uwsgi:

uwsgi --ini config.ini

Test:

open your browser and test your website:

$your_server_ip(set as 8080 port) or $your_server_ip:12345(set as 12345 port)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment