Tag: Web

  • Running Django project on Apache production server with mod_wsgi

    django

    This is official tutorial. But it didn’t work for me for 100 %. But there is some useful information on wsgi and daemon process that should be created.

    There is a good tutorial, that I followed and it actually works. But I decided to write my own post with instructions for my personal project:

    First we enter the production server via SSL and create a virtual environment there:

    virtualenv env
    

    Then we activate it:

    source env/bin/activate
    

    Check the Django version. It must be the same as in your local server:

    python -c "import django; print(django.get_version())"
    

    Save somewhere a path to a Django project:

    /root/www/den.ladby.ru/public/common-app-server/app_server
    

    Your path will be different.

    This is an example of a sites-available/*.conf file setting to run Django with WSGI:
    Снимок экрана 2014-09-19 в 20.25.49

    Here app_server is the name of my Django project or you can name it a site. But in my case it is not site but server for a mobile application.

    Python and Django version on server and virtual environment may be different. You should do everything inside virtual environments with the same versions on local and production servers.

  • Installing Django on Mac

    django

    First you should install Python and Pip:

    $ brew install python
    

    Then you install virtual environments:

    $ sudo pip install virtualenv
    

    And activate one virtual environment:

    $ virtualenv my_virtualenv
    
    $ source my_virtualenv/bin/activate
    

    Then you install Django using pip in that virtual environment. Don’t use sudo, since if you use it, then you will install Django not in virtual environment but in common environment.

    $ pip install Django
    

    Now you can start creating Django projects using this tutorial.

    To look at a version of installed Django use this:

    $ python -c "import django; print(django.get_version())"
    

    To create a project:

    $ django-admin.py startproject mysite
    

    To activate database:

    $ python manage.py migrate
    

    To run a server:

    $ python manage.py runserver
    

    To open your first project on Django:

    http://localhost:8000/
    
  • Searching for the best technology for a mobile app backend development

    Client-server applications with a mobile app client are more interesting than mobile standalone applications. They offer a new type of features for users like daily updated content. That is why at some moment of my mobile developer career I decided to learn how to create a server side for my mobile applications myself. As it happens usually in IT, there were a lot of techologies and programming languages, that I could start to learn and use for achieve my goals. Of course, if I would stop on the first one, that I have tried, I would lose, because one can never find the best way without understanding what other ways give and without any broad enough knowledge. I think, it is a good idea to get familiar with minuses of each technology before starting to learn and use it.

    This is my story. I started with Java Servlets and I have tried also PHP and Python at the moment. Also I have read about Ruby On Rails and Node.JS. Finally, I stopped currently at using Python with Django.

    Between Java Servlets and Python I choose Python. Development on Python it is much faster than on Java Servlets. It has no complex and sensible configuration files. Python offers iterative development, you can just refresh a web page and see results instantly. With Java Servlets you have to wait some time every time you update something. Python is like scripting language, despite it is compiled to some kind of byte code. I also liked python for it’s clean syntax, despite I am friendly with Java too. Using HTML templates with Python is easier to me, than creating JSP pages with servlets. Just for information, I use Sublime Text 2 as a Python editor with a Lazy color theme. It is really convenient editor.

    I will not try PHP, because it is not valuable now, loses it’s popularity. It looks like Python offers almost the same features but even is better because of syntax, because it is compiled and because it offers full OOP support from the start and you can create all kind of applications including desktop applications with it. Also there are a lot of scientific and data analysis libraries on Python, that can be useful to create sophisticated back-end.

    I don’t like the idea of using generally considered to be slow Javascript at the backend in Node.js. Also Node.js needs it’s own HTTP server.

    What about Parse, QuickBlox and other ready for use backend services? I don’t want to use them, because I want to be able to create my own-backends with complete understanding of their code and work. These are good to create something easy and usual, but not something unique.

    Probably I will try also Ruby on Rails at some moment.

    But currently I am completely satisfied with Python. I want to get familiar with Django framework on Python too.

    There are also open-source backends like Baasbox: http://www.baasbox.com/. But I haven’t tried it yet. Looks like it is written on Java, because needs JVM.

    More articles on this topic:

    http://www.raywenderlich.com/20482/how-to-choose-the-best-backend-provider-for-your-ios-app-parse-vs-stackmob-vs-appcelerator-cloud-and-more

    http://www.developer-tech.com/news/2013/feb/22/exploring-mobile-app-backend-options/

  • Solving Google App Engine : 500 Server Error

    I started to learn Google App Engine. Everything was perfect – tutorials, easiness of testing on development server before I wanted to deploy it on server. I knew that I had to use Java 7 instead of Java 8, which is currently supported by App Engine, but didn’t know, where I should change it easily to make this happen, since I used Maven.

    This error happened to me:
    Server Error The server encountered an error and could not complete your request. Please try again in 30 seconds.

    In errors of my app on App Engine Console I saw this:
    Uncaught exception from servlet java.lang.UnsupportedClassVersionError: org/apache/jsp/guestbook_jsp : Unsupported major.minor version 52.0 at com.go

    You can see logs this way:
    blogpost

    You can check your java version:

        $ java -version
    

    This is solution:

    Then go to profile:

        $ vim ~/.bash_profile
    

    And change your JAVA_HOME variable:

        export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
    

    Update that variable:

        $ source ~/.bash_profile
    

    And now rebuild your AppEngine project and redeploy it on server:

        $ mvn clean install
        $ cd guestbook-ear
        $ mvn appengine:update
    

    Now check your application on browser: https://your_app_id.appspot.com/

  • How to add www?

    Let’s say, you have a website, that is opened by URL: http://ladby.ru. And you want to be able to open it by http://www.ladby.ru also. What should you do? I will describe it now.

    In this case www is a subdomain for your website. It is useful usually if you want to have also ftp.ladby.ru for instance. Also it can be useful to redirect from www.ladby.ru so that some users that type your website URL with www will not have any problems accessing it. Here is more information on what is the purpose of the www subdomain.

    To achieve this, you should create a CNAME record in your DNS Zone file. Basicly, CNAME record tells where one should be redirected.

    I have a record:

    @                        IN A       78.47.91.3
    

    Here @ sign means basic address: ladby.ru in this case. So when somebody wants to open ladby.ru, he is redirected to the 78.47.91.3 IP address. A-record returns a 32-bit IPv4 address, most commonly used to map hostnames to an IP address of the host.

    Also I have added an important record, which is the core of the trick:

    www                      IN CNAME   @
    

    So, when somebody wants to open www subdomain, he is redirected to basic address or ladby.ru.

    In my case the final result looks like this:

    $TTL 86400
    @   IN SOA ns1.first-ns.de. postmaster.robot.first-ns.de. (
        2014062605   ; serial
        14400        ; refresh
        1800         ; retry
        604800       ; expire
        86400 )      ; minimum
     
    @                        IN NS      robotns3.second-ns.com.
    @                        IN NS      robotns2.second-ns.de.
    @                        IN NS      ns1.first-ns.de.
     
    @                        IN A       78.47.91.3
    localhost                IN A       127.0.0.1
    mail                     IN A       78.47.91.3
    ftp                      IN CNAME   www
    imap                     IN CNAME   www
    loopback                 IN CNAME   localhost
    pop                      IN CNAME   www
    relay                    IN CNAME   www
    smtp                     IN CNAME   www
    www                      IN CNAME   @
    @                        IN MX 10   mail
    

    Also, you should edit your Apache configuration file this way:

    # cd /etc/apache2/sites-available
    # vim ladby.conf
    
    ServerName www.ladby.ru
    

    It should not be like this:

    ServerName ladby.ru
    
  • Hosting multiple web sites on a single Hetzner VPS using Apache virtual hosts

    This article is for those, who want to get some understanding of a Web, VPS, Apache. And for those, who want to solve a problem of registering domain names outside Hetzner and hosting multiple web sites on a single Hetzner VPS using Apache virtual hosts, that I solved in some days with a help of lots of experienced people.

    Hetzner is a popular German hosting company. They provide, for instance, virtual private servers (VPS). Some hosting companies provide an easy and straightforward way to register any domain names and host them instantly. But Hetzner has some problems with this, since they register only .com, .org and similar domains and do not register .ru, for instance. Also they force you to buy a NameServer Robot for about 30 Euros, that will allow you to edit DNS entries. It is a little bit strange, however, I had to work with this hosting. So, here I will tell you, how I did solve my problems with hosting two different web sites with separate domain names in a Ru zone on a single VPS.

    I will describe here, what I did to register a .ru domain name and point it to a folder on the VPS. VPS was already serving for another web site and I had to create another site, which would be working on the same VPS but have a domain name like “http://ladby.ru“.

    It is important to understand the architecture of the Web. Domain name is a string like google.com, that corresponds to a single IP address. DNS are servers, that contain these entries of correspondence. They exchange between each other this info. When you register your domain name, you provide registrator with the information, on which DNS you will provide entries for your IP address. When browser tries to open google.com, it goes to a DNS and determines IP address, where it should ask for the site. On this IP address there is a web server like Apache or NGinx, that handles further process.

    dns-rev-1

    First of all, I registered on a web site http://2domains.ru to register my domain name “http://ladby.ru”, since Hetzner allows you, as I told, to register only .com domains and similar. Then, I registered there this domain name for 1 year and paid about 3$ for this. I had to fill a lot of information about myself including passport data, since Russian government wants to know everything about .ru domain name owners. Nevertheless, I had this domain name available for me in about 10 minutes. What this did allow to me is the possibility to edit DNS for this domain name. What does a DNS (domain name system)? It contains data about correspondence between domain names and IP addresses.

    So, the next step was to determine DNS names and their IP addresses on a Hetzner DNS Robot. I just created a new DNS entry using Robot admin panel and got a list of 3 DNS. Then, I determined their IP address using web tools found in Google and filled data on 2domains.ru about these DNS. This is my filled form, but be aware that you will have absolutely different DNS at Hetzner – you should not copy this, you should take your own addresses from your Robot admin panel.

    Снимок экрана 2014-06-24 в 16.38.52

    The last thing I had to do is to setup Apache virtual hosts to make a single VPS host two web sites. For this purpose, I created a new .conf file in Apache2 folder and a link to it.

    Virtual-Host-Multiple-Websites-Ubuntu-13.10-and-Rackspace-Cloud-Server

    I did access to the VPS using SSH:

    ~: $ ssh root@lovecakes.ru
    root@lovecakes.ru's password: 
    Welcome to Ubuntu 13.10 (GNU/Linux 3.11.0-15-generic x86_64)
    

    To create conf files in a proper folder I used this snippet:

    # cd /etc/apache2/sites-available
    

    Apache works this way: it looks at all *.conf files in this folder and uses them to point corresponding web addresses to corresponding folders, where your index.php or index.html is located.

    To edit conf file I used Vim:

    # vim ladby.conf
    

    To reload Apache after editing conf files I used this snippet:

    # sudo /etc/init.d/apache2 reload
    

    This is my conf file, it can be used as a template:

     11   ServerName ladby.ru
     12   ServerAdmin anna@ladby.ru
     13   UseCanonicalName Off
     14   VirtualDocumentRoot /var/www/ladby.ru/public
     15   LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
     18   
     19     Options FollowSymLinks MultiViews
     20     AllowOverride all
     21   
     22   
     23     Options Indexes FollowSymLinks MultiViews
     24     AllowOverride all
     25     Order allow,deny
     26     allow from all 
     27   
     28   ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
     29   
     30     AllowOverride all
     31     Options +ExecCGI +SymLinksIfOwnerMatch
     32     Order allow,deny
     33     Allow from all 
     34   
    

    Notice that UseCanonicalName should be Off.

    To create a link to a conf file I used this snippet:

    # cd /etc/apache2/sites-enabled
    # ln -s ../sites-available/ladby.conf  ladby.conf
    

    Notice, that a web site is located in a folder /var/www/ladby.ru/public, it is opened also using a proper domain name that I want: http://ladby.ru.

    It is important to notice, that a configuration file for Apache should have .conf extension. Also, you have to point ServerName in each config file in this folder, because if you don’t do it, your server will not see your second web site. VirtualDocumentRoot must be correct also.

    So, finally what is the result?
    1. http://ladby.ru and

    This is the second site, that was hosted on the same VPS:
    2. http://lovecakes.ru