This brief tutorial shows students and new users how to install PHP on Ubuntu 18.04 | 16.04.
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 is a widely-used, free, and efficient alternative to competitors such as Microsoft’s ASP.
If you’re a student or new user looking for a Linux system to start learning on, the easiest place to start is Ubuntu Linux OS…. It’s a great Linux operating system for beginners and folks looking for easier Linux distribution to use.
Ubuntu is an open source Linux operating systems that runs on desktops, laptops, server and other devices…
When using Ubuntu, you will find that Linux isn’t so different than Windows and other operating systems in so many ways, especially when it comes to using the system to get work done.
Both Ubuntu and Windows systems allow you to be productive, easy to use, reliable and enable you to install and run thousands of programs from gaming to productivity suite software for individuals and businesses..
When you’re ready to set up Apache2 environment with PHP support, follow the steps below:
Step 1: Install Apache2 Web Server
sudo apt install apache2
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
Step 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
The line above will allow PHP to function with many popular PHP based websites and applications.
Step 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.
Open PHP Apache2 configuration file by running the commands below
sudo nano /etc/php/7.x/apache2/php.ini
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
Next, lookup Apache2 dir.conf file and confirm the line below:
<IfModule mod_dir.c> DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm </IfModule> # 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.
Restart Apache2 and PHP services
sudo systemctl restart apache2.service
Step 4: Test PHP Setup
At this point, Apache2 and PHP should be installed and ready.. to test your Apache2 PHP settings, create a blank file with the line below:
sudo nano /var/www/html/phpinfo.php
Then add the line in the file and save.
<?php phpinfo( ); ?>
Save the file and open your browser and browse to the server name or IP address followed by phpinfo.php
You should see something similar to the image below… if you do, then you’re all good!
Congratulations! You’ve successfully installed and configured Apache2 and PHP on Ubuntu servers
You may also like the post below: