Skip to content

Installations

anjalysuresh edited this page Feb 8, 2018 · 38 revisions

Installations

Installing the Server Dependencies for Collaborative Communities project

  1. First thing let’s upgrade the packages:
   sudo apt-get update
   sudo apt-get -y upgrade
  1. Install nginx
   sudo apt-get -y install nginx
  1. Install supervisor

    Supervisor will start the application server and manage it in case of server crash or restart:

   sudo apt-get -y install supervisor

Enable and start the Supervisor:

   sudo systemctl enable supervisor
   sudo systemctl start supervisor
  1. Install mysql server
  sudo apt-get install mysql-server
  After installing mysql  
  Access mysql using mysql -u root -p enter the password

  Create the database collaboration.

   mysql> create database collaboration;
 
   mysql> exit;
   
  1. Python Virtualenv

    The Django application will be deployed inside a Python Virtualenv,

   sudo apt-get install python3 python3-pip virtualenvwrapper
  1. Create a new user
   sudo adduser collaboration
  1. Add the user to the list of sudoers:
   sudo adduser collaboration sudo
  1. su - collaboration

  2. Configure the Python3 Virtualenv

    mkvirtualenv --python=/usr/bin/python3 tester
  1. Clone the code repository to virtual environment ,the command prompt will show like below

    (tester) collaboration@edx:~/tester$

    Check the virtual environment directory created on your system .Go to the directory 
    like : cd /tester
    Run the command workon tester to work on this environment. 
    git clone https://github.com/fresearchgroup/Collaboration-System.git
  1. cd project directory
    (tester) collaboration@edx:~/tester$ cd Collaboration-System
    pip3 install -r requirements.txt
  1. Create the database settings as per project documentation, install django and other modules using the project documentation.(step 1-4 only)
     sudo vi etc/mysql/my.cnf
     
  1. Migrate the database
    python3 manage.py migrate
  1. Collect the static files
    Create a static directory in /home/collaboration 
    mkdir /home/collaboration/static
    Add STATIC_ROOT="/home/collaboration/static/" in settings.py of Collaboration System.  
    python3 manage.py collectstatic
    
  1. Add vm's ip address to allowed hosts in the settings.py of project,do the step 8 of project documentation.

  2. Check whether the project is running at ipaddress:8000 of vm

  3. Configure gunicorn

    First install Gunicorn inside the virtualenv:

    pip3 install gunicorn

    Create a file named gunicorn_start inside the bin/ folder:

    cd /home/collaboration/tester/

    sudo vi bin/gunicorn_start

    #! bin/bash
    
    NAME="Collaboration-System"
    DIR=/home/collaboration/tester/Collaboration-System
    USER=collaboration
    GROUP=collaboration
    WORKERS=3
    BIND=unix:/home/collaboration/tester/run/gunicorn.sock
    DJANGO_SETTINGS_MODULE=CollaborationSystem.settings
    DJANGO_WSGI_MODULE=CollaborationSystem.wsgi
    LOG_LEVEL=error
    
    cd $DIR
    source ../bin/activate
    
    export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
    export PYTHONPATH=$PYTHONPATH
    echo $PYTHONPATH
    
    exec ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
    --name $NAME \
    --workers $WORKERS \
    --user=$USER \
    --group=$GROUP \
    --bind=$BIND \
    --log-level=$LOG_LEVEL \
    --log-file=-
    

Make the gunicorn_start file is executable:

    chmod u+x bin/gunicorn_start

Create a directory named run in /home/collaboration/tester for the unix socket file:

    mkdir run

  1. Configure Supervisor

    Now what we want to do is configure Supervisor to take care of running the gunicorn server.

    First let’s create a folder named logs inside the virtualenv:

    mkdir logs in /home/collaboration/tester

    Create a file to be used to log the application errors:

    touch logs/gunicorn-error.log

    Create a new Supervisor configuration file:

    Collaboration-System.conf in /etc/supervisor/conf.d/

    cd /etc/supervisor/conf.d/

    vi Collaboration-System.conf

    [program:Collaboration-System]
    command= /home/collaboration/tester/bin/gunicorn_start
    user=root
    autostart=true
    autorestart=true

    redirect_stderr=true
    stdout_logfile=/home/collaboration/tester/logs/gunicorn-error.log

Reread Supervisor configuration files and make the new program available:

    sudo supervisorctl reread
    sudo supervisorctl update

Check the status:

    sudo supervisorctl status Collaboration-System

It should give like this

     Collaboration-System             RUNNING   pid 5200, uptime 0:00:09

Now you can control your application using Supervisor. If you want to update the source code of your application with a new version, you can pull the code from GitHub and then restart the process:

     sudo supervisorctl restart Collaboration-System
  1. Configure NGINX

    Add a new configuration file named Collaboration System inside /etc/nginx/sites-available/:

     sudo vim /etc/nginx/sites-available/Collaboration-System
    upstream app_server {
    server unix:/home/collaboration/tester/run/gunicorn.sock fail_timeout=0;
    }
    server {
    listen 80;
    # add here the ip address of your server
    # or a domain pointing to that ip (like example.com or www.example.com)
    server_name 10.129.26.148;

    keepalive_timeout 5;
    client_max_body_size 4G;

    access_log /home/collaboration/tester/logs/nginx-access.log;
    error_log /home/collaboration/tester/logs/nginx-error.log;

    location /static/ {
        alias /home/collaboration/tester/Collaboration-System/static/;
    }

    location /media/ {
       alias /home/collaboration/tester/Collaboration-System/media/;
    }

    # checks for static file, if not found proxy to app
    location / {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app_server;
    }
}
  1. Create a symbolic link to the sites-enabled dir:
    sudo ln -s /etc/nginx/sites-available/Collaboration-System /etc/nginx/sites-enabled/Collaboration-System
  1. Remove NGINX default website:
    sudo rm /etc/nginx/sites-enabled/default
  1. Restart NGINX:
    sudo service nginx restart
  1. Now open the browser and check the application is working at 10.129.26.148.

  2. A final test is to reboot the machine and see if everything restarts automatically:

    sudo reboot
  1. Steps to be followed to update the application
     ssh [email protected]

     cd tester
     
     workon tester
      
     cd Collaboration-System

     git pull origin master

     python3 manage.py collectstatic

     python3 manage.py migrate

     sudo supervisorctl restart Collaboration-System

     exit