Flask Config – Ubuntu 16

  • Create a user for the project if one doesn’t exist
    root@server[~]# adduser testuser
    Adding user `testuser' ...
    Adding new group `testuser' (1002) ...
    Adding new user `testuser' (1002) with group `testuser' ...
    Creating home directory `/home/testuser' ...
    Copying files from `/etc/skel' ...
    Enter new UNIX password: 
    Retype new UNIX password: 
    passwd: password updated successfully
    Changing the user information for testuser
    Enter the new value, or press ENTER for the default
            Full Name []: Test
            Room Number []: 
            Work Phone []: 
            Home Phone []: 
            Other []: 
    Is the information correct? [Y/n] Y
  • Add the user to sudoers if desired
    root@server[~]# sudo usermod -aG sudo testuser
  • Install the requisite packages
    root@server[~]# apt-get update
    root@server[~]# apt-get install python-pip python-dev nginx
    root@server[~]# pip install virtualenv
  • SSH in as the user
  • Create a project directory
  • Initialize a virtual environment
    user@server[~/projectdir]$ virtualenv venv
  • Activate the virtual environment
    user@server[~/projectdir]$ source venv/bin/activate
    # OR
    user@server[~/projectdir]$ . venv/bin/activate
    (venv) user@server[~/projectdir]$
  • Install Flask and uWSGI
    (venv) user@server[~/projectdir]$ pip install flask uwsgi
  • Create a test page at /home/user/projectdir/test_page.py , example:
    from flask import Flask
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
        return "<h1 style='color:blue'>Hello There!</h1>"
    
    if __name__ == "__main__":
        app.run(host='0.0.0.0')
  • Create the WSGI entry point at /home/user/projectdir/wsgi.py , example:
    from myproject import app
    
    if __name__ == "__main__":
        app.run()
  • Create uWSGI config file at /home/user/projectdir/test_page.ini , example:
    [uwsgi]
    module = wsgi:app
    
    master = true
    processes = 5
    
    socket = test_page.sock
    chmod-socket = 660
    vacuum = true
    
    die-on-term = true
  • Create a systemd Unit file to create a service at /etc/systemd/system/test_page.service , example:
    [Unit]
    Description=uWSGI instance to serve test_page
    After=network.target
    
    [Service]
    User=testuser
    Group=www-data
    WorkingDirectory=/home/testuser/projectdir
    Environment="PATH=/home/testuser/projectdir/venv/bin"
    ExecStart=/home/testuser/projectdir/venv/bin/uwsgi --ini test_page.ini
    
    [Install]
    WantedBy=multi-user.target
  • Start up the service
    user@server[~/projectdir]$ sudo systemctl start test_page
    user@server[~/projectdir]$ sudo systemctl enable test_page
  • Create nginx vhost at /etc/nginx/sites-available/myproject , example:
    server {
        listen 80;
        server_name server_domain_or_IP;
    
        location / {
            include uwsgi_params;
            uwsgi_pass unix:/home/user/projectdir/test_page.sock;
        }
    }
  • Enable the vhost by symlinking to /etc/nginx/sites-enabled/
    user@server[~/projectdir]$ sudo ln -s /etc/nginx/sites-available/test_page /etc/nginx/sites-enabled/test_page
  • Test the config and restart nginx if all is well
    user@server[~/projectdir]$ sudo nginx -t
    user@server[~/projectdir]$ sudo systemctl restart nginx
  • Create a firewall rule allowing traffic to/from nginx
    user@server[~/testdir]$ sudo ufw allow 'Nginx Full'
  • The site will load at the server_name set in the nginx vhost, make sure DNS is pointed, or use an IP.