Installing php7.0-fpm With Nginx Web Server

Installing php7.0-fpm With Nginx has never been easier. Everything you need is provided by the package manager in most cases with just a few commands to install. First let’s take a look at the php FPM module and the Nginx web server.

What is Nginx?

Nginx is a high performance load balancer, web server, and reverse proxy. Nginx is the heart of the modern web and a big contender to Apache. Here at BitAccel we prefer nginx for general and advanced use over Apache due to it’s ease of use and robustness. In this article we will be using Nginx as our web server.

What is php7.0-fpm?

PHP7.0 is currently the latest and greatest version of PHP from the PHP project. PHP can be served up many different ways, and one of those ways is with the FPM(FastCGI Process Manager) module. PHP-FPM is an improvement over the PHP-CGI module because it includes adaptive process spawning among other neat features.

How does php7.0-fpm work?

When implementing this module think of PHP-FPM and/or PHP-CGI as their own web server like Apache or Nginx. PHP-FPM processes the PHP files and renders the output to a unix socket or tcp socket. This data can then be read directly from the socket by a web server and served over the web. PHP also has it’s own built in web server http://php.net/manual/en/features.commandline.webserver.php that can be initialized from the php command line but is only meant for testing purposes.

So how do I install nginx with php?

Let’s get down to the dirty part. We’ll assume you are using Debian or Ubuntu distributions for the upcoming commands. With this script we are also installing a mysql database service called mariadb and adding dotdeb to our repo. If you get any errors let us know in the comments and we’ll diagnose and update the article.

#!/bin/sh
#add dotdeb to the sources.list
#You need to change 'jessie' here to whatever version of Debian you are running.
echo "deb http://packages.dotdeb.org jessie all" >> /etc/apt/sources.list
echo "deb-src http://packages.dotdeb.org jessie all" >> /etc/apt/sources.list
wget https://www.dotdeb.org/dotdeb.gpg
apt-key add dotdeb.gpg
#export so we dont get any whiptail dialogs asking for input
export DEBIAN_FRONTEND=noninteractive
apt-get -q -y update
apt-get -q -y install nginx-full
apt-get -q -y install mariadb-server-10.0 mariadb-client-10.0 mariadb-client-core-10.0 mariadb-server-core-10.0
apt-get -q -y install wget
#for ubuntu we need libturbo for php gd not in the repos
read -p "Are you running this on Ubuntu? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
if [wget -q "http://ftp.us.debian.org/debian/pool/main/libj/libjpeg-turbo/libjpeg62-turbo_1.5.1-1_amd64.deb"] && [dpkg -i libjpeg62-turbo_1.5.1-1_amd64.deb]; then
else echo "Fatal error installing libjpeg62-turbo for the php7.0-gd library, not continuing."
return 0
fi
apt-get -q -y install php7.0-fpm php7.0-cli php7.0-common php7.0-curl php7.0-dbg php7.0-gd php7.0-imap\
php7.0-json php7.0-ldap php7.0-mcrypt php7.0-mysql php7.0-odbc php7.0-opcache php7.0-pgsql php7.0-phpdbg\
php7.0-pspell php7.0-readline php7.0-recode php7.0-snmp php7.0-sqlite3 php7.0-sybase php7.0-tidy
#setup our default nginx server
cat > /etc/nginx/sites-enabled/default <
server { 
listen 80 default_server;
root /var/www/; 
server_name _; 
location / {
index index.php; 
}
location ~ \\.php$ {
fastcgi_split_path_info ^(.+\\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php7.0-fpm:
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
fastcgi_index index.php; 
include /etc/nginx/fastcgi_params; 
fastcgi_param SCRIPT_FILENAME /var/www/\$fastcgi_script_name;
}
}
EOF
#download adminer so we can manage our database if needed
wget https://www.adminer.org/static/download/4.2.3/adminer-4.2.3.php -O /var/www/adminer.php
#start up our services
service php7.0-fpm restart
service nginx restart
service mysql restart

The above code can be ran as a bash script by saving this to a file such as ‘nginxPhpFpmSetup.sh’ and running ‘sh nginxPhpFpmSetup.sh’ from your terminal or you can execute each of these lines on their own by pasting them into your terminal.

Nginx and PHP-FPM are installed!

Now you can enjoy your installation by uploading php files in /var/www and visiting ‘localhost’ in your favorite web browser, or the ip address of the machine you’ve installed this on.

Leave a Reply

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