Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support (LEMP) On Debian Wheezy - Page 2

6 Getting MySQL Support In PHP5

To get MySQL support in PHP, we can install the php5-mysql package. It's a good idea to install some other PHP5 modules as well as you might need them for your applications. You can search for available PHP5 modules like this:

apt-cache search php5

Pick the ones you need and install them like this:

apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

APC is a free and open PHP opcode cacher for caching and optimizing PHP intermediate code. It's similar to other PHP opcode cachers, such as eAccelerator and Xcache. It is strongly recommended to have one of these installed to speed up your PHP page.

APC can be installed as follows:

apt-get install php-apc

Now reload PHP-FPM:

/etc/init.d/php5-fpm reload

Now reload in your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module:


7 Making PHP-FPM Use A TCP Connection

By default PHP-FPM is listening on the socket /var/run/php5-fpm.sock. It is also possible to make PHP-FPM use a TCP connection. To do this, open /etc/php5/fpm/pool.d/www.conf...

vi /etc/php5/fpm/pool.d/www.conf

... and make the listen line look as follows:

;listen = /var/run/php5-fpm.sock
listen =

This will make PHP-FPM listen on port 9000 on the IP (localhost). Make sure you use a port that is not in use on your system.

Then reload PHP-FPM:

/etc/init.d/php5-fpm reload

Next go through your nginx configuration and all your vhosts and change the line fastcgi_pass unix:/var/run/php5-fpm.sock; to fastcgi_pass;, e.g. like this:

vi /etc/nginx/sites-available/default

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

                # With php5-cgi alone:
                # With php5-fpm:
                #fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;

Finally reload nginx:

/etc/init.d/nginx reload


8 CGI/Perl Scripts

If you want to serve CGI/Perl scripts with nginx, please read this tutorial: Serving CGI Scripts With Nginx On Debian Squeeze/Ubuntu 11.04

The recommended way is to use fcgiwrap (chapter 4).


9 Links


About The Author

Falko Timme is the owner of nginx WebhostingTimme Hosting (ultra-fast nginx web hosting). He is the lead maintainer of HowtoForge (since 2005) and one of the core developers of ISPConfig (since 2000). He has also contributed to the O'Reilly book "Linux System Administration".

Share this page:

9 Comment(s)

Add comment


From: bravo_kernel at: 2013-07-28 13:05:56

Very nice post Mr. Timme. I have just upgraded from CGI without any issues. Ty!

From: Anonymous at: 2013-11-17 17:06:50

For a German How-To with a more detailed instruction (subdomains, multiple sockets etc.) see

From: Mark Greenall at: 2014-01-10 20:29:25

This is a really good article and very informative.  A good follow up to this would be to build NGiNX from source, and 'harden' it for the production environment.

From: Anonymous at: 2013-06-04 20:32:13


 I would strongly suggest to use's version of nginx and php instead of debian's default.

 The reason is that dotdeb is always up to date.  It means more feature for nginx and possibly faster security bug resolution with php.

 my 2 cents

From: Anonymous at: 2013-09-12 19:41:15

Really nice job. Simple and effective. Kudos

From: monsune at: 2013-12-01 21:18:17

I did enjoy it to the last bit and i have also learned some security-wise. Many thanks!

From: Steve at: 2014-04-20 21:33:10

What is the advantage if nginx listen to the tcp port instead a socket?

From: Nikolay Grishchenko at: 2014-07-20 04:22:34

A nice howto guide. Thank's a lot.

Steve, you can find one of the answers here:

 Most likely you do not need PHP-FPM use TCP in sandbox or a simple project.

From: stevek at: 2015-02-17 00:39:58

You guide does nto work for me. The php config for nginx is not quite right.

The PHP block in nginx.conf  should look like this to make it work

### PHP BLOCK ### location ~ \.php?$ { fastcgi_keep_conn on; try_files $uri =404; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_hide_header X-Powered-By; # fastcgi_pass; fastcgi_pass unix:/var/run/; # fastcgi_pass unix:/var/run/hhvm/hhvm.sock; }