Install PHP On Ubuntu 18.04 | 16.04 - Step by Step Process ?

If you're going to be developing any PHP application, you're mostly going to need PHP installed. PHP is an open source server scripting language use for creating dynamic web applications and websites.

PHP has different versions and releases you can use.

Starting from the oldest that is currently supported – PHP 7.3, and onto PHP 7.4 and the latest – PHP 8.0. We'll include instructions for PHP XX, PHP XX. 

We recommend that you install PHP 8.0 as it's stable and has lots of improvements and new features.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related PHP queries on their Linux Servers.

In this context, we shall look into how to install PHP on Ubuntu 18.04 | 16.04.

How to install PHP on Ubuntu ?

To perform this installation procedure, follow the steps below.

1. Install Apache2 Web Server

i. Run the command below to install Apache:

$ sudo apt install apache2

ii. After installing Apache2, the commands below can be used to stop, start and enable Apache2 service to always startup when the server boots up:

$ sudo systemctl stop apache2.service
$ sudo systemctl start apache2.service
$ sudo systemctl enable apache2.service

2. Install PHP and Related Modules

After installing Apache2 above, run the commands below to install PHP and related PHP modules. 

There are many PHP modules that perform different functions. However, there are some important ones that are always needed when developing PHP based websites.

$ sudo apt-get install php libapache2-mod-php php-mcrypt php-cli php-mysql php-gd php-imagick php-recode php-tidy php-xmlrpc

This command will allow PHP to function with many popular PHP based websites and applications.

3. Configure Apache2 PHP Settings

Now that Apache2 and PHP are installed, you may want to configure Apache2 to use PHP properly. 

The default Apache2 PHP configuration file is located at /etc/php/7.x/apache2/php.ini

The X in the location will be 0 or 1 depending on the php version installed.

i. Open PHP Apache2 configuration file by running the commands below:

$ sudo nano /etc/php/7.x/apache2/php.ini

ii. Then edit the file to suit your environments. 

Some important lines to consider:

file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 64M

iii. Next, lookup Apache2 dir.conf file and confirm the line below:

<IfModule mod_dir.c>
     DirectoryIndex index.html index.cgi index.php index.xhtml index.htm
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

If you don't see the index.php definition on the line, please add it and save the file.

iv. To Restart Apache2 and PHP services, execute the command:

$ sudo systemctl restart apache2.service

4. Test PHP Setup

Here, Apache2 and PHP should be installed and ready. 

i. To test your Apache2 PHP settings, create a blank file with the line below:

$ sudo nano /var/www/html/phpinfo.php

ii. Then add the line in the file and save:

<?php phpinfo( ); ?>

iii. Save the file and open your browser and browse to the server name or IP address followed by phpinfo.php.

You should see a PHP Info page. If you do, then you're all good!

[Need urgent assistance in fixing PHP issues on your Ubuntu System? We can help you. ]

This article covers steps To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu Linux System. PHP stands for Hypertext Preprocessor, and it’s a script-based server-side programming language. PHP is often used to automate server tasks. It handles tasks like dynamic content, database requests, and processing and displaying data.

A "LAMP" stack is a group of open source software that is typically installed together to enable a server to host dynamic websites and web apps. 

This term is actually an acronym which represents the Linux operating system, with the Apache web server. 

The site data is stored in a MySQL database, and dynamic content is processed by PHP.

To Install PHP 7.2 with Apache on Ubuntu:

1. Ensure you are using the latest Ubuntu updates by entering the following command into a terminal window:

$ apt-get update && apt-get upgrade

2. To install PHP 7.2, enter the following command:

$ sudo apt-get install php libapache2-mod-php

Confirm the installation by hitting Enter. The system will download and install PHP from the software repositories. This command also installs dependencies and modules for use with Apache.

3. Verify PHP was installed, with the command:

$ php -v

Related Posts