Using php in linux

How to install and configure PHP

PHP is a general-purpose scripting language well-suited for Web development since PHP scripts can be embedded into HTML. This guide explains how to install and configure PHP in an Ubuntu System with Apache2 and MySQL.

Prerequisites

Before installing PHP you should install Apache (or a preferred web server) and a database service such as MySQL.

  • To install the Apache package, please refer to our Apache guide.
  • To install and configure a MySQL database service, refer to our MySQL guide.

Install PHP

PHP is available on Ubuntu Linux, but unlike Python (which comes pre-installed), must be manually installed.

To install PHP – and the Apache PHP module – you can enter the following command into a terminal prompt:

sudo apt install php libapache2-mod-php 

Install optional packages

The following packages are optional, and can be installed if you need them for your setup.

    PHP-CLI
    You can run PHP scripts via the Command Line Interface (CLI). To do this, you must first install the php-cli package. You can install it by running the following command:

sudo apt install php-mysql 
sudo apt install php-pgsql 

Configure PHP

If you have installed the libapache2-mod-php or php-cgi packages, you can run PHP scripts from your web browser. If you have installed the php-cli package, you can run PHP scripts at a terminal prompt.

By default, when libapache2-mod-php is installed, the Apache2 web server is configured to run PHP scripts using this module. First, verify if the files /etc/apache2/mods-enabled/php8.*.conf and /etc/apache2/mods-enabled/php8.*.load exist. If they do not exist, you can enable the module using the a2enmod command.

Once you have installed the PHP-related packages and enabled the Apache PHP module, you should restart the Apache2 web server to run PHP scripts, by running the following command:

sudo systemctl restart apache2.service 

Test your setup

To verify your installation, you can run the following PHP phpinfo script:

You can save the content in a file – phpinfo.php for example – and place it under the DocumentRoot directory of the Apache2 web server. Pointing your browser to http://hostname/phpinfo.php will display the values of various PHP configuration parameters.

Further reading

  • For more in depth information see the php.net documentation.
  • There are a plethora of books on PHP 7 and PHP 8. A good book from O’Reilly is Learning PHP, which includes an exploration of PHP 7’s enhancements to the language.
  • Also, see the Apache MySQL PHP Ubuntu Wiki page for more information.
Читайте также:  Immutable java classes list

Источник

Installation on Unix systems

This section will guide you through the general configuration and installation of PHP on Unix systems. Be sure to investigate any sections specific to your platform or web server before you begin the process.

As our manual outlines in the General Installation Considerations section, we are mainly dealing with web centric setups of PHP in this section, although we will cover setting up PHP for command line usage as well.

There are several ways to install PHP for the Unix platform, either with a compile and configure process, or through various pre-packaged methods. This documentation is mainly focused around the process of compiling and configuring PHP. Many Unix like systems have some sort of package installation system. This can assist in setting up a standard configuration, but if you need to have a different set of features (such as a secure server, or a different database driver), you may need to build PHP and/or your web server. If you are unfamiliar with building and compiling your own software, it is worth checking to see whether somebody has already built a packaged version of PHP with the features you need.

  • Basic Unix skills (being able to operate «make» and a C compiler)
  • An ANSI C compiler
  • A web server
  • Any module specific components (such as GD , PDF libs, etc.)
  • autoconf: 2.59+ (for PHP >= 7.0.0), 2.64+ (for PHP >= 7.2.0)
  • automake: 1.4+
  • libtool: 1.4.x+ (except 1.4.2)
  • re2c: 0.13.4+
  • bison:
    • PHP 7.0 — 7.3: 2.4 or later (including Bison 3.x)
    • PHP 7.4: > 3.0

    The initial PHP setup and configuration process is controlled by the use of the command line options of the configure script. You could get a list of all available options along with short explanations running ./configure —help. Our manual documents the different options separately. You will find the core options in the appendix, while the different extension specific options are described on the reference pages.

    When PHP is configured, you are ready to build the module and/or executables. The command make should take care of this. If it fails and you can’t figure out why, see the Problems section.

    Note:

    Some Unix systems (such as OpenBSD and SELinux) may disallow mapping pages both writable and executable for security reasons, what is called PaX MPROTECT or W^X violation protection. This kind of memory mapping is, however, necessary for PCRE’s JIT support, so either PHP has to be built without PCRE’s JIT support, or the binary has to be whitelisted by any means provided by the system.

    Note: Cross-compiling for ARM with the Android toolchain is currently not supported.

    Источник

    How to Use and Execute PHP Codes in Linux Command Line – Part 1

    PHP is an open source server side scripting Language which originally stood for ‘Personal Home Page‘ now stands for ‘PHP: Hypertext Preprocessor‘, which is a recursive acronym. It is a cross platform scripting language which is highly influenced by C, C++ and Java.

    Run PHP Codes in Linux Command Line

    A PHP Syntax is very similar to Syntax in C, Java and Perl Programming Language with a few PHP-specific feature. PHP is used by some 260 Million websites, as of now. The current stable release is PHP Version 5.6.10.

    PHP is HTML embedded script which facilitates developers to write dynamically generated pages quickly. PHP is primarily used on Server-side (and JavaScript on Client Side) to generate dynamic web pages over HTTP, however you will be surprised to know that you can execute a PHP in a Linux Terminal without the need of a web browser.

    This article aims at throwing light on the command-line aspect of PHP scripting Language.

    1. After PHP and Apache2 installation, we need to install PHP command Line Interpreter.

    # apt-get install php5-cli [Debian and alike System) # yum install php-cli [CentOS and alike System)

    Next thing, we do is to test a php (if installed correctly or not) commonly as by creating a file infophp.php at location ‘/var/www/html‘ (Apache2 working directory in most of the distros), with the content , simply by running the below command.

    # echo '' > /var/www/html/infophp.php

    and then point your browser to http://127.0.0.1/infophp.php which opens this file in web browser.

    Check PHP Info

    Same results can be obtained from the Linux terminal without the need of any browser. Run the PHP file located at ‘/var/www/html/infophp.php‘ in Linux Command Line as:

    # php -f /var/www/html/infophp.php

    Check PHP info from Commandline

    Since the output is too big we can pipeline the above output with ‘less‘ command to get one screen output at a time, simply as:

    # php -f /var/www/html/infophp.php | less

    Check All PHP Info

    Here Option ‘-f‘ parse and execute the file that follows the command.

    2. We can use phpinfo() which is a very valuable debugging tool directly on the Linux command-line without the need of calling it from a file, simply as:

    PHP Debugging Tool

    Here the option ‘-r‘ run the PHP Code in the Linux Terminal directly without tags < and >.

    3. Run PHP in Interactive mode and do some mathematics. Here option ‘-a‘ is for running PHP in Interactive Mode.

    # php -a Interactive shell php > echo 2+3; 5 php > echo 9-6; 3 php > echo 5*4; 20 php > echo 12/3; 4 php > echo 12/5; 2.4 php > echo 2+3-1; 4 php > echo 2+3-1*3; 2 php > exit

    Press ‘exit‘ or ‘ctrl+c‘ to close PHP interactive mode.

    Enable PHP Interactive Mode

    4. You can run a PHP script simply as, if it is a shell script. First Create a PHP sample script in your current working directory.

    # echo -e '#!/usr/bin/php\n' > phpscript.php

    Notice we used #!/usr/bin/php in the first line of this PHP script as we use to do in shell script (/bin/bash). The first line #!/usr/bin/php tells the Linux Command-Line to parse this script file to PHP Interpreter.

    Second make it executable as:

    5. You will be surprised to know you can create simple functions all by yourself using the interactive shell. Here is the step-by step instruction.

    Start PHP interactive mode.

    Create a function and name it addition. Also declare two variables $a and $b.

    php > function addition ($a, $b)

    Use curly braces to define rules in between them for this function.

    Define Rule(s). Here the rule say to add the two variables.

    All rules defined. Enclose rules by closing curly braces.

    Test function and add digits 4 and 3 simply as :

    php > var_dump (addition(4,3));
    Sample Output

    You may run the below code to execute the function, as many times as you want with different values. Replace a and b with values of yours.

    php > var_dump (addition(a,b));
    php > var_dump (addition(9,3.3));
    Sample Output

    Create PHP Functions

    You may run this function till you quit interactive mode (Ctrl+z). Also you would have noticed that in the above output the data type returned is NULL. This can be fixed by asking php interactive shell to return in place of echo.

    Simply replace the ‘echo‘ statement in the above function with ‘return

    and rest of the things and principles remain same.

    Here is an Example, which returns appropriate data-type in the output.

    PHP Functions

    Always Remember, user defined functions are not saved in history from shell session to shell session, hence once you exit the interactive shell, it is lost.

    Hope you liked this session. Keep Connected for more such posts. Stay Tuned and Healthy. Provide us with your valuable feedback in the comments. Like ans share us and help us get spread.

    Источник

Оцените статью