Debian Wheezy (7.5): Name-Based Web Sites on a Single IP Address (vhosts)

Configuring virtual hosting with Debian Wheezy has a little different steps from that with Slackware. To avoid from getting myself confused (and hopefully help someone else to set their virtual host sites), these are the steps I used for my local sites.

Disclaimer:
The information in this site is the result of my researches in the Internet and of my experiences. It is solely used for my purpose and may not be suitable for others. I will NOT take any responsibility of end result after following these steps (although I will try to help if you send me your questions/problems).

1) Disabling Default Virtual Host

First, let's disable the default Apache virtual host with a2dissite. What this command do is simply removing a symlink to /etc/apache2/sites-enabled/. # a2dissite default

2) Creating a New Directory and Setting Permissions

It's necessary to create a directory where site's website files and logs reside and grant ownership of the directory to the user instead of keeping it on the root system. For example, I'm setting up for siteA.org and siteB.org.

siteA.org
# mkdir -p /var/www/siteA.org/public_html # mkdir /var/www/siteA.org/logs # chown -R [$user]:[$group] /var/www/siteA.org/public_html
siteB.org
# mkdir -p /var/www/siteB/public_html # mkdir /var/www/siteB.org/logs # chown -R [$user]:[$group] /var/www/siteB.org/public_html

3) Creating Config files

Each virtual host needs own configuration file placed in /etc/apache2/sites-available/ directory. Each configuration file is as follow. Make sure that you have all directories specified in each conf file exist before you restart the apache process; otherwise, it'll fail to start.

siteA.org

# vim /etc/apache2/sites-available/siteA.org.conf ------------------------------------ <VirtualHost *:80> ServerAdmin webmaster@siteA.org ServerName siteA.org ServerAlias www.siteA.org DocumentRoot /var/www/siteA.org/public_html <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/siteA.org/public_html/> Options Indexes FollowSymLinks AllowOverride None Order allow,deny allow from all </Directory> ErrorLog /var/www/siteA.org/logs/error.log CustomLog /var/www/siteA.org/logs/access.log combined </VirtualHost>

siteB.org

# vim /etc/apache2/sites-available/siteB.org.conf ------------------------------------ <VirtualHost *:80> ServerAdmin webmaster@siteA.org ServerName siteB.org ServerAlias www.siteB.org DocumentRoot /var/www/siteB.org/public_html <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/siteB.org/public_html/> Options Indexes FollowSymLinks AllowOverride None Order allow,deny allow from all </Directory> ErrorLog /var/www/siteB.org/logs/error.log CustomLog /var/www/siteB.org/logs/access.log combined </VirtualHost>

4) Enabling the Sites

Now activate the host: # a2ensite siteA.org.conf # a2ensite siteB.org.conf

5) Restarting Apache

Restart the Apache server to initialize the changes: # service apache2 restart

6) Setting Up Local Host

Edit /etc/hosts so that the sites can be found by name: # vim /etc/hosts ------------------------------------ 127.0.0.1 localhost siteA siteB

That's all!
-gibb

Debian Wheezy (7.5): LAMP (Linux, Apache, MariaDB, and PHP)

LAMP used to refer to Linux, Apache, MySQL, and PHP but nowadays the trend is transitioning from MySQL to MySQL's drop-in replacement MariaDB. The Slackware project switched the default database to MariaDB back in March 2013 for the version 14.1 and forward.

I was a little concerned about this change and wasn't sure if my web sites would work with MariaDB. However, my worry was trivial. MariaDB uses the same files as MySQL so this makes migration a lot easier.

So it's natural for me to try MariaDB on my new Debian Wheezy (7.5) system.

Disclaimer:
The information in this site is the result of my researches in the Internet and of my experiences. It is solely used for my purpose and may not be suitable for others. I will NOT take any responsibility of end result after following these steps (although I will try to help if you send me your questions/problems).

Installing Apache2

Firts, make sure the system is up-to-date: # apt-get update && apt-get upgrade -y

Then, install apache2: # apt-get install apache2

Add apache2 to system start up and start it up now: # udpate-rc.d apache2 enable update-rc.d: using depndency based boot sequencing # service apache2 start [ ok ] Starting web server: apache2.

If you open a web browser and point it to http://localhost, you'll see the message It works!

Installing php5

Next, install php5 along with the apache php5 module, MySQL(MariaDB) php module, and other modules: # apt-get install php5-curl php5-xmlrpc php5-gd php5-intl libapache2-mod-php5 php5 php5-common php5-dev php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-mysql php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy

Restart the web server: # apache2 restart

Test the php support by creating a php file (phpinfo.php) in the default document root, /var/www: # vim /var/www/phpinfo.php -------------------------------------------- < ?php phpinfo(); ?>

With successful installation/configuration, below page should be loaded:
debian_lamp_install_phpinfo

Installing MariaDB

To properly install and configure MariaDB, I need to know the version/codename of this Debian. I already know its Wheezy but to check, type the following command: # lsb_release -a No LSB modules are available. Distributor ID: Debian Description: Debian GNU/Linux 7.5 (wheezy) Release: 7.5 Codename: wheezy

Now, open a web browser and go to MariaDB's download page to get the repository information for MariaDB: debian_lamp_install_mariadb_config

Above selection produces below repository info:
debian_lamp_install_mariadb_repo

Create a file called mariadb.list under /etc/apt/sources.list.d and copy & paste the repository info: # vim /etc/apt/sources.list.d/mariadb.list -------------------------------------------- # MariaDB 10.0 repository list - created 2014-05-10 06:44 UTC # http://mariadb.org/mariadb/repositories/ deb http://mirror.jmu.edu/pub/mariadb/repo/10.0/debian wheezy main deb-src http://mirror.jmu.edu/pub/mariadb/repo/10.0/debian wheezy main

Add MariaDB to the system: # apt-get install python-software-properties # apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db # apt-get update # apt-get install mariadb-server

Follow the on-screen instructions to set up a root password for MariaDB server.

Let's see if MariaDB server was successfully installed: # mysql -u root -p Enter password: Welcome to the MariaDB monitor. Command end with ; or \g. Your MariaDB connection id is 38 Server version: 10.0.10-MariaDB-1~wheezy mariadb.org binary distribution Copyright (c) 2000, 2014 Oracle, SkySQL Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>

VoilĂ ! With above steps, I have successfully installed LAMP stack on my Debian Wheezy server.

If you are interested, take a look at my post on VirtualHost: Name-Based Web Sites on a Single IP Address

That's all!
-gibb

Debian Wheezy (7.5): Accessing the Encrypted Partition From the Recovery System

Continued from my previous post, Debian Wheezy (7.5): Encrypted Root Filesystem on laptop.

In my previous post, I created an extra partition for recovery system that could be used to repair the main system in a situation where it becomes corrupted or un-bootable. But how exactly can I access it from the recovery system? Well, steps described below is something I would try, in other words, just a theory. If you know a better (correct) way, or if I'm doing wrong, please feel free to comment!

Disclaimer:
The information in this site is the result of my researches in the Internet and of my experiences. It is solely used for my purpose and may not be suitable for others. I will NOT take any responsibility of end result after following these steps (although I will try to help if you send me your questions/problems).

Booting Into the Recovery System

At the GRUB menu, choose the Recovery system. In my case it's on /dev/sda2.
debian_install_4

Accessing Encrypted Device with `cryptsetup luksOpen`

First, let's see my partition layout: # parted (parted) p Model: ATA WDC WD3200BEKT-6 (scsi) Disk /dev/sda: 320GB Sector Size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 1 1049kB 310GB 310GB 2 310GB 320GB 10.1GB ext4 (parted) q

Since /dev/sda1 is encrypted with crypt-luks, normal mount command would not work. # mount /dev/sda1 /mnt/main mount: unknown filesystem type 'crypto_LUKS'

So it needs to be opened to access the encrypted device. This process requires your passphrase. This will create /dev/mapper/unlocked. # cryptsetup luksOpen /dev/sda1 unlocked Enter passphrase for /dev/sda1:

Can we mount the device now? Nope. Because it's LVM.

Accessing LVM

First install lvm2. # apt-get install lvm2

SIDE NOTE:
If you get the following warnings after executing above command:

update-initramfs: Generating /boot/initrd.img-3.2.0-4-amd64
W: Possible missing firmware /lib/firmware/rtl_nic/rtl8168f-2.fw for module r8169
W: Possible missing firmware /lib/firmware/rtl_nic/rtl8168f-1.fw for module r8169
W: Possible missing firmware /lib/firmware/rtl_nic/rtl8105e-1.fw for module r8169
W: Possible missing firmware /lib/firmware/rtl_nic/rtl8168e-3.fw for module r8169
W: Possible missing firmware /lib/firmware/rtl_nic/rtl8168e-2.fw for module r8169
W: Possible missing firmware /lib/firmware/rtl_nic/rtl8168e-1.fw for module r8169
W: Possible missing firmware /lib/firmware/rtl_nic/rtl8168d-2.fw for module r8169
W: Possible missing firmware /lib/firmware/rtl_nic/rtl8168d-1.fw for module r8169

You need to add contrib and non-free repositories to /etc/apt/sources.list: # vim /etc/apt/sources.list -------------------------------------- deb http://ftp.us.debian.org/debian wheezy main contrib non-free # apt-get update Then install the firmware-realtek package: # apt-get install firmware-realtek

Then load the necessary module. # modprobe dm-mod

Scan the system for LVM volumes and identify the volume group name in the output. # vgscan Reading all physical volumes. This may take a while... Found volume group "debian" using metadata type lvm2

Activate the volume. # vgchange -ay debian 2 logical volume(s) in volume group "debian" now active

Then find the logical volume that has the root filesystem # lvs LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert root debian -wi-a--- 284.98g swap debian -wi-a--- 3.72g

Mounting It Now!

Now all the preparation is done. It's time to mount it. # mount -o ro,user /dev/debian/root /mnt/unlocked # ls mnt/unlocked bin etc lib media proc sbin sys var boot home lib64 mnt root selinux tmp vmlinuz dev initrd.img lost+found opt run srv usr

VoilĂ ! Successfully mounted!

That's all!
-gibb

Debian Wheezy (7.5): Encrypted Root Filesystem on laptop

I'm not a distro-hopper. Well, that's what I thought but I'm probably wrong (and nothing wrong with being a distro-hopper!). On my main system, I'm using Slackware since its version 9 or 10. However, on my laptop (HP Pavilion dm3-1130us), I tried RHCE, Fedora, Xubuntu, Mint, CrunchBang, Arch, and FreeBSD. Each distro had its pros and cons but it didn't really stick to me. I liked FreeBSD the best among them but it drained the battery and heated up my laptop compared to other distros. Also I couldn't get some of hardware components (ex: built-in webcam) working. I believe FreeBSD is an excellent OS for servers but probably not for laptops so much. So, I was in a quest for another distro again and decided to try on Debian.

Disclaimer: The information in this site is the result of my researches in the Internet and of my experiences. It is solely used for my purpose and may not be suitable for others. I will NOT take any responsibility of end result after following these steps (although I will try to help if you send me your questions/problems).

Now onto Debian. As other distributions, I wanted to try encrypted disk/filesystem(s) for my laptop. After a bit of research, I came across to this article. Interesting. My laptop doesn't have a CD/DVD drive, either, but I never thought of having a recovery partition in case of emergency. So I decided to give it a try with this method. Since this article is a bit outdated, I'll describe it with most recent version of Debian (Wheezy) and add some steps.

1. Creating Bootable USB Stick

Download the netinst.iso image from Debian website and create a bootable USB stick. # dd if=debian-7.5-0-amd64-netinst.iso of=/dev/sdX

2. Setting Up Recovery System

Start the Debian installer. Since I love OpenBox, I select below options for the installation.
Advanced options -> Alternative desktop environments -> LXDE -> Graphical install
Follow the installer until you get to set the hostname. I set it as debianrecov for recovery. Follow it until you get to "Partition disks" and select Manual. Here is the partition scheme to use:
  • Main partition for LVM and encrypted, taking up the whole disk minus 3GB. Set it as Do not use for now.
  • 3GB recovery partition at the end of the disk. This will be /boot for the main system. (3GB is an arbitrary size I picked. I tried with 1GB and the installation failed when installing packages.)
    - Set it as ext4 mounted as "/" - Set its label as "recovery"
Choose Finish partitioning and write changes to disk. It'll then warn you that there is no partition for swap space and ask you whether to return to the partition menu. Just select No and follow the rest of the installation. Reboot your system and make sure it boots up without any issues.

3. Setting Up Main System

Now boot the Debian installer again. Select the same options for the installation and follow it until you get to "Partition disks". Select Manual. Select the main partition and hit the Continue button. Then choose physical volume for encryption for "Use as:". debian_install_1 Select "Done setting up the partition". Next select "Configure encrypted volumes". Then "Create encrypted volumes" and choose the main partition. After selecting "Yes" for erasing data on the partition, it'll start randomizing it. This will take very long time (on my laptop, it took more than 10 hours). When it's done, it'll ask for a passphrase. This is the phrase you type at every boot and it is not recoverable so don't forget it! Select the contents of the "disk" Encrypted volume and debian_install_2 Next, select physical volume for LVM for "Use as:" and choose "Done setting up the partition". Then select Configure the Logical Volume Manager and create a Volume Group. The original article uses the hostname for the Volume Group to reduce confusion if the disk is plugged into another machine for disaster recovery. I think that's a great idea. Create a Logical Volume called swap. If you plan to use suspend-to-disk, this needs to be at least as large as your RAM. Create a Logical Volume called root. Set the swap Logical Volume you just created to be used as a swap area and your root Logical Volume to be used as ext4 mounted at "/". Also set your recovery partition to be used as ext4, mounted on "/boot", and the format partition option to "no, keep existing data". This is how the partition layout looks like: debian_install_3 Proceed with the rest of the installation and reboot the system when it's done.

4. Making Them Dual-boot

In the original article, it now talks about setting up dual-boot. Debian Wheezy uses GRUB2 and menu.lst is no longer available. However, it should automatically detect your recovery system and it should look like below during GRUB menu: debian_install_4 If your recovery system is missing, you could try running the update-grub2 command in your main system: # update-grub2 Generating grub.cfg ... Found linux image: /boot/vmlinuz-3.2.0-4-amd64 Found initrd image: /boot/initrd.img-3.2.0-4-amd64 Found Debian GNU/Linux (7.5) on /dev/sda2 done If update-grub2 did not work, make sure that the recovery partition was set to be ext4, mounted on "/boot", and the format partition option was set to "no, keep existing data" at the end of Step 3 above. Tomorrow, I'll talk about accessing main system's area from recovery system. That's all! -gibb

Linux: Getting to know `find` command

The find command is one of most important and much used command in my opinion. It's very useful because it not only finds files and directories with detailed options but also can execute additional commands (ex: mv, rm, etc...) on found items.

I'm ashamed to say this but, on the other day, I accidentally corrupted my external USB hard drive that had all of my back-up files! I used TestDisk hoping to fix its partition table but it didn't work. So I used PhotoRec to recover photos.

If you have used PhotoRec before, you know this but it does not recover files with original names. Instead, it creates a directory, recup_dir.[number], and put files with a unique names like f1175051952.jpg. In my case, it created more than 3000 directories with image files scattered all over. The find command came in handy!

Disclaimer:
The information in this site is the result of my researches in the Internet and of my experiences. It is solely used for my purpose and may not be suitable for others. I will NOT take any responsibility of end result after following these steps (although I will try to help if you send me your questions/problems).

Finding all .jpg files and ignoring case:

Find all files whose name has .jpg extension in the current directory and below. $ find . -iname "*.jpg" -print

Finding and moving all .jpg files in one single pass:

Find all files whose name has .jpg extension in the current directory and below and move them to /mnt/jpg. $ find . -iname "*.jpg" -type f -exec mv {} /mnt/jpg \;

Finding and removing empty directories:

Find empty directory in the current directory and below and remove them. $ find . -type d -empty -exec rmdir {} \;

Finding files with no extensions:

Find files whose name does not contain extension in the current directory and below. $ find . -type f ! -name "*.*"

Finding files without .jpg extension:

Find files whose name does not have .jpg extension in the current directory and below. $ find . -type f ! -name "*.jpg"

Here is some other useful options.

Finding files with 777 permissions:

Find files whose permissions are 777 $ find . -type f ! -perm 0777 -print

Finding files based on user:

Find files which belong to user ubyt3m3 under /home directory. $ find /home -user ubyt3m3 -print

Finding accessed files in last 1 hour:

Find files which are accessed in last 1 hour under /var/log directory. $ find /var/log -amin -60 -print

Finding last 7-14 days modified files:

Find files which are modified in last 1 hour under /home/www directory. $ find /home/www -mtime +7 -mtime -14 -print

That's all!
-gibb