Install and Test PHP on Linux system

Q. How can I install and test PHP on Linux? How do I make sure it is working on my Linux system? I am using Red Hat Fedora Core Linux.

A. PHP (PHP Hypertext Preprocessor) is an open-source, programming language. Originally designed as a high level scripting language for producing dynamic Web pages, PHP is used mainly in server-side application software.

Task: Install PHP

If you are using Fedora Core or Cent Os Linux, type the following command:
# yum install php

If you are using Debian Linux, type the following command:
# apt-get install php5

Now restart Web Server:
# /etc/init.d/httpd restart

Task: Test php

Go to /var/www/html/ directory:
# cd /var/www/html/

Create a php file – phptest.php
# vi phptest.php

Append following lines:
<?php phpinfo(); ?>

Save and close the file.

Open a web browser and type the url : http://yourdomain.com/phptest.php or http://localhost/phptest.php or http://server-ip-address/phptest.php.

See sample output here


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 12 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
12 comments… add one
  • alaa ali Aug 22, 2009 @ 21:04

    when i try to test my php a window appears: you have chosen to open test.php which is a php scripts.. what is this means? and how to solve this problem.. thanks in advance

  • DEA Sep 2, 2009 @ 12:11

    Works great on Cent OS 5, Thanks!

  • mpande Jan 11, 2011 @ 12:06

    To complete shutdown and exit just enter the command
    init 0
    Though remember to be root.
    i.e
    #init 0

  • Andrew Aug 6, 2011 @ 19:09

    eroer 403, forbidden

  • Dulanja Subodhana Sep 8, 2012 @ 20:27

    I can’t do step 2. Restart the web service.
    There is no httpd in /etc/init.d/
    What is the wrong. I’m using Ubuntu 11.10 with fvwm-crystal

    • Alivallo Feb 26, 2013 @ 0:49

      httpd does not exits.
      that is because you need to install Apache web server.
      after that you may find the file exists in /etc/apache/init.d/httpd

  • Rudy Aug 22, 2013 @ 18:11

    ServerName changed to:
    ServerName :80

    But explicit hostname did not work in URL only ‘localhost/phptest.php’ worked

    Using DHCP on Red Hat Enterprise Linux Workstation release 6.3 (Santiago) so I’m not sure how to specify the IP entry in /etc/hosts

    Kernel: 2.6.32-279.19.1.el6.x86_64

    • Rudy Aug 22, 2013 @ 18:17

      Correction:
      ServerName (my_hostname):80

      Comment removed &lt &gt symbols and text in-between

  • phpcoder Sep 17, 2013 @ 20:11

    It does not work I can’t copy nor move my php directory with all my scripts to var/www/html

    • chrnux Nov 15, 2013 @ 16:46

      are you logged in as root ?

      • phpcoder Nov 15, 2013 @ 18:07

        I found a solution my dear friend, thanks for responding.
        It was my lampp installation it had problems regarding its sockets.

  • aarushi May 13, 2016 @ 7:00

    in my file system – /etc/init.d/ does not contain httpd !

    What to do?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum