How to compile PHP on a Linux Server

January 10th, 2012

Compiling PHP on a Linux server can be a bear of a project. The first time you try doing it, you might just frustrated and just want to install PHP with a RPM package. But you don’t get any flexibility with RPM packages, you just get the default install.

With a custom PHP install (compiling), you can pick which PHP options you want and don’t want. Do you need cURL (now default as of PHP 5.2+)? Using MySQL/phpMyAdmin? Then you will want to compile PHP with mcrypt and mbstring. These are just a couple of examples, but you will have plenty to choose from. We’ll learn about these options very soon…

So let’s start with the install. For my installation, I’m on a Red Hat Enterprise Linux 5 Server (RHEL5). So the paths/configurations I use are specific to that operating system (OS). I’m running MySQL 5+ and Apache 2+. For this installation you will need to have ssh access to your server. If you have a shared hosting account, you might have ssh access, but most likely you will not have the proper privileges to compile PHP. I’ll break this down into steps, to hopefully simplify the process.

  1. ssh to your server (if you don’t have an ssh client, try PuTTY. It’s free.)
  2. Become root on the server (if you have root access), by typing su - and then enter the root password. Or use sudo if that is an option.
  3. cd /usr/local/src
  4. In a browser go to php.net to get the source download URL (this changes depending on PHP version and the mirror location). In this example the download URL is http://us.php.net/get/php-5.3.8.tar.bz2/from/this/mirror
  5. Now download the file by typing wget http://us.php.net/get/php-5.3.8.tar.bz2/from/this/mirror
  6. Extract the PHP source files: tar xjvf php-5.3.8.tar.bz2
  7. cd php-5.3.8
  8. Now that you have the PHP source, and have extracted the files, you can find out what you’re install options are by typing:
    ./configure –h.
    You’ll get a really long list (a bit overwhelming) of compile options. You can dig through each of these options, or just go with what I’m going to do. The nice thing is that once you have compiled PHP once, it’s fairly painless to re-compile PHP with new options should you miss something.
  9. So here’s my configure command (notice that this is specific to 64bit RHEL). Note you should copy this into Notepad or similar to make sure all of the line breaks are ok:
    ./configure –build=x86_64-redhat-linux-gnu –host=x86_64-redhat-linux-gnu –target=x86_64-redhat-linux-gnu –program-prefix= –prefix=/usr –exec-prefix=/usr –bindir=/usr/bin –sbindir=/usr/sbin –sysconfdir=/etc –datadir=/usr/share –includedir=/usr/include –libdir=/usr/lib64 –libexecdir=/usr/libexec –localstatedir=/var –sharedstatedir=/usr/com –mandir=/usr/share/man –infodir=/usr/sh
    are/info –cache-file=../config.cache –with-libdir=lib64 –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –disa
    ble-debug –with-pic –disable-rpath –with-pear –with-bz2 –with-curl –with-exec-dir=/usr/bin –with-freetype-dir=/usr –with-
    png-dir=/usr –enable-gd-native-ttf –without-gdbm –with-gettext –with-gmp –with-iconv –with-jpeg-dir=/usr –with-openssl –with
    -pspell –with-pcre-regex=/usr –with-zlib –with-layout=GNU –enable-exif –enable-ftp –enable-magic-quotes –enable-sockets –ena
    ble-sysvsem –enable-sysvshm –enable-sysvmsg –enable-wddx –with-kerberos –enable-ucd-snmp-hack –with-unixODBC=shared,/usr –enable-shmop –enable-calendar –with-mime-magic=/usr/share/file/magic.mime –without-sqlite –with-libxml-dir=/usr –enable-force-cgi-
    redirect –enable-pcntl –with-imap=shared –with-imap-ssl –enable-mbstring=shared –enable-mbregex –with-ncurses=shared –with-gd
    =shared –enable-bcmath=shared –enable-dba=shared –with-db4=/usr –with-xmlrpc=shared –with-ldap=shared –with-ldap-sasl –with-m
    ysql=shared,/usr –with-mysqli=shared,/usr/lib64/mysql/mysql_config –enable-dom=shared –with-pgsql=shared –with-snmp=shared,/usr
    –enable-soap=shared –with-xsl=shared,/usr –enable-xmlreader=shared –enable-xmlwriter=shared –enable-fastcgi –enable-pdo=shared
    –with-pdo-odbc=shared,unixODBC,/usr –with-pdo-mysql=shared,/usr/lib64/mysql/mysql_config –with-pdo-pgsql=shared,/usr –with-pdo-sqlite=shared,/usr –enable-dbase=shared –enable-mbstring –with-mcrypt=/usr/local/bin/mcrypt –with-apxs2=/usr/sbin/apxs
  10. Note that you will most likely get a lot of errors after executing this command, but no need to panic. In most cases, the packages you are installing are dependent on “devel” packages. For example, you might get this error: Error: configure: error: Cannot find pspell. To fix this you need to install the pspell devel package: yum install aspell-devel. Here are a couple of sites that will help with dependencies: http://www.lifelinux.com/error-compiling-php-on-centos-x64/ and http://www.mickgenie.com/blog/php-compilation-error/
  11. So you’ll need to go back and forth between installing devel packages with yum, and then running your configure command again. Once your configure command has run successfully, move on to the next step.
  12. Now type make
  13. Then make test (really important to do this step)
  14. Finally make install
  15. Optional step: libtool –finish /usr/local/src/php-5.3.8/libs
  16. cp all modules from /usr/local/src/php-5.3.8/modules/*.so to /usr/lib64/php/modules/
  17. cp /usr/local/src/php-5.3.8/php-recommended to /etc/php.ini (this file contains configuration settings, like file upload size limits)
  18. edit /etc/php.ini to set modules path to /usr/lib64/php/modules/
  19. edit /etc/php.ini to set include_path for pear (if you installed pear)
  20. Now restart Apache: /usr/sbin/apachectl restart
  21. To test your php install, you should do two things:
    1. Type php -v (you should list out your new PHP version, in this case PHP 5.3.8)
    2. Go to your document root, and create a php file with this code: <?php phpinfo(); ?>, then load that file in a browser. This will give you a full configuration listing on PHP, Apache and MySQL
So hopefully everything worked out for you. There are many things that can go (temporarily) wrong with PHP compiles. But once you successfully compile PHP once, you’ll have a better handle on how to do it in the future.

php

Reset auto increment index in MySQL

February 28th, 2011

To reset the auto increment value back to “1″  in the “mytable” database table:

ALTER TABLE mytable AUTO_INCREMENT=1

database, mysql

How to migrate WordPress

February 28th, 2011

How to migrate WordPress to a different Linux server.

First migrate the database (use database name “wpdb” for this example):

  1. User either phpMyAdmin or mysql command line to do a database export (export both data and structure). Here’s a mysql command line exampl example:
    # mysql -u username -p wpdb > /home/john/wpdb.sql

    After you are prompted for “username’s” password, the contents of the database will be dumped to /home/john/wpdb.sql

  2. Create the “wpdb” database on the new server, and then import the data: mysql -u username -p wpdb < wpdb.sql
  3. Set up a new database user for this database. It needs to be the same username/password you have in your wp-config.php WordPress file.
  4. From the linux command line, grant privileges for this user to use the “wpdb” database:GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON wpdb.* TO ‘username’@'localhost’ IDENTIFIED BY ‘password’;(you may need to also reload the mysql GRANT tables by typing: FLUSH PRIVILEGES;)
  5. Now update the database entries:UPDATE wp_options SET option_value = replace(option_value, ‘http://old-domain.com’, ‘http://new-domain.com’) WHERE option_name = ‘home’;UPDATE wp_options SET option_value = replace(option_value, ‘http://old-domain.com’, ‘http://new-domain.com’) WHERE option_name = ‘siteurl’;UPDATE wp_posts SET guid = replace(guid, ‘http://old-domain.com’,”);UPDATE wp_posts SET post_content = replace(post_content, ‘http://old-domain.com’, ”);
  6. Using the Worpress Dashboard, update the upload directory by going to Settings–>Miscellaneous Settings

Next migrate the site files:

  1. cd to the document root of the WordPress site
  2. tar -cvf myWordpressSite.tar *
  3. scp or ftp the tar file to your new server
  4. cd to the document root on the new server
  5. tar -xvf myWordpressSite.tar

migration, wordpress

Add a new system user on Solaris 9

February 28th, 2011

Here’s how to add a new system user “john” on Solaris 9:

  1. login as root
  2. useradd -g modern -m -d /export/home/john -s /usr/bin/bash john
  3. now create a password: passwd john

linux/unix, solaris

Get webserver info from Linux command line

February 28th, 2011

Here’s how you can get basic webserver info for another server. Please note that this won’t work if the website uses a CDN.

# telnet www.example.com 80
Trying 216.44.201.135…
Connected to www.litaxforum.com (216.44.201.135).
Escape character is ‘^]’.

/* NOW TYPE THIS: */
GET / HTTP/1.1
/* THEN HIT ENTER TWICE AND YOU GET ALL INFO */

apache, linux/unix, web server

Update Red Hat or Fedora DNS

February 28th, 2011

To update DNS on a Fedora or Red Hat server:

If NetworkManager is running, you need to edit:

/etc/sysconfig/network-scripts/ifcfg-eth0 by changing the DNS entry

on reboot the above will update /etc/resolv.conf

If there is no network manager, then you just need to edit:

/etc/resolv.conf

fedora, linux/unix, red hat

How to shutdown a RHEL5 server

October 18th, 2010

As root, use this command:

/sbin/shutdown -h now

After shutting everything down, the -h option halts the machine, or alternatively
the -r option reboots

If the computer does not power itself down, be careful not to turn off the computer until a message appears indicating that the system is halted (I believe you will see an “ok” prompt). Failure to wait for this message can mean that not all the hard drive partitions are unmounted, which can
lead to file system corruption.

fedora, linux/unix

Upgrade Fedora 12 to Fedora 13

October 14th, 2010

This upgrade was not as simple as upgrading from 10 to 11 or 11 to 12 (I used the yum/preupgrade update method for both of those).

Upgrading F12 to 13 was more involved. My /boot partition was not big enough to complete the installation via yum/preupgrade (F13 wants 500Mb /boot partition). Instead I had to switch repositories to complete the upgrade. Here’s the steps (please note this is on a i386 machine):

  1. Since my /boot partition was limited, I have to follow these directions for force the the download of the installer: http://fedoraproject.org/wiki/PreUpgrade#Method_2:_Trick_preupgrade_into_downloading_the_installer
  2. yum update rpm
  3. yum -y update
  4. yum clean all
  5. wget ftp://download.fedora.redhat.com/pub/fedora/linux/releases/13/Fedora/i386/os/Packages/fedora-release-*.noarch.rpm
  6. rpm -Uvh fedora-release-*.noarch.rpm
  7. Now get out of the graphical interface:  ctrl + alt + F2
  8. login as root
  9. telinit 3
  10. yum upgrade
  11. yum groupupdate Base
  12. /sbin/grub-install –recheck /dev/sda
  13. /sbin/grub-install /dev/sda
  14. cd /etc/rc.d/init.d; for f in *; do /sbin/chkconfig $f resetpriorities; done
  15. reboot

fedora, linux/unix , ,

Upgrade Fedora 11 to Fedora 12

October 14th, 2010

This upgrade was pretty straight forward for me. I used the “preupgrade-cli” method. Here’s the step:

  1. su -
  2. yum update rpm
  3. yum -y update
  4. yum -y install preupgrade
  5. yum clean all
  6. preupgrade-cli “Fedora 12 (Constantine)”
  7. Once you have gone through the preupgrade, you will be asked to reboot: reboot

Upgrading from Fedora 10 to 11 requires the same steps, except changing step 6 to the appropriate version info.

Upgrading from Fedora 12 to 13 was more involved for me, since the /boot partition of the PC was too small. I used this method for that upgrade: http://techblog.deeblake.com/?p=95

fedora, linux/unix ,

Upgrade Fedora 10 to Fedora 11

October 14th, 2010

This upgrade was pretty straight forward for me. I used the “preupgrade-cli” method. Here’s the steps:

  1. su -
  2. yum update rpm
  3. yum -y update
  4. yum -y install preupgrade
  5. yum clean all
  6. preupgrade-cli “Fedora 11 (Leonidas)”
  7. Once you have gone through the preupgrade, you will be asked to reboot: reboot
  8. After reboot you will be lead through the actual upgrade.

I found that after my upgrade I had no internet connection. I fixed this through the GUI by clicking System->Administration->Network and then checking “Activate device when computer starts”. Then you will need to restart network services.

Upgrading from Fedora 11 to 12 requires the same steps, except changing step 6 to the appropriate version info.

Upgrading from Fedora 12 to 13 was more involved for me, since the /boot partition of the PC was too small. I used this method for that upgrade: http://techblog.deeblake.com/?p=95

fedora, linux/unix ,