Showing posts with label Ubuntu. Show all posts
Showing posts with label Ubuntu. Show all posts

2013/05/11

Install webmin on Ubuntu server (12.04 LTS)

Webmin is powerful web-based application for administering your Unix based or Windows system. With webmin, you can modify basic configuration of your server, manage cron jobs, execute commands etc.

Webmin is modular, which makes it very flexible. You can see list of standard modules of latest version from this link:
http://www.webmin.com/standard.html

First up, you should look up most recent version of webmin, you can check latest version from here:
http://freefr.dl.sourceforge.net/project/webadmin/webmin/

Download package with wget:
 wget http://freefr.dl.sourceforge.net/project/webadmin/webmin/1.620/webmin_1.620_all.deb  

Webmin is going to need some dependencies in order to install, handy way to install all required packages is to use tool called gdebi. Run gdebi as sudo against your deb packet to install all dependencies.

First you need to install packet gdebi-core:
 sudo apt-get install gdebi-core  

Then you can run it against your .deb pakcage:
 sudo gdebi webmin_1.620_all.deb  
 Requires the installation of the following packages:  
 apt-show-versions libapt-pkg-perl libauthen-pam-perl libio-pty-perl libnet-ssleay-perl  
 web-based administration interface for Unix systems  
  Using Webmin you can configure DNS, Samba, NFS, local/remote filesystems  
  and more using your web browser. After installation, enter the URL  
  https://localhost:10000/ into your browser and login as root with your root  
  password.  
 Do you want to install the software package? [y/N]:y  

Now everything should be set up and we can proceed with webmin install:

 sudo dpkg -i webmin_1.620_all.deb  
 ...  
 Webmin install complete. You can now login to https://ip-192-168-1-1:10000/  
 as root with your root password, or as any user who can use sudo  
 to run commands as root.  

Now everything should be set! You can start configuring your server from https://<your server>:10000/

2012/10/01

Amazon EC2: Getting started (Part 2/2)

Back to Amazon EC2: Getting started (Part 1/2)

Now our instance is launching, view your instance by selecting "instances" from left navigation bar


















My instance is now running but its still Initializing, so we have to wait until instance is fully launched, this will take couple of minutes. Once "Initializing" will be replaced with something like 2/2 checks passed, instance is ready for use.

Next select your instance and properties windows will be populated with information about your instance. We need public DNS address in order to connect our instance. Private DNS address is used for internal communication of your instances.






Connecting to your server using PuTTY

First you must generate .ppk file from your private key .pem file. Windows users can follow this guide and Linux users this.

Copy paste your Public DNS or IP address to "Host name". Then browse to

Connection -> SSH -> Auth and Browse for your .ppk file and click Open.




















Since you are probably connecting your server for the first time PuTTY will alert you that this host key is not yet in system cache. You can ignore this and press Yes.

In ubuntu server, default login name is ubuntu.





Now we are logged in! Have fun!

If you want to setup LAMP server to your instance. Check out this:

Setting up LAMP in Ubuntu server

Amazon EC2: Getting started (Part 1/2)

EC2 = Amazon Elastic Compute Cloud

In this post we will create EC2 Micro instance.

For now Amazon EC2 has been my weapon of choice for testing out stuff. Instances are easy to launch and if you run it for couple of hours its basically free.

This is very simple guide that runs trough the process of creating a virtual machine in Amazon infrastructure.

I'm not going to cover signing up part, but its easy to do just follow the instructions. You need a valid credit card in order to register.

If you are registering first time to AWS, you will be eligible for Amazon's free tier!

Yes! Free micro instance for 12-months, the performance is not going to rock your world buts its alright to run small website on it. More information about free-tier here

http://aws.amazon.com/ec2/

Once you get your account done you can log into your AWS Console. As you can see EC2 is only one part Amazon Web Services, we are not covering any other services than EC2 now.


















Click EC2 to proceed.

Next up its importat to choose correct region in upper left navigation bar. If you are setting up your instance for your personal testing use, its good to choose region nearest to you.

















Once the region is selected hit "Launch Instance" button in the middle of the screen.





You can either use classical wizard or quick launch, both do the same but in our case Quick launch is quicker (duh) and correct image seem to be at the first page (Ubuntu Server 12.04.1 LTS)

Next set the name for your instance and set the name for your Key Pair. You must also download your key from the Download button next to Name field.

Key Pair? Yes, Amazon provides public/private key pairs, public key goes into your virtual machine and you will be using private key. These keys must match in order to establish a connection to your server.

You can only download this file once while creating the key pair so don't loose it! So I'm going to choose 64-bit Ubuntu server, you can use whatever you want.


Then we review our instance, we have Type t1.micro it is a Micro instance, it has 613 MB of memory poor I/O performance. But for testing purposes it will do fine. Personally at this point i would rename security group since it cannot be renamed afterwards.


You can create your custom security group if you click "Edit details". Check " Create new Security Group", name it and set some kind of description for it. From the "Create a new rule" Dropdown you select predefined groups, choose what you need and click "Add Rule". Here you could restrict from what source IP addresses can access your service, default 0.0.0.0/0 means everyone. 

After you are finished, click Create



Click Save details, then you will return to review windows and click Launch.

Continue to part 2











Generate .ppk out of .pem with Linux (Ubuntu)

Here is a example how to convert .pem to .ppk using Ubuntu.

First you need to install package putty-tools
 sudo apt-get install putty-tools  

After install, all you really need to do is this:
 puttygen key.pem -o key.ppk  

But.. with -P switch you can set passphrase for extra security, this is recommended and easy to do:
 puttygen key.pem -o key.ppk -P -C "My server key"  

It is also recommended to set comment for your key using -C switch, because this string will be prompted to you when you are entering your passphrase.





Note that you can also change passphrase afterwards by using -P switch

 m@box:~/Downloads$ puttygen -P key.ppk   
 Enter passphrase to load key:   
 Enter passphrase to save key:   
 Re-enter passphrase to verify:   

And you are done!


2012/09/24

Setting up LAMP in Ubuntu Server

This guide will take you trough process of installing fully functional LAMP server in Ubuntu. You can setup this easily in few minutes.

 sudo apt-get update  
 sudo tasksel  





Move to LAMP server and check it by pressing space, then click TAB to move to Ok and press Enter.

tasksell will download and install all necessary packets for you. Next you must enter MySQL root password and confirm it.








Once install is completed, verify it by connecting to http://<your public dns or ip>/

You should see this:









Yup! it works, now we will set up phpmyadmin for administrating MySQL

 sudo apt-get install phpmyadmin  


















Setup will automatically configure phpmyadmin for you.

Select "apache2" from the list by pressing space

Configure database for phpmyadmin with dbconfig-common? -> Yes

Here you must enter your MySQL root password. The one you entered earlier!

Enter password for application password for phpmyadmin

This is password for user "phpmyadmin" that will be used for communication between MySQL Server and phpmyadmin.

And we are set again, verify installation by visiting http://<your public dns or ip>/phpmyadmin

You can log in using "root" and your MySQL root password or with "phpmyadmin" and application specific password.














2012/09/13

Replicate linux data using rsync

I¨ve always had multiple Linux boxes at my disposal, usually they are just old laptops or desktop that i have planned to use for something but never had time to do anything useful with them, so i have them just lying around.

They never had any kind of raid or redundancy and my data has been "kept save" just by holy spirit :-)

Easy way to sleep without worrying is to replicate your data between servers. Easy way to achieve this is by using rsync and cron. In this example we don't use rsync protocol but we are using SSH connection for the transfer.

First you should create SSH keys to your server to make them access each other without need to enter credentials.

In this example i will create rsync from my home server to external server, in this case i have only 1 account for both servers that i will be using.

1. Create SSH Keys

Im using ubuntu servers so first i need to edit file /etc/ssh/sshd_config and uncomment one line

#AuthorizedKeysFile     %h/.ssh/authorized_keys
(remove # from the start)

Then i will login to my home server and create SSH key with command ssh-keygen -t rsa

I didn't use any passphrase


 m@homeserver:~$ ssh-keygen -t rsa  
 Generating public/private rsa key pair.  
 Enter file in which to save the key (/home/m/.ssh/id_rsa):   
 Enter passphrase (empty for no passphrase):   
 Enter same passphrase again:   
 Your identification has been saved in /home/m/.ssh/id_rsa.  
 Your public key has been saved in /home/m/.ssh/id_rsa.pub.  


Now we need to open this public key in /home/profile/.ssh/ that we just created, and copy paste all of its contents to external server.

login to external server
Authorized keys will be stored at file "authorized_keys" in /home/profile/.ssh

File didn't exist so i had to create it

 m@external:~/.ssh$ touch authorized_keys  

Open file with your favourite editor and paste contents of your public key into it + save.

We should be all done, time to test

 m@homeserver:~/.ssh$ ssh m@extserver  

Works! (at least for me), no password prompted, yippee.

2. rsync

I want to keep contents between my servers always identical, so i will use following rsync command:

 rsync -avz --delete /home/m/samba m@external:/home/m/backup/  

-a is for archive mode, its very good for backups it will preserve when file was modified and who is the owner and so on

-v is for verbose, so you will get some output about transfer itself

-z is for compress, will save some bandwidth

--delete will remove files on extserver that don't exist on sender, in my case homeserver

If you need to specify alternate port for your connection, add --rsh='ssh -p1234' modify 1234 to your port

3. Cronjob

Now we create cron in order to make this rsync run automatically. I want to replicate all changes once per day, there might be some huge changes in my samba directory so i want to give some time for this process. 24 hours should be enough..

Access crontab with:
 crontab -e  

In my case this will be easy, there is some predefined scheduling definitions that will work for me.
@daily will run job everyday at midnight. Please see more at Cron wiki page

So my crontab looks like this:
 @daily rsync -avz --delete /home/m/samba m@external:/home/m/backup/  

Some final words

Actually this is kind of bad backup, since one human error may compromise all of my data. So if i delete a very important file.txt from my samba share, rsync will destroy it from external server also.

There are many ways to overcome this, for example you could zip backup folder everyday for last x days, in my case this is not possible since i don't have enough disk space :-(

Anyway, be careful !


2012/09/12

Python and MySQL (MySQLdb)

If you need to access MySQL with Python MySQLdb is your friend.

Very good User guide can be found here. I lost few hairs when i was first playing with this so i decided to create small tutorial of my own. Hopefully will save some hair from someone

This is very basic stuff

Installing MySQLdb

In ubuntu system you need to install packet called python-mysqldb. Sources itself can be found from here

apt-get install python-mysqldb

Connecting database

 import MySQLdb   
  try:    
    conn = MySQLdb.connect(host=address,user=username,passwd=password,db=database)   
  except:    
    # Connection failed  

Executing commands

Here we pass two variables into INSERT command, first you need to create a cursor, then execute against this cursor.

After execute, commit must be applied or changes wont happen in your database.

 cursor = conn.cursor()  
 cursor.execute("""INSERT INTO data (col1,col2) VALUES (%s, %s)""", (var1,var2,))  
 conn.commit()  

TIP: After execute, you can get primary ID from previous insert with:

 print str(cursor.insert_id())  

Select from database

  sql = "SELECT * FROM stuff"  
  # Query  
  conn.query(sql)  

At this point, your query is executed, now you need to store your results

  # Store results  
  result_store = conn.store_result()  

Note: By default mysql db will return you only 1 row from result set, so if you specify maxrows=0 in your fetch_row, it will return all rows from the results set.

  # Fetch row from store  
  results = result_store.fetch_row(maxrows=0)  

Oh Yes.. this was my biggest issue, i didn't realize that result tuple had one tuple inside of it, stupid me!

Basically at this point if you print results output is:

 >>> print results  
 (('row1col1', 'row1col2'), ('row2col1', 'row2col2'))  

You can simply loop trough results with for each or get individual row by giving and index, row1 is naturally index number of 0 and row2 is index number of 1

 >>> row1 = results[1]  
 >>> print row1  
 ('row2col1', 'row2col2')  

And same applies to column..

 >>> col1 = row1[1]  
 >>> print col1  
 row2col2  

Closing connection
 conn.close()