Skip to content

HOWTO: Amazon EC2 Setup

mengdiwang edited this page Dec 15, 2017 · 2 revisions

Flask w/ uWSGI and nginx on EC2

  1. Update AMI tools:

     sudo yum update
    
  2. Install nginx, uWSGI and required libraries:

     sudo yum install python26 python26-devel make automake nginx gcc gcc-c++ python-setuptools git
     sudo easy_install pip
     sudo pip install uwsgi virtualenv
    
  3. Grab the latest Flask from github:

     cd /your/directory/
     git clone https://github.com/hrybacki/crowd-scholar.git
    
  4. Setup a virtualenv for the repo:

     cd /your/directory/crowd-scholar
     virtualenv venv
     . venv/bin/activate
     pip install Flask pymongo
    
  5. Create database directories and launch MongoDB instances:

     sudo mkdir -p /your/directory/crowd-scholar/data/raw
     sudo mkdir -p /your/directory/crowd-scholar/data/articles
    
  6. Download and install MongoDB:

     curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.4.3.tgz
     tar -xzf mongodb-linux-x86_64-2.4.3.tgz
     cd mongodb-linux-x86_64-2.4.3/bin
     ./mongod --version
    
  7. Launch MongoDB instances: (Note: If you see File Preallocator Progress status continuing stop the service and attempt to reexecute it.)

     sudo ./mongod --port 27018 ../../data/raw &
     sudo ./mongod --port 27017 ../../data/articles &
    
  8. Launch the Flask server:

     cd /your/directory/crowd-scholar/
     python main.py &
    
  9. Now, move it to where you want and fix the permissions:

     cd ~
     sudo mkdir -p ~/run
     sudo chown -R nginx:nginx ~
    
  10. Configure uWSGI -- make some directories:

     sudo mkdir -p /var/log/uwsgi
     sudo mkdir -p /etc/uwsgi/apps-available
     sudo mkdir -p /etc/uwsgi/apps-enabled
    
  11. Create a uWSGI configuration file via: sudo vim /etc/init/uwsgi.conf

     description "uWSGI"
     start on runlevel [2345]
     stop on runlevel [06]
     respawn
    
     env UWSGI=/usr/bin/uwsgi
     env LOGTO=/var/log/uwsgi/emperor.log
    
     exec $UWSGI --master --emperor /etc/uwsgi/apps-enabled --die-on-term --uid nginx --gid nginx --logto $LOGTO
    
  12. Start uWSGI:

     start uwsgi
    
  13. Configure a simple fork to run the blog via: sudo vim /etc/uwsgi/apps-available/hairycode.ini

     [uwsgi]
     # Variables
     base = /crowd
     app = simple
     # Generic Config
     plugins = http,python
     home = %(base)/venv
     pythonpath = %(base)
     socket = /var/www/run/%n.sock
     module = %(app)
     callable = app
     logto = /var/log/uwsgi/%n.log
    
  14. Link configuration file to the enabled sites folder:

     sudo ln -s /etc/uwsgi/apps-available/hairycode.ini /etc/uwsgi/apps-enabled/hairycode.ini
    
  15. Configure nginx to serve up the blog via: sudo vim /etc/nginx/conf.d/default.conf

     server {
         listen 80;
         server_name crowd-scholar;
         server_name crowdscholar.net;
         root /crowd-scholar;
    
         location /static/ {
             alias /crowd-scholar/static/;
             expires 30d;
             access_log off;
         }
    
         location / {
             include uwsgi_params;
             uwsgi_pass unix:/run/crowdscholar.sock;
         }
     }
    
  16. Launch ngnix:

     cd /crowd-scholar
     sudo service nginx start
     Note: Here is the problem location - states /var/lib/nginx/tmp/client_body failed to load.
    

Resources:

  1. Installing MongoDB on EC2
  2. Configuring multiple Flask sites with uWSGI and nginx on an Amazon EC2 Instance