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

Friday, October 5, 2012

What is cron and how to configure it with Drupal (secure) on a shared hosting

I can’t name myself a heavy Linux user developer, I don’t know by heart hundreds of commands, but recently I had to setup a hosting for NetBeans PHP Community Council. You can read my post about it at NetBeans PHP team blog. And I needed to setup cron to make my notifications in Drupal works.

So what is cron? Shortly cron is a time based job scheduler that allows you to run certain bash scripts in a certain frequency. What could we do with it? Well, a lot, think of email sending or verifying a certain flag in a web service, cleansing the database, you name it.

explaining cron configuration: CRONTAB

So cron has a configuration file: crontab, it describes the frequency at which the job runs and also the script that describes what will actually be done.

If you are in command line you can access crontab by typing

crontab -e

this will open the configuration file in your default Unix editor, usually vi.

FREQUENCY DESCRIPTION

Ok so how we describe frequency, there are 5 stars, which by default are placeholders. A star means ‘every’

* * * * *

Ok so what all these start mean

*  - minutes (from 0 to 59)

* – hours (from 0 to 23)

* – days of month (from 1 to 31)

* – month (from 1 to 12)

* – day of week (0 to 6)

So the previous example means

* * * * *  - every minute

* * * * 0 – every Sunday

*/5 * * * * – every 5 minutes

45 * * * * – every hour on 45th minute

15,45 * * * * – every hour on 15th and 45th minute

* * */15 * * – twice a month

I hope you got the idea. Also cron has some predefined values like @yearly, @monthly, @weekly, @daily and others.

CONFIGURE DRUPAL TO WORK WITH CRON

In Drupal we have different moving parts. One of these are notifications, which are very important and even more important in OpenAtrium, Drupal’s flavor. Notifications work with cron. To send all the notifications that are in the queue you can go to your Drupal site’s cron.php script, for example, at http://somedrupal.org/cron.php. At this point you’ll have in your administration panel, Administer –> Reports –> Recent logs, some entries related to cron script.

2012-10-09_2354

Now to make this script run at a certain frequency we’ll need to create a job for it. Drupal’s guide propose to make such an entry in your crontab:

45 * * * * /usr/bin/wget -O - -q -t 1 http://www.example.com/cron.php

When you set it up in your crontab –e everything should work fine.

CONFIGURE SHARED HOSTING TO WORK WITH CRON AnD WGET

On my shared hosting solution I hadn’t rights to execute wget command from /usr/bin/wget and my cron failed, so I made a ticket and the guys from the hosting made me a simple copy of wget binary into my /home with all the rights I needed. After that I modified the script from crontab so it could work with my local copy of wget, and everything cron related was working without a single issue from that day.

SECURE ACCESS TO CRON.PHP WITH .HTACCESS

Now that everything works ok, the question is how to configure your site that your cron.php script couldn’t be accessed from the browser. Otherwise anyone could just refresh /cron.php 5 millions time and put a big load on server.

We can avoid it by limiting access to cron.php file via .htaccess file.

  1. Find out your server’s name: use the command  uname –n.
  2. Put the following in .htaccess file
  3. <Files "cron.php">
      Order deny,allow
      Allow from WEBSERVERNAME
      Allow from 127.0.0.1
      Deny from all
    </Files>

  4. Change the WEBSERVERNAME with your own server’s name.
  5. Test that you can’t access the cron.php from browser
  6. Test that the cron job on linux is running and is executed OK.

 

That’s all I wanted to share with you today about cron, if you have any questions or suggestions please write a comment.

Thursday, January 19, 2012

Setting up Windows environment for Symfony2 developing with NetBeans, XAMPP and Git

[UPDATE] This is a series of post in the next one I’ll show you first Symfony2 app and how to make it work with Git. Second post link: Git and first Symfony2 project in NetBeans

Hi,

For a long time there was no sign of me here, but I hope this will change… So what do I do these days, well I want to go in Symfony2 to see how it feels like… Lots of guys were telling me PHP is way faster in development than in Java or .NET Web development so I want to try it out too.

So what do we actually need ?

XAMPP

No problem with that one. Get the latest version, install it but please don’t install MySql and Apache as services.  Than add a system environment variable like XAMPP_HOME for example and than add

;%XAMPP_HOME%\php;%XAMPP_HOME%\mysql\bin\

to Path environment variable. Now you should be able to check your PHP version from command line.

Try it now

XDEBUG based on your own phpinfo

A friend of mine showed me a wonderful site for getting your own DLL that is needed for your exactly configuration.

But first start your Apache server from your XAMPP control panel. Go to http://localhost/xampp/phpinfo.php and go get view source and copy all the HTML. Now go to http://www.xdebug.org/find-binary.php and insert the HTML into the textarea. Click on “Analyze my phpinfo output” button. And there you go you have your own DLL to download. Download it and put it into your php/ext folder

find_xdebug_binaryfind_xdebug_dll

Now go to your php.ini location and add these lines + the line starting with zend_extension

xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000
zend_extension = D:\xampp_server\php\ext\php_xdebug-2.1.2-5.3-vc9.dll

Now your machine should be ready for debug.


INSTALL mSYSGIT – GIT FOR Windows

Go and get the latest msysgit from and install it http://code.google.com/p/msysgit/downloads/list

2012-01-19_1144

SYMFONY2 Package

Go to http://symfony.com/download

Download the latest version in a .ZIP package. At the time of writing this it was Symfony Standart 2.0.9.zip

Make a separate folder and put this zip into it (in my case symfony2_php_sources). Also unzip it into another folder. (in my case symfony2_php_sources/Symfony).

IDE

Well we have some options like Eclipse and NetBeans also some support in PHPStorm, but I choose NetBeans 7.1.

There are a number of reasons why I chose it:

  • Out of box support for YAML
  • Support for Symfony2 (plugin)
  • Support for Twig (templating engine in Symfony2) also plugin
  • More user friendly than Eclipse (I guess someone could argue that, but I worked in Eclipse for 1 year).

So let’s starts go to  http://netbeans.org/downloads/

2012-01-19_1035

Add plugin for Symfony2. Go to Tools –> Plugins –> Tab Available Plugins –> click “Reload Catalog” –: in Search box input “symfony2” and press Enter. Now there should be a plugin there, called PHP Symfony2 Framework. Install it and restart NetBeans.

Add plugin for Twig. For now it is available for download from https://github.com/blogsh/Twig-netbeans/downloads as a NBM file. Download the latest file.

And install it in NetBeans. 

Now let’s tweak it a little bit. Go to Tools –> Options –> PHP.

Here we’ll have a little fun go to General tab –> Indicate your php.exe location in XAMPP. Also add in Global include path unpacked Symfony2 sources.

Go to Unit Testing tab and indicate the location of your phpunit.bat

Go to Symfony2 tab and indicate the location of the downloaded Symfony2 zip file.

Here is the cumulative actions screenshot

THAT’S ALL. Now your machine is configured.

Next time I’ll show you how to work with your first Symfony2 project and Git.

If you have any concerns, suggestions, ideas for blog posts please comment here.