Install nginx with PHP-FPM and mysql server 5.5
Install nginx with PHP-FPM and mysql server 5.5
In my earlier tutorials I showed you how to install PHP-FPM with fastcgi and apache web server on ubuntu or debian operating system. This time I thought of doing a tutorial in a similar manner, but a different web server, with Nginx. As all of you know, Nginx is one of the fastest and scalable web server used on the internet at the moment. Look at Cloudflare’s network how well scaled and big it is, all by using nginx. So yeah, lets just get straight to the point and show you how to install Nginx with PHP-FPM step by step.
Preparing repositories
With PHP-FPM you need to install some repositories, first for PHP extensions and then also for PHP-FPM. I currently use the Doteb repository for Debian and Ubuntu, I’m going to use them this time also, so add the below lines to your /etc/apt/sources.list
1 2 |
deb http://packages.dotdeb.org wheezy all deb-src http://packages.dotdeb.org wheezy all |
Don’t forget the Doteb key for using this repository:
1 2 |
wget http://www.dotdeb.org/dotdeb.gpg apt-key add dotdeb.gpg |
Next don’t forget to update the package list, it is necessary on Debian / Ubuntu:
1 |
apt-get update |
Install Mysql server 5.5
Mysql server 5.5 is currently the one I’m recommending the most, has been giving good results with my current server configurations. To install mysql server 5.5 run the below command:
1 |
apt-get install mysql-server-5.5 mysql-client-5.5 |
You will be asked to enter a root password twice, once completed, you should have mysql server up and running.
Install PHP, PHP extensions and FPM
This is actually what we are interested in, to install PHP and the PHP extensions, run the below commands:
1 |
apt-get install php5 php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-json |
Once we finished to install PHP and all the PHP extensions needed, we can start installing PHP-FPM
1 |
apt-get install php5-fpm |
Once we’ve completed this step, we proceed with installing the Nginx web server.
Install nginx with php-fpm on Ubuntu / Debian server
I did wrote another article before about how to install nginx web server, so you can either read that article which contains virtual host configuration and more detail explanation, or proceed further with this tutorial:
1 |
apt-get install nginx |
At this point you should have nginx installed, if you load your IP in your browser, you should see a similar page:
Configuring PHP-FPM
This is actually the hard part, I thought of getting the vhost set from scratch, but I saw that nginx by default provide us with one example that’s almost similar to what we need. Anyway, I’ve highlighted the parts you specifically need to make sure are there, the rest are normal Nginx vhost configuration.
Also first you will need to change php-fpm to listen on a TCP ip and port, as I’ve said in my other articles, that’s my default setup, although some of you will use a unix socket instead of this to avoid using too many tcp sockets, but I’ve not yet seen an issue with my setup and have been using it for some time now.
Anyway, open the file:
1 |
/etc/php5/fpm/pool.d/www.conf |
and edit the part with:
1 |
listen = 127.0.0.1:9000 |
instead of the unix socket. Next will be the Nginx vhost configuration, a default template is below and important parts are highlighted:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
[...] server { listen 80; server_name _; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.php index.html index.htm; } # redirect server error pages to the static page /40x.html # error_page 404 /404.html; location = /40x.html { root /usr/share/nginx/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/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ .php$ { root /usr/share/nginx/html; fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /.ht { deny all; } } [...] |
If you create a phpinfo() you should see:
![]() PHP Version 5.4.20-1~dotdeb.1 |
System | Linux debian 3.2.0-4-686-pae #1 SMP Debian 3.2.51-1 i686 |
Build Date | Sep 21 2013 22:37:00 |
Server API | FPM/FastCGI |
Virtual Directory Support | disabled |
Configuration File (php.ini) Path | /etc/php5/fpm |
Loaded Configuration File | /etc/php5/fpm/php.ini |
Scan this dir for additional .ini files | /etc/php5/fpm/conf.d |
Additional .ini files parsed | /etc/php5/fpm/conf.d/10-pdo.ini, /etc/php5/fpm/conf.d/20-curl.ini, /etc/php5/fpm/conf.d/20-gd.ini, /etc/php5/fpm/conf.d/20-imagick.ini, /etc/php5/fpm/conf.d/20-imap.ini, /etc/php5/fpm/conf.d/20-intl.ini, /etc/php5/fpm/conf.d/20-mcrypt.ini, /etc/php5/fpm/conf.d/20-memcache.ini, /etc/php5/fpm/conf.d/20-mysql.ini, /etc/php5/fpm/conf.d/20-mysqli.ini, /etc/php5/fpm/conf.d/20-pdo_mysql.ini, /etc/php5/fpm/conf.d/20-pdo_sqlite.ini, /etc/php5/fpm/conf.d/20-pspell.ini, /etc/php5/fpm/conf.d/20-recode.ini, /etc/php5/fpm/conf.d/20-snmp.ini, /etc/php5/fpm/conf.d/20-sqlite3.ini, /etc/php5/fpm/conf.d/20-tidy.ini, /etc/php5/fpm/conf.d/20-xmlrpc.ini, /etc/php5/fpm/conf.d/20-xsl.ini, /etc/php5/fpm/conf.d/ming.ini, /etc/php5/fpm/conf.d/ps.ini |
PHP API | 20100412 |
That’s it for now, comment below if you spotted an error and will see you again in our next tutorial.