Run php script on command line in Linux

Run php files on terminal :

First make sure you should have following packages installed on your system.
  • php5 
  • libapache2-mod-php5
  • php5-cli
Method : I
Now run the script on the terminal
# php /path/of/the/phpfile.php
Disadvantage :
If your php file in the xampp and if it requires database connectivity, the file will not be run successfully because it will not be able to connect to database. It will try to connect to default mysql server. It does not connect to xampp's mysql server. You will have to change the socket path to connect to xampp's mysql server.

Method : II
You have another way to run php files on terminal but you should be able to run them successfully on your browser.
I mean, you should have a web-server (apache) and a database-server (mysql) on your system. Now the same way you run your php file on browser, we can run it on terminal too.

Suppose you can run your php file using following url in your browser.
http://localhost/project-directory/phpfile.php

Run following command on terminal.
# wget http://localhost/project-directory/phpfile.php
Disadvantage :
The disadvantage of this method is, every time when you run the command , it downloads the php file in current directory where you run the command.

Advantage :
Method Ist's disadvantage is Method IInd's advantage.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...