Nginx, php-fpm, MariaDB in Action (Install WordPress)

wordpress php-fpm mariadb nginx

After you’ve got Nginx, MariaDB, and php-fpm installed on your Debian 8 machine, let’s have a try…

Set up Nginx Virtual Host

All Nginx configurations are stored under directory /etc/nginx

vagrant@debian:~$ ls /etc/nginx/
conf.d  fastcgi_params  koi-utf  koi-win  mime.types  modules
nginx.conf scgi_params  sites  uwsgi_params  win-utf

To create a virtual host, we create new block server configuration under directory /etc/nginx/conf.d

An example wp.conf  (block server configuration file must be end by extension .conf)

vagrant@debian:~$ cd /etc/nginx/conf.d/
vagrant@debian:/etc/nginx/conf.d$ sudo nano wp.conf

write this configuration lines and save by pressing ctrl + o

server {

	root /var/www/wp/public_html; #1
	index index.html; #2

	server_name wp.dev; #3
	listen 80; #4

}

#1 means document root, your web files location is under directory /var/www/wp/public_html

#2 means, default index file is index.html

#3 means your server name or domain name. when you’re configuring on real server, it could be your domain name, e.g yourdomain.com

since I just run this on local machine, I can use .dev or whatever I want.

#4 means, this virtual host will listen to port 80 (default port for web server)

To have a virtual host works on local machine, we have to add the server name (in this case is wp.dev) to /etc/hosts file. You don’t need this if you’re setting up real server with real domain name.

vagrant@debian:~$ sudo nano /etc/hosts

add new line with format ip address (tab/space) hostname/alias

127.0.0.1       wp.dev

In our previous configuration, it says that files for wp.dev is placed under directory /var/www/wp/public_html. Okay, so now we need to create new index.html file on that directory.

First, let’s create the directory, in case we don’t have yet

vagrant@debian:~$ sudo mkdir -p /var/www/wp/public_html
vagrant@debian:~$ cd /var/www/wp/public_html/
vagrant@debian:/var/www/wp/public_html$

Then we create index.html

vagrant@debian:/var/www/wp/public_html$sudo nano index.html

type a message, e.g Welcome to my website , then save it by pressing ctrl+0

After you’ve finished, now let’s restart Nginx server

vagrant@debian:~$ sudo service nginx restart

Now you can access your virtual host from web browser by typing wp.dev in address bar.

php-fpm Configuration

Okay, in this section, we’re only going to configure Nginx in order to be able to process .php files, very-very basic 🙂

Edit our wp.conf file

vagrant@debian:~$ sudo nano /etc/nginx/conf.d/wp.conf

Modify the server {} block so that it looks like this lines below

server {

   root /var/www/wp/public_html;
   index index.php index.html; #1

   server_name wp.dev;
   listen 80;

   location ~ \.php$ { #2
     include fastcgi_params; #3
     try_files $uri =404; #4
     fastcgi_pass unix:/var/run/php/php7.1-fpm.sock; #5
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #6
   } #7

}

See.. it’s very simple. By only adding those lines, Nginx now can process .php files. This is the very basic you should know.

#1 we’re adding index.php to tell Nginx index to process besides index.html

#2 start of php configuration block. It tells Nginx to process all .php files according to this configuration block.

#3 always put include fastcgi_params; before any fastcgi_param rule (e.g number #6, it’s a custom rule). If you put include fastcgi_params; after any fastcgi_param rule, your custom rules might be overridden.

#4 when a .php file is missing, it tells Nginx to send header response 404 and client will be redirected to 404 page. (it’s optional, your configuration still works fine without this line)

#5 so Nginx communicate to php-fpm service through socket. When php-fpm is running, it will create new socket file under certain directory, in this case /var/run/php/php7.1-fpm.sock by default. It could be in different place depends on your configuration. You must add the correct location of php-fpm socket file, otherwise Nginx wouldn’t be able to communicate to php-fpm service.

#6 It’s a custom rule. First you include fastcgi_params (number #3) then you add your custom rules. I don’t entirely understand with this line, but still you have to add it, otherwise Nginx will fail to communicate to php-fpm service. Ask Google please.. 🙂

#7 end of php configuration block.

Okay now let’s try…

Create new file info.php under your virtual host directory

vagrant@debian:~$ cd /var/www/wp/public_html/
vagrant@debian:/var/www/wp/public_html$ sudo nano info.php

Add these lines and save it by pressing ctrl+o

<?php
phpinfo(); //this will display your php configurations

Wait a second, I forgot something,

If you install php-fpm on Debian machine then all php configurations will be in under directory /etc/php/:version/fpm

vagrant@debian:~$ cd /etc/php/7.1/fpm/
vagrant@debian:/etc/php/7.1/fpm$ ls
conf.d  php-fpm.conf  php.ini  pool.d

We still have to configure something.., now open www.conf under directory pool.d

vagrant@debian:/etc/php/7.1/fpm$ sudo nano pool.d/www.conf

find these lines

; Unix user/group of processes
; Note: The user is mandatory....
;       will be used.
user = www-data
group = www-data

You have to set owner and group of directory where you put your web files recursively  to be same as in your configuration, so php-fpm have permission to access it. In this sample we have our web files under directory /var/www

vagrant@debian:~$ sudo chown -R www-data:www-data /var/www

then we also need to give permission to Nginx to listen php-fpm service socket by change listen.owner  and listen.group value to nginx

; Set permissions for unix socket...
; permissions must be set in order to allow...
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
; mode is set to 0660
listen.owner = nginx
listen.group = nginx
;listen.mode = 0660

Now let’s restart nginx and php-fpm services

vagrant@debian:~$ sudo service php7.1-fpm restart
vagrant@debian:~$ sudo service nginx restart

Now you can open browser and type http://wp.dev/info.php to test. If you get display of php configurations information, that’s mean you’re success.

MariaDB ~ Create user and Give privileges

Mostly web application need a database to be able to work. Let’s prepare a database for our web application.

Log in to MySQL

vagrant@debian:~$ mysql -u root -p

Create new user and give privileges (just in case you want)

1# > CREATE USER 'developer'@'localhost' IDENTIFIED BY 'devel123';
2# > GRANT ALL PRIVILEGES ON *.* TO 'developer'@'localhost';
3# > FLUSH PRIVILEGES;

1# I create new user developer and set password devel123

2# I grant all privileges to all tables for user developer

3# Need to flush MySQL to reflect changes

If you want to create database using your new user instead, then you can exit and re-login to MySQL by using new user credential, then

Create new database wp & exit from MySQL

> create database wp;
> exit

Install WordPress

Okay, last section, let’s put up all together what we’ve learned, configuring virtual host on Nginx, configuring php-fpm, and preparing database. We’re gonna install WordPress CMS.

  1. Download WordPress installer from wordpress.org
  2. Extract to our previous directory /var/www/wp/public_html
  3. Make sure to have owner and group of extracted files belong to www-data. You can change it if necessary.
    ..:~$ sudo chown -R www-data:www-data /var/www/wp/public_html
    
  4. Run WordPress installer from browser and follow the instructions. On database configuration section you can provide with the credential we have before. 🙂
  5. Done.

I’m showing you that only by having simple virtual host configuration (wp.conf), you can install WordPress. If you’ve ever found similar tutorials that provide complicated configuration you couldn’t understand, well.. you don’t need it. Our wp.conf configuration is very-very basic configuration you only need to run WordPress. The point is you know what makes it work on the first time.

Later you can tweak your configuration for WordPress, for example to support user friendly url, caching static files, allowing access robots.txt, disallow access to hidden files, etc.

Leave a Reply