Saturday, November 23, 2013

LAMP: Creating virtual hosts and resolving 500 Internal server error

This guide expects, that you already have web server on your machine.

1. Copy file /etc/apache2/sites-available/default and rename it to your host name. (for example testweb.net. Also your web application must be in /var/www/testweb.net/ directory.) Dont forget to add permissions to your web application directory using
sudo chmod -R 755 /var/www/testweb.net/ For development you can use 777, but it is still not the best practice.
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/testweb.net

2. Configure testweb.net file
gksudo gedit /etc/apache2/sites-available/testweb.net

This command will open testweb.net file in gedit text editor.

ServerName testweb.net
<VirtualHost *:80>
 ServerAdmin admin@mail.com
 ServerName testweb.net
 ServerAlias www.testweb.net
 [...]

DocumentRoot /var/www/testweb.net
I'm using my own hosts configuration file:
    ServerNametestweb.net
    ServerAlias www.testweb.net

    DocumentRoot /var/www/testweb.net
    <Directory /var/www/testweb.net>
        # enable the .htaccess rewrites
        AllowOverride All
        Order allow,deny
        Allow from All
    </Directory>

    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>

3. Activate your host

sudo a2ensite testweb.net

4. Edit /etc/hosts
gksudo gedit /etc/hosts
Add this line to hosts file:

127.0.0.1    testweb.net

5. Now you need to restart your apache service.
sudo service apache2 restart
Voila, your virtual host is now available!

What should you do, if you have 500 Internal Server Error? 

This problem might be caused by .htaccess file in your project root dir. First of all, check your logs (/var/log/apache2/) and try to access your site by this link: localhost/testweb.net. But for me it was not enough, so I found this solution (not sure what this command do, so run it on your own risk T_T)

sudo a2enmod rewrite
sudo service apache2 restart

Here you can read about 500 error solving: http://askubuntu.com/questions/148246/apache2-htaccess

This resource was really helpful: https://www.digitalocean.com/community/articles/how-to-set-up-apache-virtual-hosts-on-ubuntu-12-04-lts

P.S. YEAH, I know, there are LOTS of english mistakes, sorry for that :(

Thursday, November 14, 2013

SVN resolving problem: "containing working copy admin area is missing"

For an instance, we're having this error: "some/folder/.svn containing working copy admin area is missing".

There are lots of solutions here: http://stackoverflow.com/questions/1394076/how-to-fix-containing-working-copy-admin-area-is-missing-in-svn, but here's the one, that worked for me (not the most elegant, I suppose, but it worked):

1. Remove the conflicted folder (if there are some uncommited files, we need to back them up)
cd /your/project/root
sudo rm -rf some/folder
svn up
2. Commit your changes!

Wednesday, November 13, 2013

Setting up permissions on Ubuntu for /var/www (web server)

In ubuntu all web projects are in /var/www folder, so you need to set up permissions on writing files, as by default this folder belongs to root user.

1. First of all you need to ensure, that your current user belongs to www-data group. Adding user to www-data group:

sudo adduser username www-data

You can find your username by typing this command:

whoami

2. Next we need to change /var/www folder owner and owner group:

sudo chown username:www-data -R /var/www

3. Next we need to change /var/www permissions to 755 (777 not recommended due security reasons)

sudo chmod 755 -R /var/www
sudo chmod g+s -R /var/www

Information from: http://askubuntu.com/questions/162866/correct-permissions-for-var-www-and-wordpress

Wednesday, November 6, 2013

Ruby: exceptions

raise "Exception message" // this will throw RuntimeError exception with message "Exception message"

begin
  10/0 // This will raise an DivisionByZero exception
rescue DivisionByZero
  puts "Devision by zero exception"
end
Creating custom exception:
class CustomException > StandardError
end

Ruby: OOP (privacy)

Methods are public by default in Ruby.

class Test

  def public_m
  end

  private
  def private_m
  end

  // Again private method, because all methods after private keyword will be private
  def private_m_2
  end

end
Modules are containers for methods and constants.

module MyModule
  // Call it: MyModule::MOD_CONSTANT
  MOD_CONSTANT = "Module constat"

  // Call it: MyModule.my_method
  def my_method
  end
end
Require module by typing require 'MyModule', so it will be available in whole file (Module.my_method)

Include module to class, so it will be available inside class and all the methods can be called without module name:


class SimpleClass
  include MyModule // Without '
  def m
    my_method
  end
end
Infromation from: www.codeacademy.com

Ruby: OOP (Basic)

class Person
  // This is a variable, that belongs to class
  // Like static variable in PHP
  @@class_variable = 0

  // This is a global variable
  $global_var

  // This is a constructor
  def initialize(name)
    // @name - class variable, name - local variable
    // @name will be available in whole class
    @name = name
    @class_variable += 1
  end

  // This is method, that belongs to class not to instance!
  // Like static method in PHP
  def self.class_method
  end
end

// This will create 2 instances of a Person class
andy = Person.new("Andy")
marrel = Person.new("Marrel")
// @@class_variable on this step will be equals to 2

Inheritance
// Class andy inherits all from class Person
class Andy < Person
  def initialize(name)
    // This will call parent initialize method
    super(name)
  end
end 

Infromation from: www.codeacademy.com 

Ruby: procs and lambdas

Procs:
Sometimes you need to pass block to your method. The problem is that you cant save block to variable, because it is not an object. If you need to pass one block to many methods, there's no need to the same block over and over again, because you can make Proc. Proc saves your block to variable, and gives you an opportunity to manipulate with it like with object.

proc_name = Proc.new { your block here }

func1(&proc_name)
func2 &proc_name
You must pass proc to method with & symbol.

Also you can convert symbols to procs using magic & symbol:
(function .map and .collect runs through all the elements in array)
number = [0,1,2]
number.map(&:to_s) // This will convert 0,1,2 to strings
Lambda:Lambdas are almost the same as procs, with the exception of syntax and few behavioral features.

Proc.new { puts "Hello" }
same as
lambda { puts "Hello!" }
Differencies between lambdas and procs:
1. Lambda checks the number of arguments passed to it, while a proc does not. Lambda will throw an error, proc will assign nil to all unassigned arguments.
2.When a lambda returns, it passes control back to the calling method; when a proc returns, it returns immediately, whithout passing control back.

// Outputs "Hello world!"
def A
  h = Proc.new { return "Hello world!" }
  h.call
  return "Hello galaxy!"
end

// Outputs "Hello galaxy!"
def B
  h = lambda { return "Hello world!" }
  h.call
  return "Hello galaxy!"
end

Infromation from: www.codeacademy.com

Tuesday, November 5, 2013

Ruby interesting things

puts "Hello" if statement == true // This construction works fine, and puts "Hello" if statement equals to true, but construction if statement == true puts "Hello" will cause an error.

26.respond_to?(:next)   // .next() method will return next value to 26. This contruction will return true, because method next can be applied to 26 (.next method will return 27, by the way)

5.times {} // Will run block 5 times

5.upto(10) { |num| puts num } // Will return 5 6 7 8 9 10 on each line (.downto(n) works other way round)

[1,2,3]  << 4 // Will return [1,2,3,4] also works on strings "Andy" << " Marrel" -> "Andy Marrel"

"String " + 26 // Will cause error, because you neet to cast 26 to string, but you can apply string interpolation, so the string will be ok -> "String #{26}"

Information from: www.codeacademy.com