Setting up Nginx, PHP, PostgreSQL (Part-2)

In this part reader will learn –

  • How to install PHP.
  • How to install FastCGI server for parsing and serving php files. Here we will install php-fpm (PHP FastCGI Process Manager).
  • Configure Nginx to forward php file requests to php-fpm so that our PHP source files will be served properly.
  • Configure PHP and PHP-FPM to match with Nginx configuration.
  • Finally test a simple php script to check our configurations.

So this tutorial will be a short one, but it will open a whole large door towards web development. Let’s get started.

NOTE : Like before it will match with Linux OS. For me it’s Archlinux.

Open up terminal and run the following command to install PHP and PHP-FPM

sudo pacman -S php php-fpm

This will provide php interpreter, libraries, php-fpm server, php-fpm systemd unit. If reader is running some other Linux, look for php, php5-fpm packages in their respective package management tools.
After this we have the following commands available to start, stop or reload PHP-FPM server –

  • Start : sudo systemctl start php-fpm
  • Stop : sudo systemctl stop php-fpm
  • Restart : sudo systemctl reload php-fpm
  • Autostart : sudo systemctl enable php-fpm

Let’s just run sudo systemctl enable php-fpm for autostarting the FastCGI server at boot-up.

Now let’s configure nginxtest.dev so that Nginx will pass the php scripts to PHP-FPM server. Open /home/shimon/www/sites-available/nginxtest.dev and change the text of it as the following –


server {
	server_name  nginxtest.dev;
	root /home/shimon/www/sites/nginxtest.dev;
	error_log /home/shimon/www/sites/nginxtest.dev/errors.log;
	access_log /home/shimon/www/sites/nginxtest.dev/access.log;

	location / {
	    index index.php index.htm index.html;
	}

	error_page 500 502 503 504 /50x.html;

	location = /50x.html {
	    root html;
	}

	location ~ \.php$ {
	     fastcgi_pass   unix:/run/php-fpm/php-fpm.sock;
	     fastcgi_index  index.php;
	     include        fastcgi.conf;
	}
}
	

REMINDER :Don’t forget to replace shimon with your username.

Now let’s configure PHP and PHP-FPM. First open /etc/php/php.ini file and search for this line –

;cgi.fix_pathinfo=1

and change it into –

cgi.fix_pathinfo=0

Save it and open /etc/php/php-fpm.d/www.conf, there find something like this –


user = http
group = http
	

and change it into –


user = shimon
group = shimon
	

Save it and open up terminal, run the following (change the my user name) to add permissions for your user –

sudo usermod -a -G http shimon

NOTE : Reader should check if he is already added to the http group before running the last command. To check the groups where you are already included run this (change the my user name) –

groups shimon

NOTE : If reader is running other Linux, find php.ini and php-fpm.ini and edit like the above.

Now we will create a php file inside nginxtest.dev. Remove the index.html and copy-paste the following into a new file and save it as /home/shimon/www/sites/nginxtest.dev/index.php


<!DOCTYPE html>
<html>
<head>
	<title>Nginx Test Site</title>
</head>
<body>
	<?php
		echo phpinfo();
	?>
</body>
</html>
	

We are all set to check if our server setup can serve php scripts (like the index.php just have been created). Run the following commands –

	sudo systemctl start php-fpm
	sudo systemctl reload nginx

Fire up the browser, go to nginxtest.dev and see for yourself.

Screenshot from 2016-02-21 00-20-04

That’s it for now. In the next one we will install and configure PostgreSQL.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s