Friday, June 28, 2013

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.

Connect XAMPP's mysql on Linux terminal

Access XAMPP's mysql on Linux command line :

If you want to operate xampp's mysql on Linux terminal, follow the procedure.

By default, if you run
# mysql
It tries to connect to default mysql of the system and unfortunately default mysql is not XAMPP's mysql.

To open the xampp's mysql prompt on terminal, Run following command.
# /opt/lampp/bin/mysql or # /opt/lampp/bin/mysql -u root or # /opt/lampp/bin/mysql -h localhost -u root -p Default Password is blank, just press enter key.
/opt/lampp/bin/mysql is the path of the mysql binary file of xampp.

If you want to run XAMPP's mysql using 
# mysql
You should follow these steps.

1) Stop xampp & copy file /opt/lampp/bin/mysql
2) Rename the file /usr/bin/mysql to /usr/bin/mysql_default
    If you haven't installed mysql on your system, you will not get the file.
3) Paste file  /opt/lampp/bin/mysql into /usr/bin
4) Now whenever you will run mysql on terminal, it will open XAMPP"s mysql prompt.

Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

If you have installed apache2 and while starting or restarting apache2, you get this error. Try following steps to remove it.

Solution :

Add following line in /etc/apache2/apache2.conf
ServerName localhost
Restart apache2
# service apache2 restart
Now try again.