Debian: Converting Apache + PHP to FastCGI – The Simple Way

I have a server running on Debian 6 64bit installed with Apache and PHP5 using apt-get package manager. By default, it will configure DSO  as the PHP handler (more details here). I need to convert it to serve PHP through FastCGI (mod_fcgid) to save memory usage. It turns to be that converting them is easy and require simple steps.

 

Here is my pre-configured Apache + PHP settings (installed using apt-get install apache2 php5 command):

$ dpkg --get-selections | grep apache
apache2             install
apache2-mpm-prefork install
apache2-utils       install
apache2.2-bin       install
apache2.2-common    install
libapache2-mod-php5 install
 
$ dpkg --get-selections | grep php
libapache2-mod-php5 install
php5                install
php5-cli            install
php5-common         install
php5-suhosin        install

 

1. Update repository:

$ apt-get update

2. Install required packages for fcgid:

$ apt-get install apache2-mpm-worker libapache2-mod-fcgid php5-cgi

3. Enable cgi.fix_pathinfo in /etc/php5/apache2/php.ini:

cgi.fix_pathinfo=1

4. Stop Apache:

$ /etc/init.d/apache2 stop

5. Disable php5, since we will be using php-cgi:

$ a2dismod php5

6. Setup the virtual host for the website under /etc/apache2/sites-available/default:

 <VirtualHost *:80>
        ServerName www.example.com
        ServerAdmin admin@example.com
        DocumentRoot /var/www
 
        <Directory /var/www>
                Options +ExecCGI
                AllowOverride AuthConfig FileInfo Limit
                AddHandler fcgid-script .php
                FCGIWrapper /usr/lib/cgi-bin/php .php
                Order Deny,Allow
                Allow from All
        </Directory>
 
        ErrorLog /var/log/apache2/error.log
        LogLevel warn
 
        CustomLog /var/log/apache2/access.log combined
 
</VirtualHost>

7. Start Apache:

$ /etc/init.d/apache2 start

 

Done! You are now running on FastCGI as the PHP handler. You can verify this with phpinfo:

phpinfo

 

Ubuntu: Error Installing MySQL Server

I encountered following error when trying to upgrade MySQL server in Ubuntu 12.04:

dpkg: error processing mysql-server-5.5 (--configure):
 subprocess installed post-installation script returned error exit status 1
No apport report written because MaxReports is reached already
                                                              Setting up mysql-client (5.5.22-0ubuntu1) ...
dpkg: dependency problems prevent configuration of mysql-server:
 mysql-server depends on mysql-server-5.5; however:
  Package mysql-server-5.5 is not configured yet.
dpkg: error processing mysql-server (--configure):
 dependency problems - leaving unconfigured
No apport report written because MaxReports is reached already
                                                              Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
Errors were encountered while processing:
 mysql-server-5.5
 mysql-server
E: Sub-process /usr/bin/dpkg returned an error code (1)
A package failed to install.  Trying to recover:
Setting up mysql-server-5.5 (5.5.22-0ubuntu1) ...
start: Job failed to start
invoke-rc.d: initscript mysql, action "start" failed.
dpkg: error processing mysql-server-5.5 (--configure):
 subprocess installed post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of mysql-server:
 mysql-server depends on mysql-server-5.5; however:
  Package mysql-server-5.5 is not configured yet.
dpkg: error processing mysql-server (--configure):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 mysql-server-5.5
 mysql-server

The easiest way to overcome this is by removing all mysql related packages in the server:

$ sudo apt-get purge mysql*

Then, try again to reinstall the mysql-server package as follow:

$ sudo apt-get install -y  mysql-client mysql-server

Make sure to backup your data files before performing the upgrade! Hope this workaround will help others!

Install MariaDB Galera Cluster in Ubuntu

I am going to show you on how to install MariaDB Cluster (with Galera) in Ubuntu Precise (12.04) LTS 64bit. You need at least 3 hosts running together to form a reliable cluster.

Hosts list:

maria1 – 192.168.10.101
maria2 – 192.168.10.102
maria3 – 192.168.10.103

 

Prepare Hosts

In all hosts, this is my /etc/hosts definition:

192.168.10.101        maria1 maria1.mycluster.com
192.168.10.102        maria2 maria2.mycluster.com
192.168.10.103        maria3 maria3.mycluster.com

 

Install MariaDB Cluster

1. Install the GPG key required by apt:

$ sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 1BB943DB

2. Add the apt repository:

$ sudo add-apt-repository 'deb http://mirror.stshosting.co.uk/mariadb/repo/5.5/ubuntu precise main'

3. Get the latest repo:

$ sudo apt-get update

4. Install MariaDB Cluster and related packages:

$ sudo apt-get install -y galera  mariadb-galera-server-5.5 mariadb-client-5.5 libmariadbclient18 mariadb-client-core-5.5 rsync netcat-openbsd

** Make sure you put the same MySQL root password on all hosts

 

Install Percona Xtrabackup

1. We are going to use Xtrabackup to perform State Snapshot Transfer (SST). Install GPG key for Percona:

$ sudo apt-key adv --keyserver keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A

2. Add the apt repository for Percona:

$ sudo add-apt-repository 'deb http://repo.percona.com/apt precise  main'

3. Update and install Percona Xtrabackup:

$ sudo apt-get update && sudo apt-get -y install percona-toolkit percona-xtrabackup

 

Configure MariaDB Cluster

1. Once the installation completed, you need to comment following lines in /etc/mysql/my.cnf:

#bind-address
#default_storage_engine
#query_cache_limit
#query_cache_size

2. Add following lines for wsrep configuration options in /etc/mysql/my.cnf under [mysqld] directive:

binlog_format=ROW
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
innodb_locks_unsafe_for_binlog=1
innodb_doublewrite=1

3. Add following lines into specific node under [mysqld] directive:

maria1:

# wsrep provider configuration
wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_provider_options="gcache.size=256M; gcache.page_size=128M"
wsrep_cluster_address=gcomm://
wsrep_cluster_name="MariaDB_Cluster"
wsrep_node_address="maria1"
wsrep_node_name="maria1"
wsrep_sst_method=xtrabackup
wsrep_sst_auth="root:MyR00tPasswd"
wsrep_node_incoming_address=192.168.10.101
wsrep_sst_receive_address=192.168.10.101
wsrep_slave_threads=16

maria2:

# wsrep provider configuration
wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_provider_options="gcache.size=256M; gcache.page_size=128M"
wsrep_cluster_address=gcomm://maria1
wsrep_cluster_name="MariaDB_Cluster"
wsrep_node_address="maria2"
wsrep_node_name="maria2"
wsrep_sst_method=xtrabackup
wsrep_sst_auth="root:MyR00tPasswd"
wsrep_node_incoming_address=192.168.10.102
wsrep_sst_receive_address=192.168.10.102
wsrep_slave_threads=16

maria3:

# wsrep provider configuration
wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_provider_options="gcache.size=256M; gcache.page_size=128M"
wsrep_cluster_address=gcomm://maria1
wsrep_cluster_name="MariaDB_Cluster"
wsrep_node_address="maria3"
wsrep_node_name="maria3"
wsrep_sst_method=xtrabackup
wsrep_sst_auth="root:MyR00tPasswd"
wsrep_node_incoming_address=192.168.10.103
wsrep_sst_receive_address=192.168.10.103
wsrep_slave_threads=16

 

Initialize the Cluster

1. Login to maria1 to initialize the cluster by restarting the service:

$ sudo service mysql restart

2. Login to maria2 and maria3 and restart MySQL. This will allow maria2 and maria3 to perform SST from maria1:

$ sudo service mysql restart

3. Login to MariaDB console and check for following values:

MariaDB [(none)]> show status like 'wsrep_%';

And you should see following output:

| wsrep_local_state | 4 |
| wsrep_local_state_comment | Synced |
| wsrep_incoming_addresses | 192.168.10.101:3306,192.168.10.102:3306,192.168.10.103:3306 |
| wsrep_cluster_size | 3 |
| wsrep_ready | ON |

You could now point your web server or application server to one of MariaDB cluster node to query.

 

Notes

Once the cluster successfully initialized and connected, you should change the wsrep_cluster_address to other nodes in case if the server rebooted. So, login to respective server and change the wsrep_cluster_address to following value:

wsrep_cluster_address=gcomm://maria1,maria2,maria3

Fix Windows MBR using Ubuntu Live CD (USB)

I have a user which having problem after removing CentOS partition (using partition manager) in a dual boot PC with Windows Vista. The problem rise after that and Windows Vista simply unable to boot due to missing MBR. Since I have no Windows Vista installation disc anymore with me, I need to use some other way in order to fix the MBR.

Luckily I have the Ubuntu Live CD ISO which I have downloaded several days ago. The idea is boot into LiveCD, use a tool called ms-sys in Ubuntu and fix the MBR right away.

1. Get Ubuntu Desktop Live CD from Ubuntu download page here: http://www.ubuntu.com/download.

2. Download UNetbootin from here. We will use this application to burn our ISO into flash drive.

3. Prepare our flash drive. Format it to FAT32 file system. Since I am using Windows 7, just right click to the drive and click ‘Format’.

4. Launch UNetbootin and select ‘Diskimage’. Locate the ISO file in your PC and click OK as screenshot below:

 

5. Download ms-sys from http://ms-sys.sourceforge.net/ . I will the most stable version which is  2.2.1. Extract it and put it into the flash drive together with the Ubuntu Live CD as screenshot below:

 

6. Now we have enough tools to start the recovering process. Go to the problematic PC and boot from USB. Select “Try Ubuntu without installing” and make sure Ubuntu Live CD is booted up until the desktop page.

 

7. Open terminal. Go to Dashboard icon and type “terminal” to open it. We need to copy ms-sys directory into the local disk and build it:

$ cp /cdrom/ms-sys ~ -Rf
$ cd ~/ms-sys
$ sudo make

8. Run following command to analyze your disk partition:

$ sudo fdisk -l | grep /dev
Disk /dev/sda: 160.0 GB, 160041885696 bytes
/dev/sda1              16    12288527     6144256   12  Compaq diagnostics
/dev/sda2   *    12288528    96175455    41943464    7  HPFS/NTFS/exFAT
/dev/sda3        96175456   312577823   108201184    7  HPFS/NTFS/exFAT
Disk /dev/sdb: 4007 MB, 4007657472 bytes
/dev/sdb1   *         128     7827455     3913664    b  W95 FAT32

The Windows installation is located in the disk that have NTFS. In this case, it will be /dev/sda.

9. Navigate to the bin directory under ms-sys as below and install the MBR using following command:

$ cd ~/ms-sys/bin
$ sudo ./ms-sys --mbrvista /dev/sda

Done! You can now reboot the PC and remove the USB drive. Your Windows Vista should able to load after that.

Notes

Other options that can be use with ms-sys:

$ ./ms-sys 
Usage:
	./ms-sys [options] [device]
Options:
    -1, --fat12     Write a FAT12 floppy boot record to device
    -2, --fat32nt   Write a FAT32 partition NT boot record to device
    -3, --fat32     Write a FAT32 partition DOS boot record to device
    -4, --fat32free Write a FAT32 partition FreeDOS boot record to device
    -5, --fat16free Write a FAT16 partition FreeDOS boot record to device
    -6, --fat16     Write a FAT16 partition DOS boot record to device
    -l, --wipelabel Reset partition disk label in boot record
    -p, --partition Write partition info (hidden sectors, heads and drive id)
                    to boot record
    -H, --heads <n> Manually set number of heads if partition info is written
    -7, --mbr7      Write a Windows 7 MBR to device
    -i, --mbrvista  Write a Windows Vista MBR to device
    -m, --mbr       Write a Windows 2000/XP/2003 MBR to device
    -9, --mbr95b    Write a Windows 95B/98/98SE/ME MBR to device
    -d, --mbrdos    Write a DOS/Windows NT MBR to device
    -s, --mbrsyslinux    Write a public domain syslinux MBR to device
    -z, --mbrzero   Write an empty (zeroed) MBR to device
    -f, --force     Force writing of boot record
    -h, --help      Display this help and exit
    -v, --version   Show program version
    -w, --write     Write automatically selected boot record to device
 
    Default         Inspect current boot record

This tutorial is also applicable to fix MBR for other Windows platform as stated in the command options above.

Ubuntu: Install Windows Application using Winetricks

Ubuntu is surely good for end-user operating system but sometimes we still need to have Windows application more. Using Wine, we can run Windows applications in UNIX/LINUX environment. If you are using Ubuntu, installing Wine is just simple if you are using Ubuntu Software Center. Just search for ‘Wine’ and then install.

In order to run Windows application correctly, we need to install some of the Windows-only requirement like .NET Framework, windows-based fonts and some of Windows libraries (dll) file. We can achieve this by using Winetricks, an extension built for Wine to help installing Windows application in a better way (other than manually download and execute).

Variable that I used is:

OS: Ubuntu 10.04 (Lucid)
User: mikey
Wine location: /home/mikey/.wine or ~/.wine

1. Download and install Winetricks:

$ cd ~/Downloads
$ wget http://winetricks.org/winetricks
$ chmod +x winetricks
$ sudo cp winetricks /usr/local/bin

2. We can browse around on categories that Winetricks could do for us:

$ winetricks apps list
$ winetricks benchmarks list
$ winetricks dlls list
$ winetricks fonts list
$ winetricks games list
$ winetricks settings list

3. In this case, I will start by installing .NET Framework 2.0:

$ winetricks dotnet20

4. To install many applications, we can run following command:

$ winetricks corefonts vcrun6 mfc40 vb6run msxml6

5. To remove all applications which has been installed by Winetricks and re-initialize Wine:

$ rm -Rf ~/.wine
$ wineboot

 

Debian – Update Source List

If you are a Debian administrator, its advisable to run apt-get update before proceed to install any package to make sure that you can get connected to the mirror and the installation will run smoothly with latest package list. Sometimes, you will see following error when trying to update:

W: Some index files failed to download, they have been ignored, or old ones used instead.

This means that the mirror provider which has been setup in your source list is down, or unreachable, or you have routing problem between you and mirror server and any related connectivity problem. So, we need to change the sources list and provide another mirror server which you can get from Debian website, http://www.debian.org/mirror/list . In my case, I will use Japan mirror.

1. Login to the server via SSH/console and open /etc/apt/sources.list via text editor:

[email protected]: ~# nano /etc/apt/sources.list

2. Edit the file following example below. Since I will use Japan mirror, my sources list will be like this:

#
 
# deb cdrom:[Debian GNU/Linux 6.0.1a _Squeeze_ - Official amd64 NETINST Binary-1 20110320-15:00]/ squeeze main
 
#deb cdrom:[Debian GNU/Linux 6.0.1a _Squeeze_ - Official amd64 NETINST Binary-1 20110320-15:00]/ squeeze main
 
deb http://ftp.jp.debian.org/debian/ squeeze main non-free contrib
deb-src http://ftp.jp.debian.org/debian/ squeeze main non-free contrib
 
deb http://security.debian.org/ squeeze/updates main contrib non-free
deb-src http://security.debian.org/ squeeze/updates main contrib non-free
 
deb http://ftp.jp.debian.org/debian/ squeeze-updates main non-free contrib
deb-src http://ftp.jp.debian.org/debian/ squeeze-updates main non-free contrib

3. Update the package list from new provider:

[email protected]: ~# apt-get update

After you run that, make sure you will see no error at the end of the output. You should able to do apt-get installation without any problem anymore.

Using Screen to Manage Multiple Displays in one SSH Session

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Screen is very useful to help you administer server via SSH or console. It allows multiple shell session to be controlled by single SSH session.

As server administrator, Screen usually being used to:

  • Create multiple windows with each of the window is running different command
  • Create 2 windows. One window managing local server, another one is managing other remote server
  • Share window with other user remotely. They can see what you type and run in the shell via SSH

Lets install Screen first via SSH (in this example I will use Putty) and I will show you on how to use and take advantages by having this application installed:

RedHat/CentOS/Fedora:

yum install screen -y

Ubuntu/Debian:

apt-get install screen

FreeBSD:

cd /usr/ports/sysutils/screen
make
make install clean

notes: (on FreeBSD, they might have some prompt, just accept the default value)

Installation done. I will show some example so you can follow and understand on how we should use Screen:

1. Type following command to start screen:

[[email protected] ~]# screen

2. You have enter first window of your screen. Run following command:

[[email protected] ~]# top

3. Now, press ‘Ctrl-A-C’ in the keyboard. This will create another window next to it. Run following command afterwards:

[[email protected] ~]# netstat -na

4. Now, press another ‘Ctrl-A-C’ in the keyboard. This will create another windows next to 2nd window (the netstat window). Run following command:

[[email protected] ~]# dmesg

Continue reading “Using Screen to Manage Multiple Displays in one SSH Session” »

Debian/Ubuntu – Make SSH Terminal Run with Colors

For those who actually get used to have colors in SSH terminal session, will surely feels weird if you SSH into Debian/Ubuntu because by default, the terminal session will NOT run with color-enabled mode. It might be small issue but it will help you a lot in administrating and managing the Linux box when you can see and differentiate things with colors.

What you need to do is replace following content into /root/.bashrc or /home/any_user/.bashrc file (this is actually an extended version with some tweak for your .bashrc file to enable colors):

Continue reading “Debian/Ubuntu – Make SSH Terminal Run with Colors” »