How do i run a php script in ubuntu terminal?

I have installed XAMPP v1.8.3 for my PHP development. I am new to Ubuntu, so I don't know how to set environment variable for PHP which is located at /opt/lampp/bin/php.

I can run PHP scripts from localhost just fine, but I wanted to run them from the command line as well.

I want to set this variable for every user, since I am the only one who uses this system.

How do i run a php script in ubuntu terminal?

Zanna

67.3k54 gold badges206 silver badges318 bronze badges

asked Apr 13, 2014 at 14:17

sud_the_devilsud_the_devil

8456 gold badges12 silver badges19 bronze badges

To open an interactive php shell, just type in a terminal:

php -a

As for opening a file, just:

php filename.php

answered Apr 13, 2014 at 14:23

How do i run a php script in ubuntu terminal?

animaletdesequiaanimaletdesequia

8,1204 gold badges24 silver badges43 bronze badges

2

Environment variables are set in /etc/environment. You will find the $PATH variable in this file. This variable stores the path to binaries in various locations.

To add /opt/lampp/bin to the location searched for binary files, just append this path preceded by a : to the path variable.

For example, if the $PATH variable was:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

add /opt/lampp/bin to the end of it, so that it becomes:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/lampp/bin

After doing this, do a source /etc/environment.

Radu Rădeanu

161k47 gold badges318 silver badges395 bronze badges

answered Apr 13, 2014 at 14:25

8

As an alternative to /opt/lampp/bin/php, to run a php script from the command line, you just need to install php5-cli:

sudo apt-get install php5-cli

And run your script with:

php myscript.php

editor's note: depending on your version, you may need to install php7.0-cli etc instead

How do i run a php script in ubuntu terminal?

Zanna

67.3k54 gold badges206 silver badges318 bronze badges

answered Apr 13, 2014 at 14:22

How do i run a php script in ubuntu terminal?

Sylvain PineauSylvain Pineau

60.1k18 gold badges144 silver badges181 bronze badges

3

You can use

php /var/www/html/yourProjctFolder/yourFile.php

This will call your php file and output if you have written echo or print statement

How do i run a php script in ubuntu terminal?

Zanna

67.3k54 gold badges206 silver badges318 bronze badges

answered Apr 30, 2015 at 12:14

How do i run a php script in ubuntu terminal?

3

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    PHP Installation for Windows Users: Follow the steps to install PHP on the Windows operating system.

    • Step 1: First, we have to download PHP from it’s official website. We have to download the .zip file from the respective section depending upon on our system architecture(x86 or x64).
    • Step 2: Extract the .zip file to your preferred location. It is recommended to choose the Boot Drive(C Drive) inside a folder named php (ie. C:\php).
    • Step 3: Now we have to add the folder (C:\php) to the Environment Variable Path so that it becomes accessible from the command line. To do so, we have to right click on My Computer or This PC icon, then Choose Properties from the context menu. Then click the Advanced system settings link, and then click Environment Variables. In the section System Variables, we have to find the PATH environment variable and then select and Edit it. If the PATH environment variable does not exist, we have to click New. In the Edit System Variable (or New System Variable) window, we have to specify the value of the PATH environment variable (C:\php or the location of our extracted php files). After that, we have to click OK and close all remaining windows by clicking OK.

      How do i run a php script in ubuntu terminal?

    PHP Installation for Linux Users:

    • Linux users can install php using the following command.
      apt-get install php5-common libapache2-mod-php5 php5-cli

      It will install php with apache server. For more information click here.

      • PHP Installation for Mac Users:

        • Mac users can install php using the following command.
          curl -s https://php-osx.liip.ch/install.sh | bash -s 7.3

          It will install php in your system. For more information click here.

          • After installation of PHP, we are ready to run PHP code through command line. You just follow the steps to run PHP program using command line.

            • Open terminal or command line window.
            • Goto the specified folder or directory where php files are present.
            • Then we can run php code using the following command:
              php file_name.php

              How do i run a php script in ubuntu terminal?

            • We can also start server for testing the php code using the command line by the following command:
              php -S localhost:port -t your_folder/

              How do i run a php script in ubuntu terminal?

            Note: While using the PHP built-in server, the name of the PHP file inside the root folder must be index.php, and all other PHP files can be hyperlinked through the main index page.

            PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

    How do I run a PHP script in terminal?

    You just follow the steps to run PHP program using command line..
    Open terminal or command line window..
    Goto the specified folder or directory where php files are present..
    Then we can run php code using the following command: php file_name.php..

    How do I run a PHP script?

    php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.

    Can I run PHP on Ubuntu?

    PHP is a general-purpose scripting language suited for Web development. PHP scripts can be embedded into HTML. This section explains how to install and configure PHP in an Ubuntu System with Apache2 and MySQL. This section assumes you have installed and configured Apache2 Web Server and MySQL Database Server.

    How do I start PHP on Ubuntu?

    How to run PHP on Ubuntu.
    sudo apt-get install php. ... .
    In order to restart Apache web server just type the following command sudo gedit /var/www/html/index. ... .
    In this tutorial im using gedit to create my webpages but you can also install an IDE like netbeans and manage you projects from there in a way more efficient way..