Skip to content

Configuring Nginx to run PHP on ubuntu16

Default nginx setting when installed

path: /etc/nginx/sites-enable/default

# Default server configuration
server {
	listen 80 default_server;
	listen [::]:80 default_server;

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

	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
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#	# With php7.0-cgi alone:
	#	fastcgi_pass;
	#	# With php7.0-fpm:
	#	fastcgi_pass unix:/run/php/php7.0-fpm.sock;



As the line number 8 suggest to add index.php if we are running php. Let’s accomplish that by changing the line 9 to

index index.php index.html ;

Next stage:

Now we need to set the nginx to parse the PHP. So when it finds the php, we have a choice to decide what will handle the PHP for nginx. Either the option in line number 25 or line number 27.


means we want the server running on 9000 port to handle the php.

fastcgi_pass unix:/run/php/php7.0-fpm.sock;

means we want the phpfpm to handle the php. This is the reason why we have installed the phpfpm. So we will use the second option here. I normally use the first option when I have the phpfpm serving from different container but here we have it installed on same machine, thus we go with second option. Now after removing comment from line number 21 it will finally look as follow.


server {
	listen 80 default_server;

	root /var/www/html;
	# Add index.php to the list if you are using PHP
	index index.php index.html

	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
	location ~ \.php$ {
	  # With php7.0-fpm:
          fastcgi_pass unix:/run/php/php7.0-fpm.sock;

Restart the nignx or reload it

service nginx reload  or service nginx restart

Create a simple index.php file inside the root path set in the nginx to test if the nginx is serving the php or not.

Understanding the ITD (-itd) of docker

This is the most common thing we come across when playing with docker commands. i.e options -i -t and -d. Are you confident with what does it really mean in docker world. Do you know how does it really work? Let’s learn what is it exactly with a simplest example.

Meaning of the each options

  • -i : it means interactive mode
  • -t: tty mode
  • -d: daemon mode

Let us make an assumption that we have a running docker container called simple_test_container and is a linux machine with ubuntu as base image.

Now lets see what else this command will do when we try to execute a exec command on it.

Scenario 1. Use case of -i

docker exec -i simple_test_container /bin/bash

The use of exec command is to get into the running container. But you will not be able to get into the container because you are in interactive mode. You can just interact with the docker container. The only things you can do here is pass the command that you want to run on the container.

Scenario 2 Use case of -i

Now , run this instead to interact with the container

docker exec -i simple_test_container ls

Now you get the list of items shown as the result. You have successfully interacted with the running container.

Scenario 3 Use case of -t

Suppose we want to get into the container than we use the option -t

docker exec -t simple_test_container bin/bash

The problem, you will face here is though we will successfully get into the running docker container but we will not be able to run any command because we don’t have the -i option enabled that allow us to interact with the container machine.

Scenario 4 Use case of -it

To solve the problem in scenario 3 we would use both option -it which will allow us to get into the running docker container and than interact too.

docker exec -it simple_test_container /bin/bash

The problem, you will face here is once you come out (exit) of the container the container will stop.

Scenario 5 use case of -itd

To solve the issue in scenario 4 we will make use of the -d option which will run the service in daemon mode (background). Now though we come out of the container it will not get halted, will still continue running in background.

Summing up use case of docker -i -t -d options

  • -i : to just interact with the docker container
  • -d: to run the container in daemon mode (background)
  • -t : to run or get into the running container or we call it TTY mode
  • -it : allows to get into TTY and interact
  • -id: allows interaction only and runs the container in background
  • -itd : allows TTY , allows interaction and runs the container in background



installing php version 7.2 on ubuntu 16.04

The way we install php are almost same for all the versions, the only difference will be on the repository. The php version 7.2 will have its own apt repository from where we will fetch it for installation. If the version differs you have to look for the respective repository.

steps to install php 7.2 on ubuntu 16.04

Follow the following steps to install php 7.2 on ubuntu 16.04.

  1. Update the apt
    apt-get update
  2. Install the package which will help us adding the repository. These steps might not require if you have already installed it
    apt-get install software-properties-common
  3. Add the repository that is for php 7.2 its called ppa:/ondrej/php
    add-apt-repository ppa:ondrej/php
  4. Update the apt again after adding the repository
    apt-get update
  5. Now ready to install php 7.2 on ubuntu 16.04
    apt-get install php7.2
  6. Verify the php installation
    php --vesion

    sucessfully installed php 7.2 on ubuntu 16.04

Creating a simple iterators in python with example

The list, tuples and dictionary  in python are iterators. It means, we can iterate over it ie we can run a for loop on it to get its values. These are the default build-in iterators in python.

We can create our own iterators too. Here we will learn to create our very own iterators and iterate over it.

How to create a iterators:

Technically the objects that will implement the method __iter__ and __next__ are iterators. So we need to create a class and make it implement the two methods. Bellow we have a simple example of iterators, where you can find the method being implemented by the class.

Creating our own simple iterators in python3

# trying to create iterators like list , tuples

class MyIterators:
  def __init__(self, maxpoint):
    self.max = maxpoint
  #called when the iteration is called
  #pint from which iteration start
  def __iter__(self):
    self.item = 0
    return self

  #to move to the next element
  #until it stops do some action and return
  def __next__(self):
    item = self.item
    if item > self.max:
      raise StopIteration
    self.item = item +2
    return item

for item in n:


Iterating through iterable :

One way will be to use the for loop as in the example above. But we can also make use ot iter method and get the next value as shown bellow

n = MyIterators(2)
item  = iter(n)   #using the iter method as its iterable
print(next(item)) #getting next item with next method  


Using the StopIteration to stop the iteration:

As seen in the iterator example above to stop the iteration from running forever. We use the StopIteration to raise the exception. Which will stop the process when the max limit is reached.

Python IDE Is it compulsory

We often see people wandering around in internet, which IDE should I be using for python. There are so many IDE around. We would not like to compare and say this one is better than other. The choice depends and might differ from users.

As a beginners, We don’t really need IDE. We can simply run the script created in simplest code editor like VIM (linux based code editor). Only requirement is the file must be in .py extension. The IDE make life easier with many features it has like auto-complete, syntax highlighter, debugger and many more. But if you are a beginners first focus on learning the core concept rather than debating and wasting your time on choosing the right IDE.

List of IDE and code editor for python :

  1. PyCharm
  2. Sublime  Text
  3. Atom
  4. Visual Studio
  5. Eclipse with PyDev
  6. Spyder

The list goes on. We personally request beginners to not bother about it and focus more on python.


Installing python on linux (ubuntu)

Python normally comes in two version python-2 and python-3. If you are just a beginners ,like many , you might wonder too what is the difference but I would just recommend to get going with python version 3. With time you can explore the difference.

installing python on ubuntu machine

Check if python is installed:

Most of the system mainly linux based system comes with python pre-installed. In such case, we don’t need to install it again. To check if your system already have the python installed or not. Just type python in the terminal. If you see some message with python version then you already have the python installed in your system. Nowadays, the pre-installed version of python is python-3. If you want to install the python-2 follow the steps bellow.

Installing python-2 in ubuntu:

Installing python-2 is very easy in ubuntu machine. Simply type the following command in terminal. To open the terminal type ctrl+alt+t or open it manually.

sudo apt-get update && apt-get install python

Installing python-3 in ubuntu:

To install python-3 in ubuntu machine follow the steps bellow.

sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6

Opening desired python version if both python2 and python3 is installed.

Yes, we can have both version of python installed. In such scenario if we type python, the python version2 interpreter will start. for starting python version3 type python3.

Similarly, if we use python or python3 command to execute the script in respective python.


Lazy evaluation in python with simple example

Things become interesting and even the harder concept becomes easier when we have a simple example to showcase the idea. Today I am trying to put the idea of lazy evaluation with a very simple python codes, using a single built in function.

lazy evaluation in python with example

To test the process we need to run the application in python2 and python3

numbers = range(1,10000000000000)
for item in numbers:
    print item

Run it in python-2 and we will run into memory issue. Particularly, will say memory overflow. Which means it tried to put all the numbers in the range in the memory and could not do it.

Solution is lazy evaluation. To get the values only when required. So try running the same line of code and it will run smoothly without any memory issue. So what had exactly happened is. It didn’t put all the values in range into memory and our for loop fetched the value that is required and displayed it.

I hope, I have tried my best to make the concept clear. So try to make your function lazy evaluated as possible when it comes to handling large data.

Way to debug in ansible using register and debug

# tasks file for testing 

- name: check if the container is running
  shell: docker ps | grep "my_mysql"
  register: result
  failed_when: "result.rc == 2"
  check_mode: no

- debug:

- name: check value
  shell: echo "Container is not Running"
  when: result.stdout == ""

The ansible playbook above is what I normally do when it comes to debugging or running the playbook based on some condition match.

Lets write a playbook to check if the container is running or not and we will run the job only when it is running.

  • In line number 5 we are simply running bash script to check if the container with that name is running or  not.
  • In line number 6 we register the variable. This is how we store the result of the command in ansible.
  • The purpose of line number 7 8 is to ignore the error. Suppose if such container is not found in line number 5. Though we expect it to register blank value in our variable. It does not do so and terminate so we are forcing it to ignore the error. On success it returns 0 and 1 on error. So we are making it compare with 2 that will not ever happen and making the playbook not fail.
  • Use debug to check what exactly is there in our registered value. Normally it will showcase everything, here we just want to check output so specifying its key called stdout.
  • Use when to work as a conditional statement in ansible. It is like if condition in any programming language. We want to run the shell command above it only when the value registered output is empty.




how to run jenkins in docker

When it comes to Jenkins. The most easy way I find to setup a jenkin CI-CD server is ,start one AWS EC2 instance and run docker container on it. The most cool thing is it just takes few minute to do so. No hustle at all.

A one line command to run jenkin container

docker run -d -p 8080:8080 -v $PWD/jenkins:/var/jenkins_home -t jenkins/jenkins

Stopping Jenkins build on test failure or reading the logs message

You might have come to this situation. A jenkins job runs though there are test case failing and the build appears to be successful. The problem occurs when you have another job on downstream. The downstream will be running even though there was a problem in the upstream job. So to tackle this problem. Initially I tried to write bash script but that was no so effective so I started searching for the plugin. I found this Jenkins plugin which works like a charm.

  • Jenkins plugin Name: TextFinder plugin
  • Jenkins plugin URL:

How Does the TextFinder Jenkins plugin works

The plugin has the feature to search the build logs and we can pass what to look for and based on the result we can decide what to do with the jobs. To make it success or make it unstable. Which is very clear from the image below.

jenkins Text finder plugin for making jobs unstable for error
Reading the jenkins logs to detect failure or success