LAMP

Serving dynamic contents with PHP

In this recipe, we will learn how to install PHP and set it to work alongside the Apache web server. We will install PHP binaries and then the Apache module mod_php to support PHP-based dynamic contents.

Getting ready

You will need access to a root account or an account with sudo privileges.
The Apache web server should be installed and working properly.

How to do it…

Follow these steps to serve dynamic contents with PHP:
  1. Install PHP7 and the Apache module for PHP support:
    $ sudo apt-get update
    $ sudo apt-get install -y php7.0 libapache2-mod-php7.0
    
  2. Check if PHP is properly installed and which version has been installed:
    $ php -v
    
    How to do it…
  3. Create index.php under the public_html directory of our site:
    $ cd /var/www/example.com/public_html
    $ vi index.php
    
  4. Add the following contents to index.php:
    <?php echo phpinfo(); ?>
    
  5. Save and exit the index.php file.
  6. Open example.com.conf from sites-available:
    $ sudo vi /etc/apache2/sites-available/example.com.conf
    
  7. Add the following line under the VirtualHost directive:
    DirectoryIndex index.php index.html
    
    How to do it…
  8. Save the changes and reload Apache:
    $ sudo service apache2 reload
    
  9. Now, access your site with your browser, and you should see a page with information regarding the installed PHP:
    How to do it…

How it works…

Here, we have installed PHP binaries on our server along with the Apache module libapache2-mod-php7.0 to support dynamic content coded in PHP. A module, mod_php, runs inside Apache process and processes PHP scripts from within Apache itself. For mod_php to work, Apache needs to run with the mpm_prefork module. PHP setup completes all these settings and restarts the Apache server:
How it works…
After we have installed PHP and mod_php, we simply need to create a PHP script. We have created index.php with little code to display phpinfo. At this stage, if you have both index.html and index.php under the same directory; by default, index.html will take over and be rendered first. You will need to explicitly specify index.php to access the page as http://127.0.0.1/index.php. We have set a directive, DirectoryIndex, under Apache Virtual Host to set index.php as a default index file.

PHP SETTINGS

All PHP settings are listed under its own configuration file, php.ini. PHP comes with two sets of configurations, as follows:
/usr/lib/php/7.0/php.ini-development
The /usr/lib/php/7.0/php.ini-productionDevelopment file is customized for a development environment and enables options like display_errors. For production systems, you can use the configuration file, php.ini-production.
The preceding files can be treated as a reference configuration that ships with the PHP installation. A copy of php.ini-production can be found under /etc/php/7.0. Apache and CLI configurations are separated in respective directories. You can directly edit settings under these files or simply use default files by creating a symbolic link to the development or production file as follows:
$ cd /etc/php/7.0/apache2
$ sudo mv php.ini php.ini.orig
$ sudo ln -s /usr/lib/php/7.0/php.ini-development php.ini

There's more…

Along with PHP, Apache supports various other scripting languages for dynamic content. You can install modules for Perl, Python, Ruby, and other scripting languages.
Add Python support:
$ sudo apt-get install libapache2-mod-python
Add Perl support:
$ sudo apt-get install libapache2-mod-perl2
Add Ruby support:
$ sudo apt-get install libapache2-mod-passenger

INSTALLING THE LAMP STACK

If you are interested in installing the entire LAMP stack, then Ubuntu provides a single command to do so. Use the following command to install Apache, PHP, and MySQL collectively:
$ sudo apt-get install lamp-server^
Notice the caret symbol at the end of the command. If you miss this symbol, apt will return an error saying package not found.

NOTE

lamp-server is set in the Ubuntu repository as a task to install and configure Apache, PHP, and MySQL collectively. The caret symbol in apt-get command is used to specify the taskrather than the package. Alternatively, you can use the tasksel command as $ sudotasksel install lamp-serverTasksel is a program used to ease the installation of packages that are commonly used together.

UPGRADING PHP UNDER UBUNTU 14

As of Ubuntu 14.10, Ubuntu does not provide a package for PHP7 in its repository, but you can use a Debian package repository to upgrade your PHP version. This repository is maintained by Ondřej Surý.
Use the following commands to upgrade to PHP 7:
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt-get install php7.0
Check the PHP version after installation completes:
$ php -v

Comments

Popular posts from this blog

HAproxy logging

teamcity Automatic Agent Start under Linux

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all