Tuesday, December 17, 2013

Can't update wordpress on Ubuntu

I had a wordpress installation on an Ubuntu 13 machine but was unable to update the wordpress. I thought it was due to rwx permission problems but it turned out it was not.

The wordpress folder should be of group "www-data" if you're using apache. Apache by-default runs with www-data group and user. So if your wordpress folder does not belong to this group, apache can not copy the newer version in this folder so it says "I could not update."

I found the solution by following the inverse of the instructions here ( stylishjm at the bottom ).

She said we could change the user and group of apache to our own username but that cause some other problems since it caused:
 ... waiting /var/lock/apache2 already exists but is not a directory owned by "username"

So instead of changing apache's username, I changed the group of wordpress folder:

chgrp -R www-data wordpress

Hope that helps.

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_ENGINE = 'redis_sessions.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.


  <property name="my.local.maven"


    <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.


Friday, June 28, 2013

Adding external libraries (jar files) to an ivy project (Nutch)

I wanted to use my Apache Giraph fork in Apache Nutch 2.1. The Problem was that Giraph used mvn and Nutch used ivy. Although Nutch could fetch jar files from maven repositories, my customized jars are in none of those repos. So I had to find a way to use my local jars.

Add the following line to ivysettings.xml of Nutch:
<module organisation="org.apache.giraph" name=".*" resolver="internal"/>

Add the following lines to ivy.xml of Nutch:
    <dependency org="org.apache.giraph" name="giraph" 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.giraph" name="giraph-examples" rev="1.1.0-SNAPSHOT"
      conf="*->default" />

Now create put jar files under .ivy2 subfolders:


Now when you run ant runtime and start a crawling, you will see that your local jar files are binded to your app:
SLF4J: Found binding in [jar:file:/home/emre/tmp/apache-nutch-2.1/runtime/local/lib/giraph-1.1.0-SNAPSHOT.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/emre/tmp/apache-nutch-2.1/runtime/local/lib/giraph-examples-1.1.0-SNAPSHOT.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/emre/tmp/apache-nutch-2.1/runtime/local/lib/giraph-hbase-1.1.0-SNAPSHOT.jar!/org/slf4j/impl/StaticLoggerBinder.class]

Thursday, June 20, 2013

Proxy Pattern for Extending non-extendable Python Class

graph_tool library is based on boost C++ library and provides Vertex class binding for Python. If we wanted to extend this Vertex class and add some attributes and methods, it wouldn't let us do that due to private constructor in C++ code.

RuntimeError: This class cannot be instantiated from Python

We can overcome this obstacle using Proxy pattern.

In the __getattr__ method, if the attribute(or function name) is not in the Subclass MyVertex, then it looks for attributes of Vertex object that is defined inside MyVertex.
 Here is the code:


Saturday, June 8, 2013

Latex and Bibtex

Using bibliography in latex might be headache sometimes. While I was writing my MSc thesis, I had taken some notes. I'd like to share them with you.

For a while, I was wondering what is the difference between bib and bbl. I learned that when you execute bibtex, bib files are converted to bbl.

Steps for running BIBTEX with LaTEX

1. Run LaTEX, which generates a list of \cite references in its auxiliary file, .aux.
2. Run BIBTEX, which reads the auxiliary file, looks up the references in a database
(one or more .bib files, and then writes a file (the .bbl file) containing the formatted
references according to the format specified in the style file (the .bst file). Warning
and error messages are written to the log file (the .blg file). It should be noted that
BIBTEX never reads the original LaTEX source file.
3. Run LaTEX again, which now reads the .bbl reference file.
4. Run LaTEX a third time, resolving all references.

Friday, May 31, 2013

File icon extensions (emblems) like of Dropbox

I have been wondering how Dropbox could put a icon on the file indicating it's synchronizing or synchronized. I digged its code and the files of gnome itself and found the way for that.

You can find some examples for these icons in /usr/share/icons/gnome/scalable/emblems/ which belongs to Gnome.

If you want to have a custom icon, then you can put your files in /usr/share/pixmaps

An example filename: emblem-my-synced-symbolic.svg

I had the impression that the file name should start with emblem but I'm not really sure.

After having your file in /usr/share/pixmaps, you can activate this icon for a file:

gvfs-set-attribute -t stringv /home/emre/sample.txt metadata::emblems 'my-synced-symbolic'

Now your file has the svg icon on it.

Friday, May 24, 2013

Fix for SuspiciousOperation: Invalid HTTP_HOST header

With Django 1.5, HTTP_HOST header filter is applied to the requests.  If the HTTP_HOST header is not among the ALLOWED_HOSTS list in the settings.py, an error is raised, saying this is a suspicious operation.

Let me give an example. Someone (who is not Google), is trying to reach my IP address with the HTTP_HOST www.google.com as if I'm hosting the google.com homepage.:

SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): www.google.com

This happens frequently, leading to emails sent to the admins which is annoying. To overcome this, you can add the IP Address of the requester to /etc/hosts.deny file.

Indeed I thought I could put a hostname filter in nginx configuration, especially in the listen part but my configuration did not have an effect.

The attacker tries to exploit a vulnerability and performs a scan over the web. They seem to be from Vietnam.

Wednesday, April 3, 2013

Incorporating request variable in django forms dynamically

In some cases, you may need to change form contents according to the user's session information. Just like I'd like to display the content for a specific DJANGO_SITE in one of the fields: categories. User should see only the categories of the site he belongs to. There should be a filtering but we can't use request data in Forms! One way is like I explained before. But this requires the form to be called with additional parameters. What if you can't modify those parameters because the view calling it is a third-party module? Then you may wrap the form in a function and create a new form definition according to the request with the session info in it:

def get_edit_profile_form(request):
    class EditProfileForm(BaseEdit):
        def __init__(self, *args, **kw):
            super(BaseEdit, self).__init__(*args, **kw)
            self.fields['categories'].queryset = Category.objects.filter(site=request.site)

def my_profile_edit(request, username):
    # do anything before performing the original profile edit
    response = userena_views.profile_edit(request, username=username, edit_profile_form=get_edit_profile_form(request))
    # do anything after performing the original profile edit.
    return response

This way we don't have to modify userena which is a third party module.

Tuesday, January 22, 2013

Designing Django, PHP and HTML Compatible

You may want to design a web site and sell or distribute it. Headers, navigation bars and footers of the pages will be the same for most of the pages. So you may want to re-use these fragments in each page so that when you make a change in the design, it's reflected in all of those pages. Template engines come in handy in these cases. I've been using Django for my projects. Django uses template engine similar to jinja2.

But when I wanted to create a static site, I used Hyde to compile the fragments into complete html files. But this required re-compilation of all the project whenever you make a change in one single file. Perhaps compiling only the changed is possible with some kind of console arguments. Then I thought that'd be nice if there were a hyde server like django development server which recognizes which file has changed and only compiles it and serves it on HTTP. Then I found Hyde web server. I was really excited. Then I thought if I wanted to use this design on a PHP-supporting server, what would I do? I found Twig for this purpose. Wonderful! These are excellent resources for a web designer.

Inmotion Hosting is excellent

I have been using Inmotion hosting for one year and have been very satisfied with their high quality service (speed, robustness, features, unlimited space) and fast, friendly and polite support staff. Their network access is very fast and I haven't noticed any downtime. I'd recommend inmotion for whoever wants a robust and fast hosting for PHP. Yes, it's a bit expensive for a simple web site but for high-performance sites, it's a good candidate. Another drawback is that they didn't notice that my hosting is about to expire and renewed my hosting automatically. I wish I had been noticed that. Since my site hosted there was a simple static site, I was going to move it to another cheaper solution. When I applied for cancellation they were really polite and helpful. They canceled my account with no problems and gave a refund. After the cancellation form sent, they showed me a list of alternative hosting providers. I admired that!. So I wanted to write this blog entry to show my satisfaction.

Monday, January 14, 2013

Using Sendgrid on Heroku with Django

If you'd like to send emails easily within your Django application on Heroku, you may use Sendgrid.Here are the steps: 1) Activate Sendgrid: heroku addons:add sendgrid:starter. 2) Your credentials will be listed when you type "heroku config" in SENDGRID_PASSWORD and SENDGRID_USERNAME sections. 2) Do the following configurations: heroku config:add EMAIL_HOST='smtp.sendgrid.net' heroku config:add EMAIL_HOST_USER='username_provided_above' heroku config:add EMAIL_HOST_PASSWORD='password_provided_above' and set DEFAULT_FROM_EMAIL as something like 'info@yoursite.com'. These can be done in either in settings.py or as heroku config (like above). These should be sufficient for email sending.