Nginx+PHP(FPM)+MySQL+Memcache+eAccelerator on Debian6

I recently rebuild my linux image on Linode, so I have to reinstall the LEMP stack again from ground. Last time was probably more than a year ago, with the help of many tutorials from google. So this time I decide to write what I did step by step for my reference in the future.

I am using Debian 6 32bit with linux kernel 3.0.4. After first log in to your debian, first you want to add Dotdeb repository to the system repository list. Dotdeb is a repository containing packages of stable and latest LAMP or LEMP servers and their many useful extensions. To add that

  1. vi /etc/apt/sources.list
vi /etc/apt/sources.list

Add following two lines to the bottom of the file

  1. deb http://packages.dotdeb.org stable all
  2. deb-src http://packages.dotdeb.org stable all
deb http://packages.dotdeb.org stable all
deb-src http://packages.dotdeb.org stable all

And then fetch the appropriate GnuPG key

  1. wget http://www.dotdeb.org/dotdeb.gpg
  2. cat dotdeb.gpg | apt-key add -
wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | apt-key add -

After that run

  1. apt-get update
  2. apt-get upgrade
apt-get update
apt-get upgrade

Now we are ready to install the LEMP stack packages.

1. Nginx
Since the dotdeb repository we just added already have the latest stable version of nginx 1.0.6, we don’t have to compile it from the official source, just run

  1. aptitude install nginx
aptitude install nginx

That’s it. The default location for the conf files is /etc/nginx/ and there will be a init.d script /etc/init.d/nginx to start and stop nginx.
We would want nginx start each time the system boots, run the command

  1. update-rc.d nginx defaults
update-rc.d nginx defaults

2. PHP with FPM
PHP-FPM (PHP FastCGI Process Management) is a patch for PHP to improve PHP’s FastCGI capabilities and administration. It has been bundled with PHP since the PHP 5.3.3 so we don’t need to install it besides the PHP5

  1. aptitude -y install php5-cli php5-common php5-suhosin php5-fpm
aptitude -y install php5-cli php5-common php5-suhosin php5-fpm

This only php5 with fpm, I need to add some more php extensions

  1. aptitude -y install php5-mysql php5-gd php5-curl php5-pear php5-mcrypt php5-memcache
aptitude -y install php5-mysql php5-gd php5-curl php5-pear php5-mcrypt php5-memcache

Next to start nginx and php5-fpm if they are not already started.

  1. /etc/init.d/nginx start
  2. /etc/init.d/php5-fpm start
/etc/init.d/nginx start
/etc/init.d/php5-fpm start

Following is to configure nginx to redirect all php request to php-fpm to process

  1. vi /etc/nginx/sites-available/default
vi /etc/nginx/sites-available/default

The default www directory of nginx is /etc/share/nginx/www, if you still want it there, no need to change anything. I changed the directory to /var/www so I have to create them.

  1. mkdir /var/www
  2. mkdir default
  3. chown -R www-data:www-data /var/www
  4. chown -R www-data:www-data /var/www/*
mkdir /var/www
mkdir default
chown -R www-data:www-data /var/www
chown -R www-data:www-data /var/www/*

My version looks like below

Note: I use this conf file only for the ip access to the site, not combined with any domain. I will create other conf file for each site later.

  1. server {
  2.     listen   80; ## listen for ipv4; this line is default and implied
  3.     #listen   [::]:80 default ipv6only=on; ## listen for ipv6
  4.  
  5.     root /var/www/default; # this is the web root directory of this site
  6.     index index.html index.htm index.php;
  7.  
  8.     # Make site accessible from http://localhost/
  9.     server_name 72.14.185.108; # if you wanna use a domain to access,
  10.     # add domains after separated by space like yangqi.org *.yangqi.org
  11.  
  12.     #location / {
  13.         # First attempt to serve request as file, then
  14.         # as directory, then fall back to index.html
  15.     #   try_files $uri $uri/ /index.html;
  16.     #}
  17.  
  18.     location /doc {
  19.         root /usr/share;
  20.         autoindex on;
  21.         allow 127.0.0.1;
  22.         deny all;
  23.     }
  24.  
  25.     location /images {
  26.         root /usr/share;
  27.         autoindex off;
  28.     }
  29.  
  30.     #error_page 404 /404.html;
  31.  
  32.     # redirect server error pages to the static page /50x.html
  33.     #
  34.     error_page 500 502 503 504 /50x.html;
  35.     location = /50x.html {
  36.         root /usr/share/nginx/www;
  37.     }
  38.  
  39.     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  40.     # I used unix socket to pass the request which is in the memory and faster
  41.     location ~ \.php$ {
  42.         fastcgi_pass /dev/shm/nginx.socket;
  43.         include fastcgi_params;
  44.     }
  45.  
  46.     # deny access to .htaccess files, if Apache's document root
  47.     # concurs with nginx's one
  48.     #
  49.     location ~ /\.ht {
  50.         deny all;
  51.     }
  52. }
server {
	listen   80; ## listen for ipv4; this line is default and implied
	#listen   [::]:80 default ipv6only=on; ## listen for ipv6

	root /var/www/default; # this is the web root directory of this site
	index index.html index.htm index.php;

	# Make site accessible from http://localhost/
	server_name 72.14.185.108; # if you wanna use a domain to access,
     # add domains after separated by space like yangqi.org *.yangqi.org

	#location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to index.html
	#	try_files $uri $uri/ /index.html;
	#}

	location /doc {
		root /usr/share;
		autoindex on;
		allow 127.0.0.1;
		deny all;
	}

	location /images {
		root /usr/share;
		autoindex off;
	}

	#error_page 404 /404.html;

	# redirect server error pages to the static page /50x.html
	#
	error_page 500 502 503 504 /50x.html;
	location = /50x.html {
		root /usr/share/nginx/www;
	}

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	# I used unix socket to pass the request which is in the memory and faster
	location ~ \.php$ {
		fastcgi_pass /dev/shm/nginx.socket;
        include fastcgi_params;
	}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /\.ht {
		deny all;
	}
}

Now if you create a simple php file index.php in directory /var/www/default

  1. <?php echo phpinfo();?>
<?php echo phpinfo();?>

When you visit the ip address of your server you should be able to see the phpinfo page.

3.MySQL
This one is easy, just run

  1. aptitude -y install mysql-server mysql-client
aptitude -y install mysql-server mysql-client

4.Memcache

  1. aptitude -y install memcached php5-memcache
aptitude -y install memcached php5-memcache

For the configuration of those applications, I will create new posts for each one with detailed information.

5.eAccelerator
For this one, we are gonna have to install it from the official source file. The current latest version is 0.9.6.1. Before compiling, you have to install php5-dev

  1. aptitude -y install php5-dev build-essential
aptitude -y install php5-dev build-essential

We are downloading from Sourceforge, the link is
http://sourceforge.net/projects/eaccelerator/files/eaccelerator/eAccelerator%200.9.6.1/eaccelerator-0.9.6.1.tar.bz2/download

Run following commands:

  1. wget -O eaccelerator-0.9.6.1.tar.bz2 http://downloads.sourceforge.net/project/eaccelerator/eaccelerator/eAccelerator%200.9.6.1/eaccelerator-0.9.6.1.tar.bz2?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Feaccelerator%2Ffiles%2Feaccelerator%2FeAccelerator%25200.9.6.1%2F&ts=1316714345&use_mirror=surfnet
  2. tar -xvf eaccelerator-0.9.6.1.tar.bz2
  3. cd eaccelerator-0.9.6.1
  4. phpize
  5. ./configure
  6. make & make install
wget -O eaccelerator-0.9.6.1.tar.bz2 http://downloads.sourceforge.net/project/eaccelerator/eaccelerator/eAccelerator%200.9.6.1/eaccelerator-0.9.6.1.tar.bz2?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Feaccelerator%2Ffiles%2Feaccelerator%2FeAccelerator%25200.9.6.1%2F&ts=1316714345&use_mirror=surfnet
tar -xvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1
phpize
./configure
make & make install

If every command goes through without errors, the eaccelerator module should be installed, but it’s not in use yet. We have to configure php.ini to include the module.
Usually there are multiple php.ini files, to see which file is in use right now you can see the phpinfo page just created or in the command line:

  1. /usr/sbin/php5-fpm -i | less
/usr/sbin/php5-fpm -i | less

If you follow the steps above, it should in /etc/php5/fpm/php.ini, add following code to the end of php.ini

  1. [eaccelerator]
  2. extension="eaccelerator.so"
  3. eaccelerator.shm_size="16"
  4. eaccelerator.cache_dir="/tmp/eaccelerator"
  5. eaccelerator.enable="1"
  6. eaccelerator.optimizer="1"
  7. eaccelerator.check_mtime="1"
  8. eaccelerator.debug="0"
  9. eaccelerator.filter=""
  10. eaccelerator.shm_max="0"
  11. eaccelerator.shm_ttl="0"
  12. eaccelerator.shm_prune_period="0"
  13. eaccelerator.shm_only="0"
  14. eaccelerator.compress="1"
  15. eaccelerator.compress_level="9"
  16. eaccelerator.allowed_admin_path="/var/www/default/control.php"
[eaccelerator]
extension="eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.allowed_admin_path="/var/www/default/control.php"

Then restart php-fpm

  1. /etc/init.d/php5-fpm restart
/etc/init.d/php5-fpm restart

Now go to you phpinfo page, if it looks like below, then you have the eaccelerator module in work.

If you want to view the status of the eaccelerator and control it through web,

  1. cp control.php /var/www/default
cp control.php /var/www/default

The default username is admin, password is eAccelerator. Please change the password in the control.php file!

Till here you have accomplished the basic set up of a LEMP server on Debian 6. However there are still configuration and optimization steps to go. These steps will be different for different machines, systems and environment.

This entry was posted in Linux and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">