Installing Nginx with PHP (as PHP-FPM) and MariaDB (LEMP) on Debian 8

Channel: Linux
Abstract: systemctl reload php5-fpm.service Now create the following PHP file in the document root /var/www/html/apt-get -y install php5-fpm PHP-FPM is a daemon
Installing Nginx with PHP (as PHP-FPM) and MariaDB (LEMP) on Debian 8

This tutorial will show you the installation of the Nginx web server on Debian 8. Nginx (pronounced "engine x") is a free, open-source, high-performance HTTP server. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption. This tutorial shows how you can install Nginx with PHP support (through PHP-FPM) and MariaDB as MySQL drop-in replacement. This setup is often referred to as LEMP = Linux + nginx (pronounced "engine x") + MySQL + PHP) .

 

Preliminary Note

In this tutorial, I use the hostname server1.example.com with the IP address 192.168.1.100. These settings might differ for you, so you have to replace them where appropriate. You should have a Debian 8 server, I will use the Debian minimal server as the base system for this tutorial.

Updating the System

It is recommended to update the package lists and install any pending updates before we start with the Nginx setup. Run the following commands to install any pending updates.

apt-get update
apt-get upgrade -y

I will use the nano editor later to edit configuration files. Nano can be installed with this command:

apt-get -y install nano 

Installing MariaDB (as MySQL drop-in replacement)

In order to install MariaDB, we run:

apt-get -y install mariadb-server mariadb-client

You will be asked to provide a password for the MariaDB root user - this password is valid for the user [email protected] as well as [email protected], so we don't have to specify a MariaDB root password manually later on:

New password for the MariaDB "root" user: <-- yourrootsqlpassword
Repeat password for the MariaDB "root" user: <-- yourrootsqlpassword

 

Installing Nginx

Nginx is available as a package for Debian Jessie which we can install as follows:

apt-get -y install nginx

Start Nginx afterward:

systemctl start nginx.service

Type in your web server's IP address or hostname into a browser (e.g. http://192.168.1.100), and you should see the following page:

The default nginx document root on Debian 8 is /var/www/html.

 

Installing PHP

We can make PHP5 work in nginx through PHP-FPM (PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites) which we install as follows:

apt-get -y install php5-fpm

PHP-FPM is a daemon process (with the systemd unit file php5-fpm.service) that runs a FastCGI server on the socket /var/run/php5-fpm.sock.

 

Configuring nginx

The nginx configuration is in /etc/nginx/nginx.conf which we open now:

nano /etc/nginx/nginx.conf

The configuration is easy to understand (you can learn more about it here: http://wiki.nginx.org/NginxFullExample and here: http://wiki.nginx.org/NginxFullExample2)

First set the keepalive_timeout to a reasonable value like 2 seconds:

[...]
    keepalive_timeout   2;
[...]

The virtual hosts are defined in server {} containers. The default vhost is defined in the file /etc/nginx/sites-available/default - let's modify it as follows:

nano /etc/nginx/sites-available/default
[...]
server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;

server_name _;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;

# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
}

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

server_name _; makes this a default catchall vhost (of course, you can as well specify a hostname here like www.example.com).

I've added index.php to the index line. root /var/www/html; means that the document root is the directory /var/www/html.

The important part for PHP is the location ~ \.php$ {} stanza. Uncomment it like shown above to enable it.

Now save the file and reload Nginx:

systemctl reload nginx.service

Next open /etc/php5/fpm/php.ini...

nano /etc/php5/fpm/php.ini

... and set cgi.fix_pathinfo=0:

[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]

Reload PHP-FPM:

systemctl reload php5-fpm.service

Now create the following PHP file in the document root /var/www/html/:

nano /var/www/html/info.php
<?php
phpinfo();
?>

Now we call that file in a browser (e.g. http://192.168.1.100/info.php):

As you see, PHP5 is working, and it's working through FPM/FastCGI, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL is not listed there which means we don't have MariaDB / MySQL support in PHP yet.

Getting MySQL / MariaDB Support in PHP

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

apt-cache search php5

Pick the ones you need and install them like this:

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

APCu is a free and open PHP opcode cacher for caching and optimizing PHP intermediate code which extends the PHP opcache. It replaces the APC cache. and 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.

APCu can be installed as follows:

apt-get install php5-apcu

Now reload PHP-FPM:

systemctl reload php5-fpm.service

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

 

Making PHP-FPM use a TCP connection (optional)

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...

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

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

[...]
;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000
[...]

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

Then reload PHP-FPM:

systemctl reload php5-fpm.service

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 127.0.0.1:9000;, e.g. like this:

nano /etc/nginx/sites-available/default
[...]
location ~ \.php$ {
include snippets/fastcgi-php.conf;

# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
} [...]

Finally, reload Nginx:

systemctl reload nginx.service

Virtual Machine image

This tutorial is available as ready to use virtual machine in OVA / OVF format for Howtoforge subscribers. The VM format is compatible with VMWare and Virtualbox ond probably some other tools that can import this format. You can find the download link in the right menu on the top. Click on the file name to start the download.

The login details of the VM are:

SSH Login

Username: root
Password: howtoforge

MariaDB Login

Username: root
Password: howtoforge

Please change the passwords after the first boot.

The VM is configured for the static IP 192.168.1.100, the IP can be changed in the file /etc/network/interfaces.

Links
  • nginx: http://nginx.net/
  • nginx Wiki: http://wiki.codemongers.com/Main
  • PHP: http://www.php.net/
  • PHP-FPM: http://php-fpm.org/
  • MariaDB: https://mariadb.org/
  • Debian: http://www.debian.org/

Ref From: howtoforge

Related articles