Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Friday, November 8, 2013

Nginx multiple sites in subdirectories

I recently started using Nginx server which seems to provide the best security options and also the highest performance for the websites. As I migrated from XAMPP which is one of the simplest ones to setup, I found it hard to setup multiple sites in subdirectories using Nginx. As I struggled to find out the correct configuration for setting it up, I thought it deserved a blog post and here it is.

I was trying to setup three websites under the root folder (in my case /usr/share/nginx/www) in an Ubuntu server and all of them are PHP based site. One is PHPMyAdmin, the other is an yii based PHP website and another website with a single PHP file(an API for a mobile app). You can start with the default config file in /etc/nginx/sites-available folder and use fast-cgi configuration for the PHP files. A typical configuration for a PHP site would look like this,


server {
    listen 80 default;
    server_name localhost;

    access_log /var/log/nginx/access.log;

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

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
If we have to setup multiple sites as sub-directories we need to define multiple location elements in the same config file as below. 

For my yii Site1,

 location /Site1/ {
                root /usr/share/nginx/www/Site1;
               try_files $uri $uri/ /index.php?$query_string;
        }

        # the images need a seperate entry as we dont want to concatenate that with index.php      
        location ~ /Site1/.+\.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
                root /usr/share/nginx/www/Site1;
        }
        # pass the PHP scripts to FastCGI server
        location ~ /Site1/.+\.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                allow 127.0.0.1;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_index index.php;
        }
For the phpmyadmin Site

  location /phpmyadmin/ {
                root /usr/share/nginx/www/phpmyadmin;
               try_files $uri $uri/ /index.php?$query_string;
        }

        # the image url for this site were including the folder so we can ignore that and provide root     
        location ~ /phpmyadmin/.+\.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
                root /usr/share/nginx/www;
        }
        
        # pass the PHP scripts to FastCGI server
        location ~ /phpmyadmin/.+\.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                allow 127.0.0.1;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_index index.php;
        }
For Site3, which is just a single php file and does not have any images


 location /Site3/ {
                root    /usr/share/nginx/www/Site3;
        }

        # pass the PHP scripts to FastCGI server
        location ~ /Site3/.+\.php$ {
                allow 127.0.0.1;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                include fastcgi_params;
                #we are directly using the $request_filename as its a single php script
                fastcgi_param SCRIPT_FILENAME $request_filename;
        }
Hope this helps someone who is having similar issues with setting up subdirectories for multiple sites. This is still not as straight forward as Xampp is but once we figure it out, its lot simple and everytime we add a subdirectory we can copy over the location settings.

Monday, May 27, 2013

Eclipse and XDebug


PHP Debugging in Eclipse

I have googled my way through many different forums and faced many problems in configuring Eclipse for PHP debugging. So this post is to document the steps so that its easier for any newbie to look and follow through.
  • Install Eclipse June(4.2.x) from the Eclipse site. Download the classic version http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops4/R-4.2.2-201302041200/eclipse-SDK-4.2.2-win32-x86_64.zip * Install the PHP Development tools plugin. In the Eclipse IDE, go to Help->Install New Software-> Add http://download.eclipse.org/tools/pdt/updates/release
  • Now the PHP development environment is setup.
  • To setup a local server, we can use the Xampp server which has the Apache, PHP, MySQL in a single package. Download it fromhttp://www.apachefriends.org/download.php?xampp-win32-1.8.1-VC9.zip . Note that this is an extractable copy which you can directly use without installing. (You can copy it extract the Zip file and copy the Xamp directory to C:\)
  • We have to configure the php ini file for xdebug. Go to C:\xampp\php\php.ini and go to the end of the while where Zend extension is configured. We have to correct it with C:\ path like this zend_extension = “C:\xampp\php\ext\php_xdebug.dll” Since this is important configuration details I am pasting the XDebug configuration below. Except the zend_extension and xdebug.remote_enable are commented and unused for now. (We can use other parameters if we want to set up remote debug).
  •  [XDebug]
    zend_extension = "C:\xampp\php\ext\php_xdebug.dll" 
    ;xdebug.profiler_append = 0
    ;xdebug.profiler_enable = 1
    ;xdebug.profiler_enable_trigger = 0
    ;xdebug.profiler_output_dir = "\xampp\tmp" 
    ;xdebug.profiler_output_name = "cachegrind.out.%t-%s" 
    xdebug.remote_enable = 1
    ;xdebug.remote_handler = "dbgp" 
    ;xdebug.remote_host = "127.0.0.1" 
    ;xdebug.trace_output_dir = "\xampp\tmp"
  • Go to C:\xampp and double click on xampp-control.ext. Choose language/country and then it will show a control panel to start/stop server. Start Apache server from there.

Create a new PHP project

  • Then start the Eclipse IDE with workspace as C:\xampp\htdocs.
  • Create New PHP project. (From File->New->PHP->PHP Project and give a name say MySite and click Finish). Since this is the first time we are creating PHP file, it will prompt to open PDT perspective and choose Ok.
  • Create a new PHP file by right clicking on the project folder in PHP Explorer. New->PHP File and name it as test.php. You can add a sample code in test.php file to test
    <?php
    $a =20;
    echo $a;
    ?>
    .
  • Now we have to setup the debugger to use XDebug. Go to menu Window->Preferences->PHP->Debug and under Debug Settings Choose XDebug as the PHP Debugger instead of Zend. Click Apply and Ok.
  • Right click on the test.php file and choose Debug As-> PHP Web Application. It will show a dialog with the URL and Click Ok. Now it will break in the first line of the PHP file. You can also set breakpoints in the php file before starting the execution and it will break at breakpoints