Guide to installing NGINX + PHP-FPM + MariaDB on Debian 7 & 8

Share

This tutorial will guide you through the steps to get nginx, php5-fpm and MariaDB working on your server with Debian 7 or 8 installed. This will run just fine on a 128MB OpenVZ VPS.

Let’s get started by installing nginx. First you’ll need to add the nginx repo to get the latest stable release installed on your server. Follow the simple steps below.

Installing Nginx and adding repository

Login to your server using either PuTTy or your favourite SSH client and use the following commands.

Make sure to get the right keys added or else the install will fail with a key error;

wget http://nginx.org/keys/nginx_signing.key
apt-key add nginx_signing.key

Add the NGINX repository to your sources.list, for debian 7 (Wheezy) change jessie to wheezy;

echo 'deb http://nginx.org/packages/debian/ jessie nginx' /etc/apt/sources.list

Now perform an update and upgrade and install the latest stable version of nginx;

apt-get update apt-get upgrade
apt-get install nginx

Now let’s configure our first website. Go to /etc/nginx/conf.d, remove the default.conf and example_ssl.conf and create a new your-website.conf file using nano (apt-get install nano).

cd /etc/nginx/conf.d
rm default.conf example_ssl.conf
nano your-website.conf

Copy and paste the following configuration file into your-website.conf, make sure to change the your-website.tld to your own domain name you’d like to use. Use CTRL + C to save the file once you’re done in nano;

server {
    listen 80;
    root /var/www/your-website;
    index index.php index.html;
    server_name your-website.tld www.your-website.tld;
    location / {
            try_files $uri $uri/ /index.php;
    }
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

If you’d like to have SSL enabled on your website then please use the following configuration file, this will also redirect all non HTTPS/SSL request to HTTPS/SSL. Make sure to have a valid .pem and a .key file in your /etc/ssl directory.

server {
    listen 80;
    root /var/www/your-website;
    index index.php index.html;
    server_name your-website.tld www.your-website.tld;
  return 301 https://$host$request_uri;
    location / {
            try_files $uri $uri/ /index.php;
    }
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_pass unix:/var/lib/php5/php5-fcgi.sock;
    }
}
server {
    listen   443;
    ssl    on;
    ssl_certificate    /etc/ssl/your-website.pem;
    ssl_certificate_key    /etc/ssl/your-website.key;
    root /var/www/your-website;
    index index.php index.html;
    server_name your-website.tld www.your-website.tld;    
    location / {
            try_files $uri $uri/ /index.php;
    }
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_pass unix:/var/lib/php5/php5-fcgi.sock;
    }
}

Now let’s create the /var/www directory and give nginx permissions to the /var/www folder.

chown -R nginx:nginx /var/www

Installing PHP5-FPM

Now, let’s go and install PHP5-FPM and some of the extra PHP5 modules.

apt-get install php5-fpm php5-common php5-sqlite php5-gd php5-mysqld php-pear php5-imap php5-mcrypt php5-curl

Now we’re going to make a few modifications to the php.ini file located at /etc/php5/fpm/php.ini and we’re going to replace the php.ini file with a file optimized for wordpress by me (this will make wordpress load faster).

cd /etc/php5/fpm
rm php.ini
wget --no-check-certificate https://raw.githubusercontent.com/joodle/Hiawatha-MariaDB-PHP5-FPM-SQLBuddy/master/php.ini

Now we’ll need to make a few small changes to our php-fpm.conf configuration file so that nginx will be able to load PHP files. I already have a almost pre-configured php-fpm.conf on github, which will make things a bit easier.

cd /etc/php5/fpm
rm php-fpm.conf
wget --no-check-certificate https://raw.githubusercontent.com/joodle/Hiawatha-MariaDB-PHP5-FPM-SQLBuddy/master/php-fpm.conf

We’re going to change some permission settings in the php-fpm.conf file. Nginx will otherwise complain about not being able to access the php5-fcgi.sock file which is needed to execute php files on the server end.

Open php-fpm.conf in the nano text editor and search for the following;

[www]user = www-data
group = www-data

And replace it with;

[www]user = nginx
group = nginx

Save the file using CTRL + C and press Y.

Create a php information file at the /var/www/your-website directory;

echo /var/www/your-website/phpinfo.phpsite/phpinfo.php

Now restart php-fpm and nginx;

systemctl restart php5-fpm.service
systemctl restart nginx.service

Or when you’re using Debian 7;

service php5-fpm restart
service nnginx restart

Navigate to http://your-website.tld/phpinfo.php and if everything went alright you should see a page containing some information about the php version you’ve installed on your server.

Installing MariaDB MySQL Server

First we’ll have to add the MariaDB official repository plus keys.

apt-get install software-properties-common
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
add-apt-repository 'deb http://mirror.i3d.net/pub/mariadb/repo/10.1/debian jessie main'
apt-get update

Let’s install MariaDB;

apt-get install mariadb-server

Make sure to have a strong password for the MariaDB (MySQL) root user. You can generate strong passwords on random.org (https://www.random.org/passwords/).

You can add/create databases using the following commands;

<strong>Login to MySQL</strong>: mysql -u root -p
<strong>Add user</strong>: CREATE USER 'YOUR-OWN-USERNAME'@'localhost' IDENTIFIED BY 'YOUR-OWN-PASSWORD';
<strong>Add database</strong>: CREATE DATABASE DATABASENAME;
<strong>Assign user to database</strong>: GRANT ALL PRIVILEGES ON DATABASENAME . * TO 'YOUR-OWN-USERNAME'@'localhost';
<strong>Delete database</strong>: DROP DATABASE DATABASENAME;
<strong>Delete user</strong>: DROP USER 'YOUR-OWN-USERNAME'@'localhost';
<strong>Flush all privileges</strong>: FLUSH PRIVILEGES;

That’s it! Now you have a server with Nginx + PHP5-FPM + MariaDB installed!

Leave A Reply