Friday, December 21, 2012

Howto extend LVM on VMWare Guest running Linux


Recently I found my one of virtual Linux machine which running on VMWare ESX server have run out of disk space. And after I googled the Internet, I think it's quite simple as my virtual Linux machine is running on LVM.

And I also found the following solution will also apply to any VMWare virtual machine which running Linux OS with LVM.

Expand the disk

Turn off the virtual machine that you wish to extend disk space.


You can expand the virtual disk size by entering ESX server hidden console mode or using VMware Infrastructure Client to remote manage your ESX Server. I will introduce both way here.
  1. Method One:

    Since the GUI mode is easy. Click Your VMWare Guest Machine in your remote management console from VMware Infrastructure Client. Then click "Edit Virtual Machine Settings" from "Getting Started" which is in the Right Panel. SelectHardware -> Hard Disk 1 -> Capacity - New Size (at Right Side), and then expand the Capacity to whatever size that you need. In our case, I expanded from 100GB to 200GB. Then press "OK".


  2. Method Two:

    For ESXi Server, using the following command to enter ESXi hidden console mode after press "Alt"+"F1":

    unsupported



    Then press "Enter", now enter your ESXi server root password. You should be able to successfully log into the hidden ESXi Server console.

    For ESX Server, If you press Alt-F1, you can get access to the Linux login prompt of the ESX service console and login to a command prompt.


Now enter the following command to expand the VM Disk:

vmkfstools -X 200G "Your VMDK file here"


To extend an existing Virtual Disk to 200GB.

Note:

If you are running VMware workstation product, then you can expand the virtual disk to expand the virtual disk on your Windows machine by running this command:

vmware-vdiskmanager -x 200G "My harddisk.vmdk"


After above steps, you finished preparing the extra virtual disk space. Now we can start your VMware virtual machine and open a terminal session to continue expanding your LVM.

Issue the df -k command which shows us that the logical volume is at 100% usage. We need to create a partition on /dev/sda. Type ls -al /dev/sda* which lists the existing partitions. Our new partition will be /dev/sda3

# ls -al /dev/sda*
brw-r----- 1 root disk 8, 0 Apr 2 19:26 /dev/sda
brw-r----- 1 root disk 8, 1 Apr 2 19:26 /dev/sda1
brw-r----- 1 root disk 8, 2 Apr 2 19:26 /dev/sda2


Type fdisk /dev/sda then type n for new partition. Enter p and 3 for the partition number (in this instance, obviously enter the partition number that matches your environment.) Also accept the default First and Last cylinders. Finally type w to write table to disk and exit.

If prompted to reboot then do so to ensure that the new partition table is written. After a restart, type ls -al /dev/sda* to verify whether the new partition was created successfully. 

# ls -al /dev/sda*
brw-r----- 1 root disk 8, 0 Apr 2 19:26 /dev/sda
brw-r----- 1 root disk 8, 1 Apr 2 19:26 /dev/sda1
brw-r----- 1 root disk 8, 2 Apr 2 19:26 /dev/sda2
brw-r----- 1 root disk 8, 3 Apr 2 19:26 /dev/sda3


After verification of new partition, we need to create a physical volume and add it to the volume group. 

Type pvcreate /dev/sda3 

[root@rhel5 ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created


Type vgextend VolGroup00 /dev/sda3

# vgextend VolGroup00 /dev/sda3
Volume group "VolGroup00" successfully extended


We need to extend the logical volume. Type vgdisplay:

# vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 99.84 GB
PE Size 32.00 MB
Total PE 3345
Alloc PE / Size 2868 / 99.88 GB
Free PE / Size 2870 / 99.97 GB
VG UUID MkluWy-e5PA-QTGN-fF7k-ZxO3-6bC7-qxfCii


Which shows that there is 99.97GB free that we can add to the volume group. To extend the volume type lvextend -L +99.97G /dev/VolGroup00/LogVol00. If you got an error message that stated that there was "Insufficient free space: 2871 extends needed but only 2870 evailable". After a quick google: Insufficient Free Extents for a Logical Volume. It seems that we needed to select a lightly smaller size. So we changed to 99GB rather than 99.97GB which solved this problem.

# lvextend -L+99G /dev/VolGroup00/LogVol00
Rounding up size to full physical extent 99 GB
Extending logical volume LogVol00 to 198 GB
Logical volume LogVol00 successfully resized


Finally we need to resize the file system by typing resize2fs /dev/VolGroup00/LogVol00.

resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 20101888 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 20101888 blocks long.


Type df -k to see if the new space is available to the logical volume.

df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
1713916000 82428400 88250280 48% /
/dev/sda1 101086 24538 71329 26% /boot
tmpfs 1037744 0 1037744 0% /dev/shm


The logical volume has now been resized and now has used space of just 48%!

Original Source: http://echenh.blogspot.com/2010/04/how-to-extend-lvm-on-vmware-guest-os.html 

Wednesday, December 5, 2012

How to Set Up Apache Virtual Hosts on Ubuntu 12.04 LTS


About Virtual Hosts


Virtual Hosts are used to run more than one domain off of a single IP address. This is especially useful to people who need to run several sites off of one virtual private server. The sites display different information to the visitors, depending on with which the users accessed the site.There is no limit to the number of virtual hosts that can be added to a VPS.

Set Up

The steps in this tutorial require the user to have root privileges. You can see how to set that up in the here Initial Server Setup. Furthermore, if I reference the user in a step, I’ll use the name www. You can implement whatever username suits you. 

Additionally, you need to have apache already installed and running on your virtual server 
If this is not the case, you can download it with this command:
sudo apt-get install apache2

Step One— Create a New Directory


The first step in creating a virtual host is to a create a directory where we will keep the new website’s information. 

This location will be your Document Root in the Apache virtual configuration file later on. By adding a -p to the line of code, the command automatically generates all the parents for the new directory.
sudo mkdir -p /var/www/example.com/public_html

You will need to designate an actual DNS approved domain, or an IP address, to test that a virtual host is working. In this tutorial we will use example.com as a placeholder for a correct domain name. 

However, should you want to use an unapproved domain name to test the process you will find information on how to make it work on your local computer in Step Seven. 

Step Two—Grant Permissions


We need to grant ownership of the directory to the user, instead of just keeping it on the root system.
 sudo chown -R www:www /var/www/example.com/public_html 

Additionally, it is important to make sure that everyone will be able to read our new files.
 sudo chmod 755 /var/www

Now you are all done with permissions.

Step Three— Create the Page


Within our configurations directory, we need to create a new file called index.html
sudo nano /var/www/example.com/public_html/index.html

We can add some text to the file so we will have something to look at when the IP redirects to the virtual host.

  
    www.example.com
  
  
    

Success: You Have Set Up a Virtual Host


Save and Exit

Step Four—Create the New Virtual Host File


The next step is to set up the apache configuration. We’re going to work off a duplicate—go ahead and make a copy of the file (naming it after your domain name) in the same directory:
 sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/example.com

Step Five—Turn on Virtual Hosts


Open up the new config file:
 sudo nano /etc/apache2/sites-available/example.com

We are going to set up a virtual host in this file.

The first step is to insert a line for the ServerName under the ServerAdmin line.
  ServerName example.com 

The ServerName specifies the domain name that the virtual host uses. 

If you want to make your site accessible from more than one name (for example, with www in the URL), you can include the alternate names in your virtual host file by adding a ServerAlias Line. The beginning of your virtual host file would then look like this:

        ServerAdmin webmaster@example.com
        ServerName example.com
        ServerAlias www.example.com
  [...]

The next step is to fill in the correct Document Root. For this section, write in the extension of the new directory created in Step One. If the document root is incorrect or absent you will not be able to set up the virtual host.

The section should look like this:
 DocumentRoot /var/www/example.com/public_html 

You do not need to make any other changes to this file. Save and Exit.

The last step is to activate the host. Luckily, Ubuntu provides an easy method to do this:
 sudo a2ensite example.com

Step Six—Restart Apache


We’ve made a lot of the changes to the configuration, and the virtual host is set up. However none of the changes that we made will take effect until Apache is restarted. Use this command to restart apache:
 sudo /etc/init.d/apache2 reload 

You may see an error along the lines of
Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName 

The message is just a warning, and you will be able to access your virtual host without any further issues.

Optional Step Seven—Setting Up the Local Hosts


If you have pointed your domain name to your virtual private server’s IP address you can skip this step—you do not need to set up local hosts. Your virtual hosts should work. However, if want to try out your new virtual hosts without having to connect to an actual domain name, you can set up local hosts on your computer alone. 

For this step, make sure you are on the computer itself, not your droplet. 

To proceed with this step you need to know your computer’s administrative password, otherwise you will be required to use an actual domain name to test the virtual hosts.

If you are on a Mac or Linux, access the root user (su) on the computer and open up your hosts file:
nano /etc/hosts 

If you are on a Windows Computer, you can find the directions to alter the host file on theMicrosoft site

You can add the local hosts details to this file, as seen in the example below. As long as that line is there, directing your browser toward, say, example.com will give you all the virtual host details for the corresponding IP address.
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost

#Virtual Hosts 
12.34.56.789    example.com 

However, it may be a good idea to delete these made up addresses out of the local hosts folder when you are done to avoid any future confusion. 

Step Eight—RESULTS: See Your Virtual Host in Action


Once you have finished setting up your virtual host, you can see how it looks online. Type your ip address into the browser (ie. http://12.34.56.789) 

It should look somewhat similar to my handy screenshot

Good Job!

Creating More Virtual Hosts


To add more virtual hosts, you can just repeat the process above, being careful to set up a new document root with the appropriate domain name, and then creating and activating the new virtual host file.

Original Source: https://www.digitalocean.com/community/articles/how-to-set-up-apache-virtual-hosts-on-ubuntu-12-04-lts

Install LAMP on Ubuntu



sudo apt-get install lamp-server^

Don't leave out the caret (^) at the end


sudo apt-get install php5 php5-gd php5-mysql php5-curl php5-cli php5-cgi php5-dev


Install phpmyadmin
sudo apt-get install phpmyadmin
After some time downloading packages, you will be prompted to set a password for the root user forMySQL.

Enter the password you want to use for MySQLDo not leave it blank. You'll be prompted a second time to confirm your password.


Test Apache

Open a web browser window and enter the address http://localhost/. You should see a web page that says "It Works!"
Apache works




Test php

Now that you've confirmed that the Apache web server works, you want to make sure that your phpinstallation is working. To do that you need to create a file in /var/www called testing.php. You can use your favorite text editor as root, or you can use the following terminal command:
echo "" | sudo tee /var/www/testing.php
After that you need to restart the Apache web server.
sudo service apache2 restart
Go back to your web browser and enter the address http://localhost/testing.php/ and you should see a page displaying information about your php installation.
php information













Enable SSH (Secure Shell) in Ubuntu 12.04


sudo apt-get install openssh-server
sudo vi /etc/ssh/sshd_config
openssh_server_precise_2.png (555×164)

Change Ubuntu Server from DHCP to a Static IP Address


Change Ubuntu Server from DHCP to a Static IP Address

If the Ubuntu Server installer has set your server to use DHCP, you will want to change it to a static IP address so that people can actually use it.
Changing this setting without a GUI will require some text editing, but that’s classic linux, right?
Let’s open up the /etc/network/interfaces file. I’m going to use vi, but you can choose a different editor
sudo vi /etc/network/interfaces
For the primary interface, which is usually eth0, you will see these lines:
auto eth0
iface eth0 inet dhcp
As you can see, it’s using DHCP right now. We are going to change dhcp to static, and then there are a number of options that should be added below it. Obviously you’d customize this to your network.
auto eth0
iface eth0 inet static
        address 192.168.1.100
        netmask 255.255.255.0
        network 192.168.1.0
        broadcast 192.168.1.255
        gateway 192.168.1.1
Now we’ll need to add in the DNS settings by editing the resolv.conf file:
sudo vi /etc/resolv.conf
On the line ‘name server xxx.xxx.xxx.xxx’ replace the x with the IP of your name server. (You can do ifconfig /all to find out what they are)
You need to also remove the dhcp client for this to stick (thanks to Peter for noticing). You might need to remove dhcp-client3 instead.
sudo apt-get remove dhcp-client
Now we’ll just need to restart the networking components:
sudo /etc/init.d/networking restart
Ping www.google.com. If you get a response, name resolution is working(unless of course if google is in your hosts file).
Really pretty simple.
Original Source: http://www.howtogeek.com/howto/ubuntu/change-ubuntu-server-from-dhcp-to-a-static-ip-address/