Tuesday, July 30, 2013

Installing NGINX, PHP, and CUBRID PHP Driver


In this article, I will show how to install NGINX, PHP and CUBRID-PHP Driver from source code in order to establish the web server environment. There are other ways to accomplish the same task, for instance, via the package manager of you operating system.

Configure PHP

First, let's get PHP Engine installed.

1. Download PHP source package.

The PHP source package can be downloaded in the PHP official site.
The PHP version needs to be 5.3 or 5.4. For example, php-5.4.11.tar.gz. When using PHP with NGINX, the FASTCGI is needed to process the PHP requests. The PHP-FPM is a FastCGI implementation, and the current PHP 5.3 and 5.4 already include the FPM part in the source code. More infomation about PHP-FPM can be found here.

2. Build PHP.

The concise build command is as below. You can add other options if neccessary. The --enable-fpm, in our case, is the important option that enables the PHP-FPM.
1
2
3
4
5
tar zxvf php-.tar.gz (or tar jxvf php-.tar.bz2)
cd php-
./configure --prefix=/home//php/ --enable-fpm
make
make install
After installation, you will find the php-fpm executable in /home//php/sbin/.

3. Configure PHP FPM.

When the php-fpm is started, it will read the configuration, such as the IP address and port to listen, and then run as daemon. So before starting it, we need to get its configuration ready.
Run the below command to create the php-fpm.conf by making a copy of the default template file:
1
2
cd /home//php/etc
cp php-fpm.conf.default php-fpm.conf
The below line in php-fpm.conf configures the IP address on which to accept FastCGI requests:
1
listen = 127.0.0.1:9000
The above listening address is important because NGINX needs this infomation to communicate with PHP-FPM. When configuring NGINX, this listening address will be set to fastcgi_pass parameter.

4. Run PHP FPM.

Run the following command to start the PHP-FPM.
1
2
cd /home//php/
sbin/php-fpm

Install CUBRID PHP Library

We have previously written several thorough tutorial on how to install CUBRID PHP driver. Please refer to CUBRID PHP Driver Installation Instructions for details.

Configure NGINX

1. Download NGINX.

The NGINX source package can be downloaded in the NGINX download page.

2. Build.

The FASTCGI is supported in NGINX by default. You can simply build as below. If necessary, you can add other configuration options.
1
2
3
4
5
tar zxvf nginx-.tar.gz
cd nginx-
./configure --prefix=/home//nginx/
make
make install

3. Configure the fastcgi section in nginx.conf.

After NGINX is installed, we need to configure the FASTCGI related parameters. The NGINX configuration file can be found at /home//nginx/conf/nginx.conf. You can refer to the below simple configuration for fastcgi.
You can read more about NGINX configuration at NGINX wiki site.
    server {
        listen       80; 
        server_name  localhost;
        root         html;

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

Test CUBRID PHP Driver

1. Create a test.php file with the following contents, and put it into the NGINX's document root:
1
2. Use your web browser to navigate to http://localhost/test.php to see the contents as shown below which will mean the CUBRID PHP driver is successfully installed and enabled.
CUBRID support
enabled
Driver Version
8.4.3.0001
CCI Version
8.4.3
3. Create a test2.php file with the following contents, and put it into the NGINX's document root as well.


    PHP Test for CUBRID
    


    

PHP Test for CUBRID

An Example of PHP in Action "; echo date("g:i A l, F j Y.");?>
\n", "class_name"); print "===================== "; while ($row = cubrid_fetch_row($req)) { printf("%10s \n", $row[0]); } cubrid_disconnect($conn); ?>
4. Use your web browser to navigate to http://localhost/test2.php to retrieve the sample data from a database. Note that the db_class table is a system table in CUBRID that exists in every database, so no need to create it manually.
This concludes this tutorial on how to install NGINX, PHP, and CUBRID PHP driver. If you have questions, feel free to ask at CUBRID Q&A site, Facebook, or Twitter.

No comments:

Post a Comment