Friday, February 20, 2015

Update PHP Version to 5.4.37 in Ubuntu 12.04

PHP 5.4.37 is not available in Ubuntu 12.04 repositories. You may update your PHP version from some other repositories (PPA).

I am going to use ondrej's oldstable PPA for updating PHP. You will have to run the following commands to complete the process:

1. sudo apt-get install python-software-properties
2. sudo add-apt-repository ppa:ondrej/php5-oldstable
3. sudo apt-get update
4. sudo apt-get install php5

The latest version of PHP will be installed.

Sunday, October 13, 2013

How to Install nginx on Ubuntu for Hosting PHP scripts

"nginx" is http server developed by Igor Sysoev. It became popular because of it's efficiency for serving static web pages. It's caching mechanism grabbed web administrators' attraction. PHP scripts can be hosted in nginx server too. Now, I am going discuss how to install nginx on Ubuntu and how to configure to host PHP scripts.

Installing nginx:

sudo apt-get install nginx

Start nginx server:

sudo service nginx start


Now, you may host your static html pages in nginx. Root directory of server is at:
/usr/share/nginx/www

If you want to host hello.html, you have to copy the "hello.html" file into the root directory and then browse from "http://localhost/hello.html".

Now install "php5-fpm" for executing PHP scripts:

sudo apt-get install php5-fpm

Now, you have to configure nginx to execute PHP scripts. If you ignore this part, then your hosted php scripts will be downloaded instead of executing.

Open the configuration file:

sudo nano /etc/nginx/sites-available/default

Now find the following line:

index index.html index.htm;

Then change as follow:

index index.php index.html index.htm;

Find following line too:

     #location ~ \.php$ {
     #   fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #    # With php5-cgi alone:
        fastcgi_pass 127.0.0.1:9000;
    #    # With php5-fpm:
    #    fastcgi_pass unix:/var/run/php5-fpm.sock;
     #   fastcgi_index index.php;
    #    include fastcgi_params;
    #}

Then change as follow:

location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #    # With php5-cgi alone:
        fastcgi_pass 127.0.0.1:9000;
    #    # With php5-fpm:
    #    fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }


Final updated texts will be as follows:

# You may add here your
# server {
#    ...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
    #listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /usr/share/nginx/www;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost2/
    server_name localhost;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to index.html
        try_files $uri $uri/ /index.html;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.2;
        deny all;
    }

    # Only for nginx-naxsi : process denied requests
    #location /RequestDenied {
        # For example, return an error code
        #return 418;
    #}

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = /50x.html {
    #    root /usr/share/nginx/www;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #    # With php5-cgi alone:
        fastcgi_pass 127.0.0.1:9000;
    #    # With php5-fpm:
    #    fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny all;
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen 8000;
#    listen somename:8080;
#    server_name somename alias another.alias;
#    root html;
#    index index.html index.htm;
#
#    location / {
#        try_files $uri $uri/ /index.html;
#    }
#}


# HTTPS server
#
#server {
#    listen 443;
#    server_name localhost;
#
#    root html;
#    index index.html index.htm;
#
#    ssl on;
#    ssl_certificate cert.pem;
#    ssl_certificate_key cert.key;
#
#    ssl_session_timeout 5m;
#
#    ssl_protocols SSLv3 TLSv1;
#    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
#    ssl_prefer_server_ciphers on;
#
#    location / {
#        try_files $uri $uri/ /index.html;
#    }
#}

Installation of nginx for hosting PHP scripts is complete.

Thursday, December 20, 2012

PHP Script Execution Nature, A Problem and Solution

Do you know that a php script cannot be executed simultaneously on a single server. Even another script which is included with a executing script also becomes unavailable for execution or being included with other script. Usually a php script with complex algorithms and multiple mysql operations takes approximately 0.005 seconds for completing execution. For usual scripting with some mysql operations and an limited amount of users, the execution nature of apache server (PHP) is perfect. But, suppose, if a script has single or multiple API request to remote servers, then it will take some extra seconds to accept the reply. By the mean time other requests will be queued and will be processed while the script becomes free.

Recently, I have created a script which has more than 25 API requests and requires around 1-2 minutes for completing the whole execution. As the script has some dependencies over some universal scripts of my application, so I needed to change something to make the system always accessible for other users. First thing came to mind was to divide the whole execution and run it on multiple execution to make sure it frees the related scripts for other users for some while. I have used a Session Variable ($_SESSION) where I have put all information required for requesting API request. For every execution of the script, now it sends only a single API request, and then frees the script and redirect to the same script for rest of the requests.

Friday, October 12, 2012

Enable mod_rewrite on apache2 server in Ubuntu

Open terminal write the following codes:

sudo a2enmod rewrite

sudo gedit /etc/apache2/sites-available/default


After executing the second code, a text editor will be open. Find the following code:

Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all


Edit and change like below:

Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all


Then restart apache

sudo service apache2 restart

-That's all.

Increase Maximum Memory Allocation Limit without changing in PHP.INI

Add the following code in your php code if you want to increase maximum memory allocation for any of your specific script which execute to create or modify bigger graphical or other types of files.

ini_set('memory_limit','800M');

800M represents 800 megabytes.

Wednesday, September 12, 2012

Need More Execution Time without Changing PHP.INI

Sometimes it is required to execute multiple commands in a single script that requires more time than usual. We can edit and increase execution time limit from "php.ini" to avoid termination of execution because of outrun of allowed execution time limit. Where increase is only needed for some specific functions or scripts it is unnecessary to change php.ini file. We can easily add more execution time by adding following line before the bigger time consuming functions/codes:

set_time_limit(120);

Code displayed above is defining 120 seconds as a limit of execution time.

Thursday, August 9, 2012

Database Design Best Practices - part 1

Never create multiple tables for storing similar information.

Suppose, you are going to make an inventory software for a shop where you will need to store purchase, sales and transfer information. You have two choices.

One is to create multiple tables for "purchase", "sales" and "transfer" information.

Second is to make a common table for every transaction of products for all of the given type of transactions.

Second is better.

Example:

Bad Practice
table: purchase (pur_id, date, order_by)
table: sales (sales_id, date, order_by)
table: transfer (tr_id, date, order_by)

Best Practice
table: orders (id, date, order_by, order_type)

(Here, bracketed elements are the names of attributes or fields of the table.)