Subscribe to RSS Feed

This is guide howto install nginx, PHP 5.3 and FastCGI webserver with MySQL and/or PostgreSQL and Memcache support on CentOS 5.4, Red Hat RHEL 5.4 and Fedora.

nginx (engine x) is a robust, small and high performance http server, reverse proxy server and also mail proxy server.

1. Add and enable needed repositories:

Updated 19.3.2010

Use following repositories to install nginx 0.8.xx version (currently 0.8.34 version)

rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
 
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm
 
rpm -Uvh http://yum.chrislea.com/centos/5/i386/chl-release-5-3.noarch.rpm
 
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CHL

Just Epel repository is now obsolete, since there can be found only nginx 0.6.xx branch (currenlty version 0.6.39) Check this comment for more info.

rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm

2. Install following packages:

  • nginx
  • php
  • php-mysql
  • php-pgsql
  • php-pecl-memcache
  • fcgi
  • spawn-fcgi

with following command:

yum --enablerepo=remi install nginx php php-mysql php-pgsql php-pecl-memcache fcgi spawn-fcgi
 
...
 
Dependencies Resolved
 
=================================================================================================
 Package                     Arch           Version                         Repository      Size
=================================================================================================
Installing:
 fcgi                        i386           2.4.0-4.el5                     epel            43 k
 nginx                       i386           0.6.39-2.el5                    epel           318 k
 php                         i386           5.3.1-1.el5.remi                remi           1.3 M
 php-mysql                   i386           5.3.1-1.el5.remi                remi           140 k
 php-pecl-memcache           i386           3.0.4-2.el5.remi.1              remi            64 k
 php-pgsql                   i386           5.3.1-1.el5.remi                remi           119 k
 spawn-fcgi                  i386           1.4.22-2.el5                    epel            17 k
 
Transaction Summary
=================================================================================================
Install      7 Package(s)         
Update       0 Package(s)         
Remove       0 Package(s)

3. Add Fast-CGI init script:

Save following script to /etc/init.d/phpfcgi file.

#!/bin/bash
#
# Startup script for the PHP FastCGI server.
#
# chkconfig: 345 85 15
# description: PHP is an HTML-embedded scripting language
# processname: php
# config: /etc/php.ini
 
# Source function library.
. /etc/rc.d/init.d/functions
 
SPAWNFCGI="/usr/bin/spawn-fcgi" 
PHPFCGI="/usr/bin/php-cgi"
FCGIPORT="9000"
FCGIADDR="127.0.0.1"
PHP_FCGI_CHILDREN=5
PHP_FCGI_MAX_REQUESTS=1000
ALLOWED_ENV="PATH USER"
USER=nginx
GROUP=nginx
#PHPUSER=php
PIDFILE=/var/run/phpfcgi.pid
 
if [ -z "$PHP_FCGI_CHILDREN" ]; then
  PHP_FCGI_CHILDREN=5
fi
 
ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS"
 
case "$1" in
  start)
        PHPFCGI_START=$"Starting ${NAME} service: "
        echo -n $PHPFCGI_START
 
        # check for $PHPUSER, create if non-existent
        if [ -z "`id -u $PHPUSER 2> /dev/null`" ]; then
            useradd -s /sbin/nologin $PHPUSER
        fi
 
        # clean environment
        E=
        for i in $ALLOWED_ENV; do E="$E $i=${!i}"; done
        #daemon --user $PHPUSER --pidfile $PIDFILE "env - $E $PHPFCGI -q -b $FCGIADDR:$FCGIPORT &> /dev/null &"
	daemon $SPAWNFCGI -a ${FCGIADDR} -p ${FCGIPORT} -u ${USER} -g ${GROUP} -P ${PIDFILE} -C ${PHP_FCGI_CHILDREN} -f ${PHPFCGI}
    retval=$?
 
        #pid=`pidof php-cgi`
        #if [ -n "$pid" ]; then
        #    echo $pid > $PIDFILE
        #    success $PHPFCGI_START
        #else
        #    failure $PHPFCGI_START
        #fi
        #echo
        ;;
  stop)
        echo -n "Stopping php-fcgi: "
        killproc -p $PIDFILE phpfcgi
        echo
        ;;
  status)
        status phpfcgi
        ;;
  restart)
        $0 stop
        $0 start
        ;;
  *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
esac
 
exit 0

Originally from here: http://wiki.nginx.org/RedHatPHPFCGIInitScript, but modified little bit, because original script not worked.

Now normal start, stop, status, restart should work:

/etc/init.d/phpfgci start
/etc/init.d/phpfgci stop
/etc/init.d/phpfgci status
/etc/init.d/phpfgci restart

4. Configure nginx:

Modify /etc/nginx/nginx.conf file following rows:

worker_processes  2;
 
keepalive_timeout  25;
 
gzip  on;
 
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
	root           /usr/share/nginx/html;
	fastcgi_pass   127.0.0.1:9000;
	fastcgi_index  index.php;
	fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
	include        fastcgi_params;
}
 
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
	deny  all;
}

5. Start FastCGI and nginx services:

/etc/init.d/phpfcgi start
/etc/init.d/nginx start

6. Create PHP file and print phpinfo:

echo "<?php echo phpinfo(); ?>" > /usr/share/nginx/html/info.php

7. Test info.php file:

Go to http://127.0.0.1/info.php and check that everything is working.

Bookmark & Share

Wondering what to do next?

Grab My Feed

Subscribe to RSS Feed

Catch Me On Twitter

Catch Me On Twitter!

Sponsors


9 Responses to “ Install nginx, PHP 5.3 and FastCGI webserver with MySQL, PostgreSQL and Memcache support on CentOS 5.4, Red Hat RHEL-5.4, Fedora ”

  1. Nnyan
    December 30, 2009 at 9:17 pm

    How about if I wanted to install Nginx 0.8.31?

  2. JR
    December 31, 2009 at 1:16 pm

    If you want use development versions then best way is building nginx from source. Here is quick guide to build nginx 0.8.31 from source:

    # Install following packages
    yum install gcc openssl-devel pcre-devel zlib-devel
     
    # Download nginx-0.8.31.tar.gz
    wget http://www.nginx.org/download/nginx-0.8.31.tar.gz
     
    # Untar and unzip nginx-0.8.31.tar.gz 
    tar xvfz nginx-0.8.31.tar.gz
     
    # Move to nginx-0.8.31 directory
    cd nginx-0.8.31
     
    # Configure nginx
    # (Configure options for CentOS, 
    #  Red Hat, Fedora from nginx wiki)
    ./configure \
      --prefix=/usr \
      --sbin-path=/usr/sbin/nginx \
      --conf-path=/etc/nginx/nginx.conf \
      --error-log-path=/var/log/nginx/error.log \
      --pid-path=/var/run/nginx/nginx.pid  \
      --lock-path=/var/lock/nginx.lock \
      --user=nginx \
      --group=nginx \
      --with-http_ssl_module \
      --with-http_flv_module \
      --with-http_gzip_static_module \
      --http-log-path=/var/log/nginx/access.log \
      --http-client-body-temp-path=/var/tmp/nginx/client/ \
      --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
      --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ 
     
    # Build nginx
    make
     
    # Install nginx as root
    make install

    And then install other needed stuff, configure webserver and test.

  3. Mark
    January 28, 2010 at 2:46 pm

    Maybe im missing something, but it doesnt seem like mysql was installed like the title implies. I cant even install mysql now through yum either. I get the follow error and a few others just like it, but more just referrence language files:

    file /etc/my.cnf from install of mysql-5.0.77-4.el5_4.1.i386 conflicts with file from package mysql-libs-5.1.42-1.el5.remi.i386

  4. JR
    January 28, 2010 at 3:42 pm

    Hi Mark,

    Actually this manual will guide only in how to install nginx, FastCGI and PHP with MySQL, PostgreSQL and Memcache support. So after this setup you should get PHP+nginx server with MySQL, PostgreSQL and Memcache support, but not MySQL, PostgreSQL or Memcache servers installed.

    But your problem with MySQL install. If you want install MySQL 5.1.42 then try to install it with following command:

    yum --enablerepo=remi install mysql mysql-server
  5. JR
    February 15, 2010 at 11:31 am

    Maybe its better try downgrade back to MySQL version 5.0.77, simply remove following packages (if you have installed those):

    yum remove mysql mysql-libs mysqlclient15

    Then install mysql with remi repository disabled:

    yum install --disablerepo=remi mysql

    This should install mysql-5.0.77-4.el5_4.1 on your system.

  6. Nik
    February 15, 2010 at 9:46 am

    This fixed the original problem.

    Now the error is “ERROR] Error message file '/usr/share/mysql/english/errmsg.sys' had only 480 error messages,
    but it should contain at least 641 error messages.” when starting mysqld.

    Is something messed up in the mysql packages on that repo?

  7. unblocked
    February 26, 2010 at 7:24 am

    I love the tips on this site, they are always to the point and just the information I was looking for. Its hard to find good content these days in the world of spam and garbage sites. [Removed email address]

  8. Will
    March 19, 2010 at 12:35 am

    There are critical bugs in nginx in the version used in the EPEL repository (0.6.39).

    http://nginx.org/en/security_advisories.html

    According to nginx’s site they affect most version up until the most recent 0.7 and 0.8 branches which have been fixed.You are way, way better off from a security standpoint building nginx from scratch using the latest 0.7 release.

Leave a Reply