Copyright
PHP and PostgreSQL Programming By Example
Agus Kurniawan
1st Edition, 2016
Copyright 2016 Agus Kurniawan
Table of Contents
Preface
In general people use PHP and database MySQL. This book provides alternative approach to build PHP application with database PostgreSQL. It describes how to work with PHP and PostgreSQL and illustrates their use with code examples.
Agus Kurniawan
Berlin, March 2016
1. Setting up Development Environment
This chapter explains how to setup development to develop PHP and PostgreSQL.
1.1 System Environment
We need to setup development environment to develop PHP application with database PostgreSQL. The following is the list of development platform you can use:
In this book, I use Ubuntu, OS X and Windows 10 for testing. I use Apache web server on Linux and Mac. Otherwise, I use IIS web server on Windows platform.
1.1.1 IIS Web Server on Windows Platform
You also must install IIS on your Windows platform. On Windows Client, you can find it on Program and Features. You can see it on Figure below. Then you can find Turn Windows features on or off (red arrow).
After you click Turn Windows features on or off you can obtain a dialog as below.
Checked Internet Information Services and World Wide Web Services. Don't forget to checked CGI, ISAPI Extensions and ISAPI Filters. You can checked other several IIS features. If finished, click OK button.
It will install all selected features. If done, you can check web server IIS on Administrative Tools. You can see Internet Information Services (IIS) Manager. Click it and then you can obtain web server IIS dialog, shown in Figure below.
You also can verify installed web server IIS by opening browser and then type http://localhost . If success, you can see the result as follows.
1.1.2 Apache on Ubuntu and Mac
On OS X , Apache already installed for you. On Ubuntu, you can install it using this command.
$ sudo apt-get update $ sudo apt-get install apache2
1.2 Deploying PHP on Web Server
In this section, we will deploy PHP on web servers. I use IIS and Apache web servers for testing.
Let's go.
1.2.1 IIS for Windows Platform
There are two options to install PHP on IIS. You can install it via Microsoft Web Platform Installer, http://php.iis.net/. After downloaded, you can run it and then you obtain the following dialog.
** this dialog is a sample for Web Platform Installer 4.6.
Click Options and select IIS. If done, click OK button.
After that, click Install button to start installation.
In this book, I installed PHP on IIS manually. You can install the latest PHP on web server IIS. How to install?
Firstly, we must install PHP for Windows. You can download it on http://windows.php.net/download/ . You can download it for x86 or x64 and Thread-Safe or Non Thread-Safe. It's recommended to download Non Thread-Safe PHP. After downloaded, you obtain ZIP file (particular PHP version provides installer file). Extract it on the specific folder, for instance, C:\PHP.
The following is a sample result of extracted PHP.
After that, we can install PHP Manager for IIS. You can download it on https://phpmanager.codeplex.com/ . If you have install PHP Manager for IIS, you can see PHP Manager on IIS.
Double click PHP Manager. Then you can see PHP Manager view. Click Register new PHP version. Navigate to php-cgi.exe file location. If you have extracted PHP file on C:\PHP, you can find it on C:\PHP\php-cgi.exe .
To verify, click Check phpinfo() and then you obtain a dialog as below.
Select Default Web Site and URL http://localhost. If done, click OK button.
If success, you can see phpinfo() output. The following is a sample output.
1.2.2 Apache for Linux and Mac Platforms
For Linux platform, you can install Apache with PHP using these commands
$ sudo apt-get update $ sudo apt-get install apache2 $ sudo apt-get install php5 libapache2-mod-php5
For Mac, Apache has installed by default. You can enable this service using this command.
$ sudo apachectl start
Then, open a browser and navigate to http://localhost
To install PHP on Mac, you can use brew. For instance, I install PHP 7 with PostgreSQL driver using these commands.
$ brew tap homebrew/dupes $ brew tap homebrew/versions $ brew tap homebrew/homebrew-php $ brew install php70 --with-postgresql
After installed PHP, you can verify by checking PHP version using this statement
$ php -v
Now you can configure PHP on Apache. Firstly, backup your
$ cd /etc/apache2/ $ sudo cp httpd.conf httpd.conf.bak
Now edit httpd.conf file.
$ sudo nano httpd.conf
Disable PHP5 and enable PHP 7 as follows.
#disable PHP5 module #LoadModule php5_module libexec/apache2/libphp5.so #Enable PHP 7 module LoadModule php7_module /usr/local/opt/php70/libexec/apache2/libphp7.so \. php $> SetHandler application/x-httpd-php
Now restart Apache.
$ sudo apachectl restart
For testing PHP, create test.php on Document root of Apache.