How to Install PHP 7 on Ubuntu 16.04 Running Apache Web Server

PHP (Hypertext Preprocessor) is one of the most preferred and powerful general purpose programming language. The open source scripting software runs millions of dynamic websites including CMS (Content Management Systems) like WordPress and OpenCart.

PHP popularity comes from the community support that builds free production frameworks such as Laravel . This has made the life of many PHP web developers extremely easy.

PHP still breaths despite a tough war from competitors such as ASP.net, Python, Node Js and Angular Js.

Nevertheless, PHP is efficient, free and a faster scripting language that works well with most Linux distributions.

You can install PHP on your Linux 16.04 VPS server by following this guide.

Pre-requisites

 Step 1: Using apt system to install PHP

PHP is maintained in the Ubuntu central installation repository so we can pull it from there using the apt-get command.

Before you do this, please update your system by typing.

sudo apt-get update

Then Run the following commands to install PHP

sudo apt-get install php libapache2-mod-php

Press Y and Enter when the installation confirmation prompt appears.

The libapache2-mod-php downloads the required module for an Apache server.

Step 2: Modify Apache default Index files

By default, Apache prioritizes index.html, index.cgi and Index.pl when a user requests a page from the root of a website or directory. To override this setting, we need to edit Apache dir.config file.

Type the command below to open the Apache directory configuration file and edit it using nano.

sudo nano /etc/apache2/mods-enabled/dir.conf

Once the file opens, moves index.php just after the text DirectoryIndex to give it precedence over other default index files. At last, your dir.conf file will then read as follows

<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Then, press CTR + X then Y and Enter when you are done to save the changes to the file

Step 3: Install basic PHP Modules

You probably want run a website or an application on your Ubuntu 16.04 VPS after installing Apache. Therefore, we need to install basic PHP modules to ensure your sites work without any problem.

To do this, type the command below

sudo apt-get install php7.0-cli php7.0-common php7.0-mbstring php7.0-gd php7.0-intl php7.0-xml php7.0-mysql php7.0-mcrypt php7.0-zip php7.0-curl php7.0-xmlrpc

The command above will install all the necessary PHP modules that you need to run your websites.

Step 4:  Configuring PHP

By default Php7 configuration file path is /etc/php/7.0/apache2/php.ini after the installation. We need to make some changes to that file so that our websites or applications will run without a problem.

To do this, type the command below to open PHP configuration file on the nano text editor:

sudo nano /etc/php/7.0/apache2/php.ini

You need to find the upload_max_filesize = 2M  AND post_max_size= 8M  settings on the php.ini file and change them to read as follows:

upload_max_filesize = 16M
post_max_size = 16M

You can increase the values according to your needs if you anticipate file uploads of more than 16MB

Then, press CTR + X then Y and Enter when you are done to save the changes to the php.ini file. 

Step 5: Test PHP

Finally, we can now test PHP by creating test file on the var/www/html/ folder. If you are running multiple websites using virtual hosts, you need to match the document root with the default public html folder that you created for your website(s).

Type the command below to create the info.php file

sudo nano /var/www/html/info.php

Then, enter the text below.

<?php

phpinfo();

?>

Press CTRL + X then Y and Enter when finished to make changes to the file.

Step 6: Restarting Apache

We need to restart Apache for all the changes we have made to be effected. We can do this by typing the command below.

sudo systemctl restart apache2

You can now visit the info.php file on your website using your domain name or IP address to see if PHP is working. You can do this by typing the below text on a web browser. Remember to replace example.com and 127.0.0.1 with your domain name or VPS server IP address respectively.

http://www.example.com/info.php

or 

http://127.0.0.1/info.php

If everything was set correctly, you will see a page that looks like this.

Conclusion

That’s it when it comes to installing PHP 7 on Ubuntu 16.04 server running an Apache web server. Remember, you can now create additional PHP files or probably run a content management system like WordPress and it should work pretty well when PHP is installed.

Ads by Google

One thought on “How to Install PHP 7 on Ubuntu 16.04 Running Apache Web Server”

Leave a Reply

Your email address will not be published. Required fields are marked *