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.
The Apache web server should be installed and working properly.
Follow these steps to serve dynamic contents with PHP:
- Install PHP7 and the Apache module for PHP support:
$ sudo apt-get update $ sudo apt-get install -y php7.0 libapache2-mod-php7.0
- Check if PHP is properly installed and which version has been installed:
$ php -v
- Create
index.php
under thepublic_html
directory of our site:$ cd /var/www/example.com/public_html $ vi index.php
- Add the following contents to
index.php
:<?php echo phpinfo(); ?>
- Save and exit the
index.php
file. - Open
example.com.conf
fromsites-available
:$ sudo vi /etc/apache2/sites-available/example.com.conf
- Add the following line under the
VirtualHost
directive:DirectoryIndex index.php index.html
- Save the changes and reload Apache:
$ sudo service apache2 reload
- Now, access your site with your browser, and you should see a page with information regarding the installed PHP:
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:
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.
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
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
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-server
. Tasksel is a program used to ease the installation of packages that are commonly used together.
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
$ php -v
Comments
Post a Comment