Tuesday, May 15, 2018

Python f strings

Tuesday, April 10, 2018

Apache2 configuration to run both django and flask in one server.


# Under /etc/apache2/sites-available/ add a file say multi_sites.conf with the following contents 
project name of django is DP and project name of flask is FP:

<VirtualHost *>
   Alias /static /../DP/static
   <Directory /.../DP/static>
        Require all granted
   </Directory>
   <Directory  /../DP/DP>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
   WSGIDaemonProcess  python-path=/../DP/
   WSGIProcessGroup DP
   WSGIScriptAlias / /../DP/DP/wsgi.py
</VirtualHost>

<VirtualHost *>
    Alias /static /../FP/static
   <Directory /../FP/static>
        Require all granted
   </Directory>
   <Directory /../FP/>
       <Files wsgi.py>
           Require all granted
       </Files>
   </Directory>
   WSGIDaemonProcess ocean python-path=/../FP/
   WSGIScriptAlias / /../FP/wsgi.py
</VirtualHost>

3. Enable sites: 
sudo a2ensite multi_sites.conf
sudo a2dissite 000-default.conf
sudo apache2ctl restart

Saturday, March 10, 2018

Docker-compose

#Create a docker compose NAME.yml file like below:
version: "3"
services:
  web:
    # replace NAME:run with your name and image details
    image: NAME:run
    deploy:
      replicas: 1
      resources:
        limits:
          cpus: "1"
          memory: 1000M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet
networks:
  webnet:

#Then run the following command
docker-compose -f NAME.yml up

Friday, February 23, 2018

Tab completion in linux terminal

For tab completion, install two packages called bash-completion & bash-completion-extras
RedHat/CentOS: sudo yum install bash-completion bash-completion-extras
Ubuntu/Debian: sudo apt install
bash-completion bash-completion-extras

Tuesday, February 6, 2018

To find the time response of a website using curl

To find a website response time and other statistics, use curl command in linux with following options:

curl -s -w '\n Statistics for :%{url_effective}\n Remote IP:\t\t%{remote_ip}\n Name Lookup Time:\t%{time_namelookup}\n TCP Connect Time:\t%{time_connect}\n SSL Connect Time:\t%{time_appconnect}\n Redirect Time:\t\t%{time_redirect}\n Pre-transfer Time:\t%{time_pretransfer}\n Start-transfer Time:\t%{time_starttransfer}\n Total Time:\t\t%{time_total}\n HTTP Code:\t\t%{http_code}\n' -o /dev/null https://www.amazon.com

 Statistics for :  https://www.amazon.com/
 Remote IP:               54.192.139.249
 Name Lookup Time: 0.253042
 TCP Connect Time: 0.326341
 SSL Connect Time: 0.498637
 Redirect Time: 0.000000
 Pre-transfer Time:     0.498744
 Start-transfer Time: 0.718770
 Total Time:                1.270710

Sunday, February 4, 2018

Docker commands

#Build: To build, create a Dockerfile in a directory and inside the directory run the following command: (Change NAME with your image name)
docker build -t NAME .

#List Docker Images:
docker images

#Remove Docker Image
docker image rm -f ID

#Run: To run the container image called NAME with following options:
1. Map the host port 80 with container port 80
2. Limit memory to 1000MB
3. Limit CPU = 1
4. Assign name = NAME
--use the following command:
docker run --rm -it -p 80:80 -m 1000m --cpus=1 --name=NAME NAME

#List Docker processes
docker ps

#See a live stream of container(s) resource usage statistics (like top)
docker container stats

#Display the running processes of a container (one shot)
docker container top NAME

#Check iptables for port mapping:
sudo iptables -t nat -L -n

#Commit Container or take a snapshot while running:
docker commit NAME NAME:run


Dockerfile

#Create a Dockerfile in the project directory. Following is a sample Dockerfile to install ubuntu with postgres and apache

FROM ubuntu:rolling
  RUN apt update 
  RUN apt install -y vim python3-pip apache2 libapache2-mod-wsgi-py3 postgresql postgresql-contrib postgresql-server-dev-9.6 openssh-server sudo curl iproute
  RUN pip3 install --upgrade pip
  RUN pip3 install Django django-mathfilters lxml psycopg2-binary requests
  ADD install.sh /
  ENTRYPOINT ["/bin/bash", "-x", "install.sh", "arg1"]