Friday, July 1, 2011

Install PHP 5.3.6 with PHP-FPM, MySQL and NginX 1.04 on Centos 5.6

If you want to host a web application like Wordpress or any kind of PHP app with database requirements without spending an outrageous amount of money on a server, a LEMP server (Linux, Engine X, MySQL, PHP) is the way to go. You can use this setup on servers with memory as low as 100MB.

If you want to host only 1 blog/dynamic site with moderate traffic for example, I would recommend at least 100Mb but if you want to host multiple blogs/dynamic sites, say, like 5 with moderate traffic, I'd recommend at least 512MB of memory. It all depends on the traffic and the way you setup the web server and FastCGI process manager but anyway, if your server keeps on going down due to lack of memory, just upgrade it!

This setup has been tested on a XEN VPS with 128MB of RAM with Centos 5.6 x86 installed. Although I'm using x86, this setup should work for other architectures as well; you just need to download packages for your architecture instead!

# Note: If you're on a GUI environment, you must set your init scripts to start on runlevel 5 instead of 3!

Now, let's get busy. First we'll just update our software packages:
yum update

We will now install some development tools and libraries that we'll need for compilation stuff.
yum groupinstall "Development Tools"
yum groupinstall "Development Libraries"

Let's install the MySQL server now. You can skip mysql-server package if you want to host your databases on another server to keep load on this server low! I usually do that on low memory servers.
yum install mysql mysql-server mysql-devel

Fire up MySQL daemon and configure mysql. Skip this step if you didn't install mysql-server.
service mysqld start

The installation is pretty self explanatory. You just need to set a root password and say yes everytime.

Set mysqld to start at boot.
chkconfig --levels 3 mysqld on

We will now install some more packages and compile NginX web server 1.0.4, the best web server ever!
cd /usr/local/src


tar -zxvf nginx-1.0.4.tar.gz

yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel

cd nginx-1.0.4

./configure --user=nginx --group=nginx --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --pid-path=/var/run/ --lock-path=/var/lock/subsys/nginx --with-


make install

We will now make an init script to enable NginX to start at boot.
cd /etc/init.d

vi nginx

Paste the text from this page in the file.

Make the script executable, create user nginx, fire up nginx and finally set it to start at boot.
chmod +x nginx

adduser nginx

service nginx start

chkconfig --levels 3 nginx on

You can now put your IP address in your web browser's address bar to see the NginX default index.html

We now proceed to installation of PHP 5.3.6
yum install zip gd gd-devel libjpeg libjpeg-devel libmcrypt libmcrypt-devel curl libpng libpng-devel

cd /usr/local/src


Before compiling PHP 5.3.6 we will need the following packages installed:
cd /tmp


rpm -Uvh autoconf-2.13-8.noarch.rpm


rpm -ivh libtool-ltdl-2.2.6-15.5.el5.1.i386.rpm

We can now compile PHP. We will use PHP-FPM to fire up PHP scripts with nginx.

cd /usr/local/src

tar -zxvf php-5.3.6.tar.gz

cd php-5.3.6

./buildconf --force

./configure --enable-zip --with-gd --with-jpeg-dir=/usr/lib --with-libxml-dir=/usr/lib --with-zlib-dir=/usr/lib --with-mcrypt --enable-gd-native-ttf --with-jpeg-dir=/usr/lib --with-curl=/usr/lib --enable-calendar --with-openssl --enable-magic-quotes --with-mysql=/usr/lib/mysql --with-mysqli=/usr/bin/mysql_config --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-config-file-scan-dir=/usr/local/lib/php --with-config-file-path=/etc/php

make all install

You can test your PHP installation if you want but this is completely optional and takes a much time to complete.
make test

Let's copy the init script for PHP-FPM to init.d directory
cp sapi/init.d.php-fpm /etc/init.d/php-fpm

cd /etc/init.d

chmod +x php-fpm

We will now configure PHP-FPM. If you want to host multiple websites on this server, skip this step! This step is for people who want to host like 1-2 websites only.
cd /usr/local/etc

mv php-fpm.conf.default php-fpm.conf

vi -w php-fpm.conf

Change the settings like that. Make sure you remove the ; (uncomment) for these lines:

pid = /usr/local/var/run/
error_log = log/php-fpm.log
log_level = notice
daemonize = yes
listen.owner = nginx = nginx
listen.mode = 0666

If you want to host multiple websites on this server, skip the following step! This step is for people who want to host like 1-2 websites only.

pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 5

Set PHP-FPM to start at boot
chkconfig --levels 3 php-fpm on

Make PHP configuration file
cd /etc

mkdir php

cd php

vi php.ini

Add the following line. Find your timezone from first and replace mine with yours:

date.timezone = "Australia/Queensland"

We will now configure nginx. Take a look at the wiki for information on virtual hosting and stuff.
vi /etc/nginx/nginx.conf

Modify as follows:

location ~ \.php$ {
root html;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_scrip$
include /etc/nginx/fastcgi_params;

Change worker_processes from 1 to the number of processors your server has.

Let's take a look at what we've got so far.
cd /usr/share/nginx/html

vi info.php
Paste the lines from the pastebin:

Now browse to the file from your web browser and you should see:

If you see the page, it means that PHP 5.3.6 has successfully been installed. You can now create virtual hosts for your domain names and install your favorite CMS.

No comments:

Post a Comment