Apache

Last class on our Linux class we installed Apache (the most popular web server), which is an amazing open source project. Why it is great? Well first cost is often a factor in deciding which product to choose (Apache is a free software). Apache has a standard and straightforward configuration. Apache server logs are quite good to configure referrers, other important data and logs. Of course, it is up to you what server to use but Apache is a great web server and you should definitely try it.
So at home where I use my Lenovo laptop I decided to install Apache2.
This is the code that you should use if you want to install Apache:

sudo apt-get update

sudo apt-get install apache2

Then I tried to open in web browser localhost. And it worked. In the web browser you can find a message that says web server is working but no content has been added. Well let’s add some content. First you might want to try to go to localhost/public_html. The requested URL was not found. If you are wondering why, the best way to find out is to go into your logs.

So how to do it:

cd /var/log

ls (list all the logs, there you find apache2)

go to apache2 (cd apache2)

List all the file again and there you find: access.log error.log other_vhosts_access.log

So go into the access.log (command is less access.log)

Let’s look into my error log:

[Wed Sep 25 18:53:27 2013] [notice] Apache/2.2.22 (Ubuntu) configured — resuming normal operations
[Wed Sep 25 18:54:19 2013] [error] [client 127.0.0.1] File does not exist: /var/www/favicon.ico
[Wed Sep 25 18:54:19 2013] [error] [client 127.0.0.1] File does not exist: /var/www/favicon.ico
[Wed Sep 25 19:03:03 2013] [error] [client 127.0.0.1] File does not exist: /var/www/public_html
[Wed Sep 25 19:13:40 2013] [error] [client 127.0.0.1] File does not exist: /var/www/public_html
First of all you can see when the error is happened, that is exact time [Wed Sep 25 18:53:27 2013], then you can see what exactly happened – was it a error or a notice (as in first line when the download process was interrupted), then you can see the client that made the request (in the first case it is Apache and in the second case it is localhost) and then exactly what happened(error message). So the first error log message is about that the download process of Apache was stopped (I paused it on purpose to create some error logs), and the other 4 error logs are that when I tried to access localhost/public_html, it was trying to look in the wrong place in var, where the files don’t even exist for the public_html.

Well we can easily change that. First go to your home directory, that is cd. Then do this command sudo a2enmod userdir and then this one sudo service apache2 restart.

After that try accessing your localhost/public_html again. And go again into the error logs to see how it changed (Now you can try dong it yourself). Now you can see that there is no errors but still we can’t see anything. Why? Simple, we didn’t create any html files. Let’s create some. First create a public_html directory (mkdir public_html) There you can create some html files (nano htmlexample.txt), then run the command whoami. After that try running in your web browser localhost/ˇwhoami (here you put the output of whoami). Now we see the page that contains all that we created, if we would like to have an index file, so just create a regular html file and call it index.

I hope you enjoyed this tutorial!

This is going to be now my last tutorial about Linux, since currently I am doing some extra university studies, getting ready for my project management certification and some work. but don’t forget to stop by my blog sometime, I will update some interesting projects I am working on, or some pieces of code. Currently I am trying to master Pyhon and just bought Raspberry Pi to play around with it. And special thanks to Tero Karvinen for teaching me so much about Linux, check out his website here (he provides lots of info about Linux, Python, free software and also he currently published a great book).

Til next time!

Advertisements

Command Line in Linux – Part 2

Today we are gonna look into command line deeper, and will try to use it for admin purposes: downloading software, getting updates, etc. So how to become an admin while using command line? It is quite easy: use $sudo. But be careful! You don’t want to do a lot of things as $sudo, because that could harm your computer.

So now you know how to be an admin in your Linux and we are gonna learn now how to get updates through command line, it’s easy and simply awesome.

So first you should always make sure you are getting updates. In Linux you can do it only with one line in command line (Haha, Windows! Shame on you!). So in order to get all the updates of available packages try this command:

$sudo apt-get update (where sudo is an admin, apt-get is a great command for installing software and getting update).

So after we did that let’s try to search for the software you would like to install. You don’t need to be $sudo for that by the way, for example in the business environment if you are the only admin (no one will be able to get into sudo, they should know your password for that), but the employees can search for the software they would like to install. It is nice and saves time for you. So this is the command for it:

$apt-cache search blah blah blah

As for me I decided to search for some awesome internet browser… so this is my command: $apt-cache search browser |less. The reason I created a pipe (we will get into that later) less, this pipe is for displaying this command in a separate window, it is better if you know that there is gonna be loads of lines for this command (so now remember |less is for displaying in the separate window). Whoa, I get loads of results for the browser.. And then I decided since I am using a lot of command line to get a browser for the command line so for that I again gonna use pipe, only this time it is grep. Grep is a great pipe for searching… So my command looks like this $apt-cache search browser |grep command line |less… After this command I get only several lines of results.. After reading some descriptions I chose w3m. So in order to download it I use this command $sudo apt-get install w3m. So the download starts, there is usually one or two questions that you have to answer with y (for yes). So after it downloaded I decided to try out this interesting browser, for this there is a command $man, so my command is $man w3m. Whoa, I have an awesome browser in my command line! It is great, you can search anything on Google and do whatever you would like, I really like how this works and happy that I downloaded it. Then I am figuring out that I like my browsers to be minimal, so I decided to run this command $apt-cache search browser |grep minimal |less. My search gets me a very interesting browser called xxxterm.

It is a very interesting minimalist browser with security features designed-in, rather than through an add-on after-the-fact. It provides both persistent and per-session controls for scripts and cookies, making it easy to thwart tracking and scripting attacks. So again i am installing this package with the command $sudo apt-get install xxxterm. After installing I am trying it out I got to Browsers Menu and there I see xxxterm browser. Great browser! I really like it and will use a lot in future! Another great piece of software is InkScape.

I am using the same process as I did installing 2 packages before (try doing it! it is quite fun).

Now after installing I came up with the idea, what if some of my friends would like to switch to Linux as well as me, then I can help them with installing some awesome software (for example w3m, xxxterm and InkScape). We can create a small program for that. it is super easy! You should try it out too. So in order to install these 3 packages we used these commands:

$sudo apt-get install w3m

$sudo apt-get install xxxterm

$sudo apt-get install inkscape

Now in order to create a small program all you have to do is to save these commands as txt, so create a txt file for example $nano software.txt. And now copy and paste these commands in this text file. Well the program is ready! All you have to do is run this program. How to do it? I showed it in my first Linux post here. Easy way to do it is – $bash software.txt.

And magic, it worked! Easy, nice and simple! (btw since it is sudo don’t forget to use bash as a root as well)

Next thing I decided to do is to look at my logs (var/log) and analyze them (which is a nice practice):

Sep 20 19:56:09 gigity sudo:  gigity: TTY=pts/1 ; PWD=/home/gigity ; USER=root ; COMMAND=/usr/bin/apt-get install w3m
Sep 20 19:56:09 gigity sudo: pam_unix(sudo:session): session opened for user root by gigity(uid=0)
Sep 20 19:56:19 gigity sudo: pam_unix(sudo:session): session closed for user root

So let’s analyze firs line,  first comes time (Sep 20 19:56:09), then user (gigity), then sudo rights (sudo), after that it again repeats the user (gigity), then it shows the terminal (TTY=pts/1), then where the command was run (PWD=/home/gigity), then the user (USER=root), and then the command itself (COMMAND=/usr/bin/apt-get install w3m). It is very good for the future if you would have some errors concerning logs.

That is all for today! Hope you enjoyed!

Command Line in Linux

Command Line in Linux is an easy way to get around.

The most useful are:

ls List information about file(s)

pwd Print Working Directory

cd Change Directory

history Command History

less Display ouput one screen at a time

help Display help

…and so much more. If you would like to learn command line better, this is a great website for learning.

But I decided to try out interesting, more complecated cases and also pipes.

What is a pipe? A pipe is a form of redirection that is used in Linux and other Unix-like operating systems to send the output of one program to another program for further processing.

Redirection is the transferring of standard output to some other destination, such as another program, a file or a printer, instead of the display monitor (which is its default destination). Standard output, sometimes abbreviated stdout, is the destination of the output from command line (i.e., all-text mode) programs in Unix-like operating systems.

Pipes are used to create what can be visualized as a pipeline of commands, which is a temporary direct connection between two or more simple programs. This connection makes possible the performance of some highly specialized task that none of the constituent programs could perform by themselves. A command is merely an instruction provided by a user telling a computer to do something, such as launch a program. The command line programs that do the further processing are referred to as filters.

This direct connection between programs allows them to operate simultaneously and permits data to be transferred between them continuously rather than having to pass it through temporary text files or through the display screen and having to wait for one program to be completed before the next program begins.

Pipes can be used in threads and processes. The program below demonstrates how pipes can be used in processes. A new process can be created using the system call fork(). It returns two differnt values to the child and parent. The value 0 is returned to the child (new) process and the PID (Process ID) of the child is returned to the parent process. This is used to distinguish between the two processes. In the program given below, the child process waits for the user input and once an input is entered, it writes into the pipe. And the parent process reads from the pipe. (btw don’t try out this program at home, this is still not tested, this is to show how the pipes can be used)

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define MSGLEN  64

int main(){
    int     fd[2];
    pid_t pid;
    int     result;

    //Creating a pipe
    result = pipe (fd);
    if (result < 0) {
        //failure in creating a pipe
        perror("pipe");
        exit (1);
    }

    //Creating a child process
    pid = fork();
    if (pid < 0) {
         //failure in creating a child
         perror ("fork");
         exit(2);
    }

    if (pid == 0) {
        //Child process
         char message[MSGLEN];

          while(1) {
                    //Clearing the message
                    memset (message, 0, sizeof(message));
                    printf ("Enter a message: ");
                    scanf ("%s",message);

                    //Writing message to the pipe
                    write(fd[1], message, strlen(message));
            }
            exit (0);
    }
    else {
        //Parent Process
         char message[MSGLEN];

         while (1) {
                    //Clearing the message buffer
                    memset (message, 0, sizeof(message));

                    //Reading message from the pipe
                   read (fd[0], message, sizeof(message));
                    printf("Message entered %s\n",message);
            }

            exit(0);
     }
}
Pipes are one of the most commonly used mechanisms for IPC (inter process communication). IPC is the mechanism by which two or more processes communicate with each other. The commonly used IPC techniques includes shared memory, message queues, pipes and FIFOs (and sometimes even files). Pipes are helpful in the unidirectional form of communication between processes.The pipe() creates a pipe.Syntax
int pipe(fd[2])pipe() creates a pair of file descriptors, pointing to a pipe inode, and places them in the array pointed to by fd. fd[0] is for reading, fd[1] is for writing.Let’s see a simple program how to use piping. (this program is free to use, just create a text file and then execute it, how to do it I showed in my first post about Linux)
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MSG_LEN 64

int main(){

      int     result;
      int     fd[2];
      char    *message="Linux World!!!";
      char    recvd_msg[MSG_LEN];

      result = pipe (fd); //Creating a pipe fd[0] is for reading and fd[1] is for writing

      if (result < 0) {
          perror("pipe ");
          exit(1);
      }

      //writing the message into the pipe

      result=write(fd[1],message,strlen(message));
      if (result < 0) {
          perror("write");
          exit(2);
      }

      //Reading the message from the pipe

      result=read (fd[0],recvd_msg,MSG_LEN);

      if (result < 0) {
          perror("read");
          exit(3);
      }

      printf("%s\n",recvd_msg);
      return 0;
}

Output

Linux World!!!

As seen above , the pipe() system call creates a pipe. Now normal system calls like read() or write() can be used for reading and writing data to the pipe. As you have noticed the fd[0] can only be used for reading from the pipe and does not permit writing. Similarly fd[1] can only be used to perform writing to the pipe.

Why pipe is so awesome?
You are writing into the pipe and reading from it. That one can do even from a file.

Now let’s see another scenario. If we add more writes in the above program before reading anything and then start reading, you will find that the first read call will fetch you the first written message and so on.

So pipe() is helpful in Implementing a QUEUE strategy(First in First out)

FIFO: the message which was written first will be available for the first read, then the message which came second, for the second read.

Note: The first read itself can get all of the messages written by different write, if it specifies a large size in the size field (a size which can span the size of one or more writes ) as demonstrated here

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MSG_LEN 64

int main(){
   int     result;
   int     fd[2];
   char    message[MSG_LEN];
   char    recvd_msg[MSG_LEN];

   result = pipe (fd); //Creating a pipe//fd[0] is for reading and fd[1] is for writing

   if (result < 0) {
     perror("pipe ");
     exit(1);
   }

   strncpy(message,"Linux World!! ",MSG_LEN);

   result=write(fd[1],message,strlen(message));
   if (result < 0) {
      perror("write");
      exit(2);
   }

   strncpy(message,"Understanding ",MSG_LEN);

   result=write(fd[1],message,strlen(message));
   if (result < 0) {
      perror("write");
      exit(2);
  }

  strncpy(message,"Concepts of ",MSG_LEN);

  result=write(fd[1],message,strlen(message));
  if (result < 0) {
      perror("write");
      exit(2);
 }

 strncpy(message,"Piping ", MSG_LEN);
 result=write(fd[1],message,strlen(message));

 if (result < 0) {
     perror("write");
     exit(2);
 }

 result=read (fd[0],recvd_msg,MSG_LEN);
 if (result < 0) {
      perror("read");
      exit(3);
 }

 printf("%s\n",recvd_msg);
 return 0;

}

Output:

Linux World!! Understanding Concepts of Piping

In the above program, The read() call reads all the messages in a single stretch, because of the MSG_LEN(64) which is greater than sum of the size of the messages written in four different writes. The only thing that we must remember that the read() reads in the order of messages written.

Hopefully you enjoyed this post!

Why choose Linux? part 2

In my first post about Linux I wrote about installing Xubuntu version 13-04, a bit about command line and trying out machinotero. I decided to challenge myself and try to work only on Linux platform since I really like it and also love the fact that I don’t have to pay for the software licenses.

At school  we are using Windows 7, but always having your Xubuntu Live CD is quite handy. At home I am using Windows 7 as well that is having 45 inch HDTV as a computer monitor. I also have Lenovo laptop that has Linux Ubuntu and netbook ASUS that has Windows 7.

First I did was I installed Xubuntu on my main computer with 45 inch HDTV. I had no problems with resolution, everything works great and I really like the interface. But I miss my Visual Studio, Photoshop and Illustrator. Well luckily that is not a problem since there are lots of program alternatives that are free. Instead of Visual Studio I decided to try out gedit. It is the official text editor of the GNOME desktop environment. Gedit is a powerful general purpose text editor. It has full support for internationalized text, configurable syntax for various languages (C, C++, Java; HTML, XML, Python, Perl, etc.), editing files from remote locations, file reverting, text wrapping, line numbers, etc. And I must say I really like the interface – simple, easy to use and free from unnecessary boxes (as in Visual Studio for example) And by the way, gedit also features a flexible plugin system which can be used to dynamically ass new advanced features to gedit itself. In gedit I tested out Java. Here is a bit from my code:

——————————————————————————————————————————————————————————

import java.util. *;

class Program

{

public static void main (String[] args)

{

task_1();

}

static void task_1()

{

ArrayList<Point> pointList = new ArrayList<Point>();

Point p = new Point();

p.setX(5);

p.setY(10);

System.out.println(p.getX() + p.getY());

for (int i = 1; I <= 3; I ++)

{

pointList.get(i).setX((i+1)*10);

pointList.get(i).setY((i+1)*20);

}

System.out.println();

—————————————————————————————————————————————————————————–

Another program I decided to try is the alternative to Illustrator, which is Inkscape.

The result of using it you can see in the picture below.

Image

As for the alternative for Photoshop which is GIMP, I must say that I didn’t really enjoy working in this photo editor, it is great for those who use Photoshop for their everyday use of retouching pictures, but unfortunately at work I have to use photoshop for business purposes and GIMP is just not the say. But again it is a great tool for editing photographs.

Also I decided to translate programs in Launchpad Translations. It is great  tool to help out to translate someone’s program or you can also ask for help to translate your program. Currently I am working on translating Unity Tweak Tool to italian and finnish.

Hope now you can have even more fun with Linux and explore the world more through Linux goggles.

Understanding objects and references in C# – Part 1

It is sometimes quite difficult to understand the basics of Object-oriented Programming. I think the trick here is to understand every single detail. Today finally after hours of studying this subject, I can be quite confident to say that there is a simple set of rules you need to remember and try to follow. And also let’s not forget: practice makes everything perfect.

Let’s practice a bit.

Exercise 1.

int x;

int y = 2;

x = y;

So what happened in the first line? We created integer x, that has an undefined value.

In the second line we created integer y that was assigned to have a value of 2.

Third line is quite simple, we are just assigning integer x to have the same value as integer y, which is 2. Quite simple, isn’t it?

But let’s try to understand this exercise:

int x = 0;

Result r;

r = new Result();

So once again, in the first line we created integer x that has a value of 0. In the second line we created Result r that has a value of null (If you are wondering, why it is null, read my soon upcoming Part 2 of Understanding objects and references in C). In the third line now Result r has a new value and we can see that it is also calling for a class called Result. So let’s assume we have a class called Result:

class Result

{

private int score

public Result()

{

score = 10

}

public Result (int score)

{

this.score = score;

}

}

Then what do you think would be the new value of r? You can write your answers in the comments, and giving the class Result try to figure out this exercise as well:

Result r1 = new Result();

Result r2 = new Result(5);

That’s it for now with this small tutorial about Understanding objects and references in C#, if you would like to understand them better and do more complicated task, I will upload soon Part 2.

Tryout of Xubuntu 13-04.

It’s my second semester in Haaga-Helia studying Business IT. I was very excited about my course that I just have started Linux Basics taught by a very talented instructor Tero Karvinen, you can check out his website here. I was familiar with Linux before, but only knew how to use Linux Mint, never tried other distributions. For this course we are working closely with Linux Xubuntu. Our first small task was to create a Live CD and play around with a couple of commands. So I downloaded Xubuntu torrent from their official website. After the download, which was super fast, I started burning the CD, but realized that CD space of 700Mb is not enough, DVD was needed. Xubuntu size is around 800Mb. So after burning DVD, I left the DVD in my optical, I restarted the computer and went into the BIOS to set boot priority to optical. And after that, voilá! You can start enjoying the goodness of Xubuntu.

Of course I started playing around with command line and decided to try out a nice command $sudo lshw -short. This is how the output looks like:

—————————————————————————————————————————-

H/W path Device Class Description
===================================================
system System Product Name (SKU)
/0 bus F2A85-M LE
/0/0 memory 64KiB BIOS
/0/2e memory 8GiB System Memory
/0/2e/0 memory 4GiB DIMM DDR3 Synchronous 800 MHz (1.2 ns)
/0/2e/1 memory 4GiB DIMM DDR3 Synchronous 800 MHz (1.2 ns)
/0/38 memory 192KiB L1 cache
/0/39 memory 4MiB L2 cache
/0/3e processor AMD A10-5800K APU with Radeon(tm) HD Graphics
/0/100 bridge Family 15h (Models 10h-1fh) Processor Root Complex
/0/100/1 display Trinity [Radeon HD 7660D]
/0/100/1.1 multimedia Trinity HDMI Audio Controller
/0/100/10 bus FCH USB XHCI Controller
/0/100/10.1 bus FCH USB XHCI Controller
/0/100/11 storage FCH SATA Controller [AHCI mode]
/0/100/12 bus FCH USB OHCI Controller
/0/100/12.2 bus FCH USB EHCI Controller
/0/100/13 bus FCH USB OHCI Controller
/0/100/13.2 bus FCH USB EHCI Controller
/0/100/14 bus FCH SMBus Controller
/0/100/14.2 multimedia FCH Azalia Controller
/0/100/14.3 bridge FCH LPC Bridge
/0/100/14.4 bridge FCH PCI Bridge
/0/100/15 bridge Hudson PCI to PCI bridge (PCIE port 0)
/0/100/15.1 bridge Hudson PCI to PCI bridge (PCIE port 1)
/0/100/15.1/0 eth0 network RTL8111/8168 PCI Express Gigabit Ethernet controller
/0/101 bridge Family 15h (Models 10h-1fh) Processor Function 0
/0/102 bridge Family 15h (Models 10h-1fh) Processor Function 1
/0/103 bridge Family 15h (Models 10h-1fh) Processor Function 2
/0/104 bridge Family 15h (Models 10h-1fh) Processor Function 3
/0/105 bridge Family 15h (Models 10h-1fh) Processor Function 4
/0/106 bridge Family 15h (Models 10h-1fh) Processor Function 5
/0/1 scsi0 storage
/0/1/0.0.0 /dev/sda disk 120GB KINGSTON SVP200S
/0/1/0.0.0/1 /dev/sda1 volume 100MiB Windows NTFS volume
/0/1/0.0.0/2 /dev/sda2 volume 111GiB Windows NTFS volume
/0/2 scsi2 storage
/0/2/0.0.0 /dev/cdrom disk DRW-24B5ST
/0/2/0.0.0/0 /dev/cdrom disk
/0/2/0.0.0/0/2 volume 15EiB Windows FAT volume
/0/3 scsi8 storage
/0/3/0.0.0 /dev/sdb disk 2TB My Book 1130
/0/3/0.0.0/1 /dev/sdb1 volume 491GiB Windows NTFS volume
/0/3/0.0.0/2 /dev/sdb2 volume 1371GiB Windows NTFS volume
/0/3/0.0.1 generic SES Device

—————————————————————————————————————————-

This command is useful for when you want to find out the hardware configuration with device paths.

Another cool thing that I decided to try is called machinotero, a small program written by our instructor. That wasn’t quite easy, but let’s go step by step. First I saved all the code in the text editor with the name machinotero in my home folder. Running the code didn’t work straight away from command line. I heard the problem was most likely with file permissions, so in my home directory I gave the command $chmod 775 machinotero. After that I still couldn’t run just by declaring the program name (for that apparently the program needs to be in /usr/bin/local) but I could run it with $./machinotero. The program returned a notice that it should be run as super user, so I ran $sudo ./machinotero. This program lists many if not all necessary information of the host system. It is handy for finding out information regarding version of Linux, IP address, free and used disk space, information about hardware, server logs, etc.

So definitely try out Xubuntu if you would like to explore the world through Linux goggles. And especially if you don’t like paying for software licenses.