Sunday, December 8, 2013

Django Redis backend for sessions on Heroku

Session queries take significant time at each view request. To bypass PostgreSQL and obtain the session information, you can use Redis.

On Heroku, add RedisCloud addon to your app. Then install django-redis-sessions and  add the following lines to your production settings file.

########## REDIS SESSION ###########
from os import environ

redis_url = environ.get("REDISCLOUD_URL")
credential_part, host_part = redis_url.split("//")[1].split("@")
rediscloud, rpassword = credential_part.split(":")
rhost, rport = host_part.split(":")

SESSION_REDIS_PASSWORD = rpassword
SESSION_REDIS_HOST = rhost
SESSION_REDIS_PORT = int(rport)
SESSION_ENGINE = 'redis_sessions.session'
SESSION_REDIS_DB = 0
SESSION_REDIS_PREFIX = 'session'
######## END REDIS SESSION #########

Thursday, October 10, 2013

Using PositiveSSL with nginx

Make sure you have nginx with SSL support:

sudo apt-get install nginx-full

Create a key:
sudo mkdir -p /etc/nginx/ssl
cd /etc/nginx/ssl
sudo openssl genrsa -des3 -out domain.key 2048
sudo openssl req -new -key domain.key -out domain.csr


Create certificate: a combined pem file
Put the zip file Namecheap has sent you via email on server, extract them.

cat www_mydomain_com.crt >> www_mydomain_com.pem
cat PositiveSSLCA2.crt >> www_mydomain_com.pem
cat AddTrustExternalCARoot.crt >> www_mydomain_com.pem
service nginx restart

Edit /etc/nginx/sites-available/default:
# Default server for non-domain requests
server {
       return 404;
}

upstream django {
        server localhost:8000 fail_timeout=10;
}

server {
  listen 80;
  server_name www.mydomain.com mydomain.com uk.mydomain.com;
  return 301 https://$host$request_uri;
}


server {
    listen 443;
    server_name www.mydomain.com mydomain.com uk.mydomain.com;
    root /home/user/projects/myproject;
    ssl on;
    ssl_certificate /etc/nginx/ssl/www_mydomain_com.pem;
    ssl_certificate_key /etc/nginx/ssl/domain.key;

    ...
}

The first server lets wrong domain requests to get 404, not 500.

The second is the django server

The third is the HTTP to HTTPS redirection

The fourth is the ssl settings for the main project.

Saturday, October 5, 2013

File transfer from Android to Linux

It's problematic to perform file transfers from Android via MTP protocol to Linux. I was desperate until today when I found the solution.

cd adt-bundle-linux-x86_64/sdk/platform-tool
 
This will fetch all the files from internal memory:
./adb pull /storage/sdcard0/

and this will fetch all the files from external sd card:
./adb pull /sotrage/extSdCard/

Thursday, July 18, 2013

Avoiding fake server requests in nginx

I have been having requests to my server with HTTP_HOST header gameframe.net or server5.cyberpods.net which are not related to me. They were causing error mails sent to me by my Django site since these domains were not listed in my ALLOWED_HOSTS list. I was tired of having error messages every day so after some research  I found the solution.

I told my nginx server to listen only my own domains but it was listening to other domains as well. I added the following block to the top of my config file

server {
        return 404;
}

before the original server settings:

server {
       listen 80;
       server_name my.domain.com myother.domain.com;
       ...
}

This way any domain that is not in my list gets 404 by default. Hope it helps!


Monday, July 8, 2013

Adding local maven dependencies to Ivy

If you want to use your local maven packages in your app configured with ivy, you should add some lines to ivy.xml and ivysettings.xml. Below is an example.

ivysettings.xml

  <property name="my.local.maven"
    value="file:///${user.home}/.m2/repository/"
    override="false"/>   

ivy.xml

    <dependency org="org.apache.giraph" name="giraph-core" rev="1.1.0-SNAPSHOT" conf="*->default"/>
    <dependency org="org.apache.giraph" name="giraph-examples" rev="1.1.0-SNAPSHOT" conf="*->default"/>
    <dependency org="org.apache.giraph" name="giraph-hbase" rev="1.1.0-SNAPSHOT" conf="*->default"/>
    <dependency org="org.apache.hbase" name="hbase" rev="0.90.5" conf="*->default"/>

Thursday, July 4, 2013

Adding maven dependencies into the target jar file

I wanted to add hbase and giraph-hbase dependencies to giraph-example module. I realized that I should specify scope for each to get the into the resulting giraph-example.jar.

    <dependency>
      <groupId>org.apache.giraph</groupId>
      <artifactId>giraph-hbase</artifactId>
      <version>1.1.0-SNAPSHOT</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase</artifactId>
      <version>0.90.5</version>
      <scope>compile</scope>
    </dependency>